[unity] Some code and editor cleanup.

This commit is contained in:
pharan 2018-10-06 02:37:09 +08:00
parent 801081eae0
commit bb73f7d6e4
6 changed files with 83 additions and 73 deletions

View File

@ -32,20 +32,20 @@ using UnityEngine;
namespace Spine.Unity.Editor {
public static class AssetDatabaseAvailabilityDetector {
const string MARKER_RESOURCE_NAME = "SpineAssetDatabaseMarker";
private static bool _isMarkerLoaded;
const string MarkerResourceName = "SpineAssetDatabaseMarker";
private static bool isMarkerLoaded;
public static bool IsAssetDatabaseAvailable (bool forceCheck = false) {
if (!forceCheck && _isMarkerLoaded)
if (!forceCheck && isMarkerLoaded)
return true;
TextAsset markerTextAsset = Resources.Load<TextAsset>(MARKER_RESOURCE_NAME);
_isMarkerLoaded = markerTextAsset != null;
TextAsset markerTextAsset = Resources.Load<TextAsset>(AssetDatabaseAvailabilityDetector.MarkerResourceName);
isMarkerLoaded = markerTextAsset != null;
if (markerTextAsset != null) {
Resources.UnloadAsset(markerTextAsset);
}
return _isMarkerLoaded;
return isMarkerLoaded;
}
}
}

View File

@ -228,11 +228,11 @@ namespace Spine.Unity.Editor {
EditorGUI.EndDisabledGroup();
// Flip
EditorGUILayout.BeginHorizontal(GUILayout.MaxWidth(160f));
EditorGUILayout.LabelField("Scale", GUILayout.MaxWidth(EditorGUIUtility.labelWidth - 20f));
skeleton.ScaleX = EditorGUILayout.DelayedFloatField(".ScaleX", skeleton.ScaleX, GUILayout.MaxWidth(70f));
skeleton.ScaleY = EditorGUILayout.DelayedFloatField(".ScaleY", skeleton.ScaleY, GUILayout.MaxWidth(70f));
GUILayout.EndHorizontal();
skeleton.ScaleX = EditorGUILayout.DelayedFloatField(".ScaleX", skeleton.ScaleX);
skeleton.ScaleY = EditorGUILayout.DelayedFloatField(".ScaleY", skeleton.ScaleY);
//EditorGUILayout.BeginHorizontal(GUILayout.MaxWidth(160f));
////EditorGUILayout.LabelField("Scale", GUILayout.Width(EditorGUIUtility.labelWidth - 20f));
//GUILayout.EndHorizontal();
// Color
skeleton.SetColor(EditorGUILayout.ColorField(".R .G .B .A", skeleton.GetColor()));

View File

@ -134,7 +134,7 @@ namespace Spine.Unity.Editor {
}
EditorGUILayout.Space();
if (SpineInspectorUtility.LargeCenteredButton(SpineInspectorUtility.TempContent("Set Mipmap Bias to " + SpineEditorUtilities.Preferences.DEFAULT_MIPMAPBIAS))) {
if (SpineInspectorUtility.LargeCenteredButton(SpineInspectorUtility.TempContent("Set Mipmap Bias to " + SpineEditorUtilities.Preferences.DEFAULT_MIPMAPBIAS, tooltip: "This may help textures with mipmaps be less blurry when used for 2D sprites."))) {
foreach (var m in atlasAsset.materials) {
var texture = m.mainTexture;
texture.mipMapBias = SpineEditorUtilities.Preferences.DEFAULT_MIPMAPBIAS;
@ -261,12 +261,18 @@ namespace Spine.Unity.Editor {
}
#else
if (atlasFile.objectReferenceValue != null) {
EditorGUILayout.LabelField("Atlas Regions", EditorStyles.boldLabel);
int baseIndent = EditorGUI.indentLevel;
var regions = SpineAtlasAssetInspector.GetRegions(atlasAsset.GetAtlas());
int regionsCount = regions.Count;
using (new EditorGUILayout.HorizontalScope()) {
EditorGUILayout.LabelField("Atlas Regions", EditorStyles.boldLabel);
EditorGUILayout.LabelField(string.Format("{0} regions total", regionsCount));
}
AtlasPage lastPage = null;
for (int i = 0; i < regions.Count; i++) {
for (int i = 0; i < regionsCount; i++) {
if (lastPage != regions[i].page) {
if (lastPage != null) {
EditorGUILayout.Separator();

View File

@ -1165,15 +1165,13 @@ namespace Spine.Unity.Editor {
internal static readonly List<SkeletonComponentSpawnType> additionalSpawnTypes = new List<SkeletonComponentSpawnType>();
public static void IngestAdvancedRenderSettings (SkeletonRenderer skeletonRenderer) {
public static void TryInitializeSkeletonRendererSettings (SkeletonRenderer skeletonRenderer, Skin skin = null) {
const string PMAShaderQuery = "Spine/Skeleton";
const string TintBlackShaderQuery = "Tint Black";
if (skeletonRenderer == null)
return;
if (skeletonRenderer == null) return;
var skeletonDataAsset = skeletonRenderer.skeletonDataAsset;
if (skeletonDataAsset == null)
return;
if (skeletonDataAsset == null) return;
bool pmaVertexColors = false;
bool tintBlack = false;
@ -1199,6 +1197,14 @@ namespace Spine.Unity.Editor {
skeletonRenderer.pmaVertexColors = pmaVertexColors;
skeletonRenderer.tintBlack = tintBlack;
skeletonRenderer.zSpacing = SpineEditorUtilities.Preferences.defaultZSpacing;
var data = skeletonDataAsset.GetSkeletonData(false);
bool noSkins = data.DefaultSkin == null && (data.Skins == null || data.Skins.Count == 0); // Support attachmentless/skinless SkeletonData.
skin = skin ?? data.DefaultSkin ?? (noSkins ? null : data.Skins.Items[0]);
if (skin != null) {
skeletonRenderer.initialSkinName = skin.Name;
}
}
public static SkeletonAnimation InstantiateSkeletonAnimation (SkeletonDataAsset skeletonDataAsset, string skinName, bool destroyInvalid = true) {
@ -1227,8 +1233,9 @@ namespace Spine.Unity.Editor {
GameObject go = new GameObject(spineGameObjectName, typeof(MeshFilter), typeof(MeshRenderer), typeof(SkeletonAnimation));
SkeletonAnimation newSkeletonAnimation = go.GetComponent<SkeletonAnimation>();
newSkeletonAnimation.skeletonDataAsset = skeletonDataAsset;
IngestAdvancedRenderSettings(newSkeletonAnimation);
TryInitializeSkeletonRendererSettings(newSkeletonAnimation, skin);
// Initialize
try {
newSkeletonAnimation.Initialize(false);
} catch (System.Exception e) {
@ -1239,16 +1246,6 @@ namespace Spine.Unity.Editor {
throw e;
}
// Set Defaults
bool noSkins = data.DefaultSkin == null && (data.Skins == null || data.Skins.Count == 0); // Support attachmentless/skinless SkeletonData.
skin = skin ?? data.DefaultSkin ?? (noSkins ? null : data.Skins.Items[0]);
if (skin != null) {
newSkeletonAnimation.initialSkinName = skin.Name;
newSkeletonAnimation.skeleton.SetSkin(skin);
}
newSkeletonAnimation.zSpacing = SpineEditorUtilities.Preferences.defaultZSpacing;
newSkeletonAnimation.skeleton.Update(0);
newSkeletonAnimation.state.Update(0);
newSkeletonAnimation.state.Apply(newSkeletonAnimation.skeleton);
@ -1274,7 +1271,22 @@ namespace Spine.Unity.Editor {
return InstantiateSkeletonMecanim(skeletonDataAsset, skeletonDataAsset.GetSkeletonData(true).FindSkin(skinName));
}
public static SkeletonMecanim InstantiateSkeletonMecanim (SkeletonDataAsset skeletonDataAsset, Skin skin = null) {
public static SkeletonMecanim InstantiateSkeletonMecanim (SkeletonDataAsset skeletonDataAsset, Skin skin = null, bool destroyInvalid = true) {
SkeletonData data = skeletonDataAsset.GetSkeletonData(true);
if (data == null) {
for (int i = 0; i < skeletonDataAsset.atlasAssets.Length; i++) {
string reloadAtlasPath = AssetDatabase.GetAssetPath(skeletonDataAsset.atlasAssets[i]);
skeletonDataAsset.atlasAssets[i] = (AtlasAssetBase)AssetDatabase.LoadAssetAtPath(reloadAtlasPath, typeof(AtlasAssetBase));
}
data = skeletonDataAsset.GetSkeletonData(false);
}
if (data == null) {
Debug.LogWarning("InstantiateSkeletonMecanim tried to instantiate a skeleton from an invalid SkeletonDataAsset.");
return null;
}
string spineGameObjectName = string.Format("Spine Mecanim GameObject ({0})", skeletonDataAsset.name.Replace("_SkeletonData", ""));
GameObject go = new GameObject(spineGameObjectName, typeof(MeshFilter), typeof(MeshRenderer), typeof(Animator), typeof(SkeletonMecanim));
@ -1285,32 +1297,26 @@ namespace Spine.Unity.Editor {
go.GetComponent<Animator>().runtimeAnimatorController = skeletonDataAsset.controller;
SkeletonMecanim anim = go.GetComponent<SkeletonMecanim>();
anim.skeletonDataAsset = skeletonDataAsset;
IngestAdvancedRenderSettings(anim);
SkeletonMecanim newSkeletonMecanim = go.GetComponent<SkeletonMecanim>();
newSkeletonMecanim.skeletonDataAsset = skeletonDataAsset;
TryInitializeSkeletonRendererSettings(newSkeletonMecanim, skin);
SkeletonData data = skeletonDataAsset.GetSkeletonData(true);
if (data == null) {
for (int i = 0; i < skeletonDataAsset.atlasAssets.Length; i++) {
string reloadAtlasPath = AssetDatabase.GetAssetPath(skeletonDataAsset.atlasAssets[i]);
skeletonDataAsset.atlasAssets[i] = (AtlasAssetBase)AssetDatabase.LoadAssetAtPath(reloadAtlasPath, typeof(AtlasAssetBase));
// Initialize
try {
newSkeletonMecanim.Initialize(false);
} catch (System.Exception e) {
if (destroyInvalid) {
Debug.LogWarning("Editor-instantiated SkeletonAnimation threw an Exception. Destroying GameObject to prevent orphaned GameObject.");
GameObject.DestroyImmediate(go);
}
data = skeletonDataAsset.GetSkeletonData(true);
throw e;
}
// Set defaults
skin = skin ?? data.DefaultSkin ?? data.Skins.Items[0];
anim.zSpacing = SpineEditorUtilities.Preferences.defaultZSpacing;
newSkeletonMecanim.skeleton.Update(0);
newSkeletonMecanim.skeleton.UpdateWorldTransform();
newSkeletonMecanim.LateUpdate();
anim.Initialize(false);
anim.skeleton.SetSkin(skin);
anim.initialSkinName = skin.Name;
anim.skeleton.Update(0);
anim.skeleton.UpdateWorldTransform();
anim.LateUpdate();
return anim;
return newSkeletonMecanim;
}
#endif
#endregion

View File

@ -100,6 +100,13 @@ namespace Spine.Unity {
stateData = null;
}
public AnimationStateData GetAnimationStateData () {
if (stateData != null)
return stateData;
GetSkeletonData(false);
return stateData;
}
/// <summary>Loads, caches and returns the SkeletonData from the skeleton data file. Returns the cached SkeletonData after the first time it is called. Pass false to prevent direct errors from being logged.</summary>
public SkeletonData GetSkeletonData (bool quiet) {
if (skeletonJSON == null) {
@ -187,6 +194,18 @@ namespace Spine.Unity {
FillStateData();
}
public void FillStateData () {
if (stateData != null) {
stateData.defaultMix = defaultMix;
for (int i = 0, n = fromAnimation.Length; i < n; i++) {
if (fromAnimation[i].Length == 0 || toAnimation[i].Length == 0)
continue;
stateData.SetMix(fromAnimation[i], toAnimation[i], duration[i]);
}
}
}
internal Atlas[] GetAtlasArray () {
var returnList = new System.Collections.Generic.List<Atlas>(atlasAssets.Length);
for (int i = 0; i < atlasAssets.Length; i++) {
@ -215,25 +234,6 @@ namespace Spine.Unity {
return json.ReadSkeletonData(input);
}
public void FillStateData () {
if (stateData != null) {
stateData.defaultMix = defaultMix;
for (int i = 0, n = fromAnimation.Length; i < n; i++) {
if (fromAnimation[i].Length == 0 || toAnimation[i].Length == 0)
continue;
stateData.SetMix(fromAnimation[i], toAnimation[i], duration[i]);
}
}
}
public AnimationStateData GetAnimationStateData () {
if (stateData != null)
return stateData;
GetSkeletonData(false);
return stateData;
}
}
}

View File

@ -115,8 +115,6 @@ namespace Spine.Unity {
}
#endregion
void Reset () {
Clear();
}