[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 { namespace Spine.Unity.Editor {
public static class AssetDatabaseAvailabilityDetector { public static class AssetDatabaseAvailabilityDetector {
const string MARKER_RESOURCE_NAME = "SpineAssetDatabaseMarker"; const string MarkerResourceName = "SpineAssetDatabaseMarker";
private static bool _isMarkerLoaded; private static bool isMarkerLoaded;
public static bool IsAssetDatabaseAvailable (bool forceCheck = false) { public static bool IsAssetDatabaseAvailable (bool forceCheck = false) {
if (!forceCheck && _isMarkerLoaded) if (!forceCheck && isMarkerLoaded)
return true; return true;
TextAsset markerTextAsset = Resources.Load<TextAsset>(MARKER_RESOURCE_NAME); TextAsset markerTextAsset = Resources.Load<TextAsset>(AssetDatabaseAvailabilityDetector.MarkerResourceName);
_isMarkerLoaded = markerTextAsset != null; isMarkerLoaded = markerTextAsset != null;
if (markerTextAsset != null) { if (markerTextAsset != null) {
Resources.UnloadAsset(markerTextAsset); Resources.UnloadAsset(markerTextAsset);
} }
return _isMarkerLoaded; return isMarkerLoaded;
} }
} }
} }

View File

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

View File

@ -134,7 +134,7 @@ namespace Spine.Unity.Editor {
} }
EditorGUILayout.Space(); 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) { foreach (var m in atlasAsset.materials) {
var texture = m.mainTexture; var texture = m.mainTexture;
texture.mipMapBias = SpineEditorUtilities.Preferences.DEFAULT_MIPMAPBIAS; texture.mipMapBias = SpineEditorUtilities.Preferences.DEFAULT_MIPMAPBIAS;
@ -261,12 +261,18 @@ namespace Spine.Unity.Editor {
} }
#else #else
if (atlasFile.objectReferenceValue != null) { if (atlasFile.objectReferenceValue != null) {
EditorGUILayout.LabelField("Atlas Regions", EditorStyles.boldLabel);
int baseIndent = EditorGUI.indentLevel; int baseIndent = EditorGUI.indentLevel;
var regions = SpineAtlasAssetInspector.GetRegions(atlasAsset.GetAtlas()); 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; 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 != regions[i].page) {
if (lastPage != null) { if (lastPage != null) {
EditorGUILayout.Separator(); EditorGUILayout.Separator();

View File

@ -1165,15 +1165,13 @@ namespace Spine.Unity.Editor {
internal static readonly List<SkeletonComponentSpawnType> additionalSpawnTypes = new List<SkeletonComponentSpawnType>(); 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 PMAShaderQuery = "Spine/Skeleton";
const string TintBlackShaderQuery = "Tint Black"; const string TintBlackShaderQuery = "Tint Black";
if (skeletonRenderer == null) if (skeletonRenderer == null) return;
return;
var skeletonDataAsset = skeletonRenderer.skeletonDataAsset; var skeletonDataAsset = skeletonRenderer.skeletonDataAsset;
if (skeletonDataAsset == null) if (skeletonDataAsset == null) return;
return;
bool pmaVertexColors = false; bool pmaVertexColors = false;
bool tintBlack = false; bool tintBlack = false;
@ -1199,6 +1197,14 @@ namespace Spine.Unity.Editor {
skeletonRenderer.pmaVertexColors = pmaVertexColors; skeletonRenderer.pmaVertexColors = pmaVertexColors;
skeletonRenderer.tintBlack = tintBlack; 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) { 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)); GameObject go = new GameObject(spineGameObjectName, typeof(MeshFilter), typeof(MeshRenderer), typeof(SkeletonAnimation));
SkeletonAnimation newSkeletonAnimation = go.GetComponent<SkeletonAnimation>(); SkeletonAnimation newSkeletonAnimation = go.GetComponent<SkeletonAnimation>();
newSkeletonAnimation.skeletonDataAsset = skeletonDataAsset; newSkeletonAnimation.skeletonDataAsset = skeletonDataAsset;
IngestAdvancedRenderSettings(newSkeletonAnimation); TryInitializeSkeletonRendererSettings(newSkeletonAnimation, skin);
// Initialize
try { try {
newSkeletonAnimation.Initialize(false); newSkeletonAnimation.Initialize(false);
} catch (System.Exception e) { } catch (System.Exception e) {
@ -1239,16 +1246,6 @@ namespace Spine.Unity.Editor {
throw e; 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.skeleton.Update(0);
newSkeletonAnimation.state.Update(0); newSkeletonAnimation.state.Update(0);
newSkeletonAnimation.state.Apply(newSkeletonAnimation.skeleton); newSkeletonAnimation.state.Apply(newSkeletonAnimation.skeleton);
@ -1274,7 +1271,22 @@ namespace Spine.Unity.Editor {
return InstantiateSkeletonMecanim(skeletonDataAsset, skeletonDataAsset.GetSkeletonData(true).FindSkin(skinName)); 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", "")); 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)); 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; go.GetComponent<Animator>().runtimeAnimatorController = skeletonDataAsset.controller;
SkeletonMecanim anim = go.GetComponent<SkeletonMecanim>(); SkeletonMecanim newSkeletonMecanim = go.GetComponent<SkeletonMecanim>();
anim.skeletonDataAsset = skeletonDataAsset; newSkeletonMecanim.skeletonDataAsset = skeletonDataAsset;
IngestAdvancedRenderSettings(anim); TryInitializeSkeletonRendererSettings(newSkeletonMecanim, skin);
SkeletonData data = skeletonDataAsset.GetSkeletonData(true); // Initialize
if (data == null) { try {
for (int i = 0; i < skeletonDataAsset.atlasAssets.Length; i++) { newSkeletonMecanim.Initialize(false);
string reloadAtlasPath = AssetDatabase.GetAssetPath(skeletonDataAsset.atlasAssets[i]); } catch (System.Exception e) {
skeletonDataAsset.atlasAssets[i] = (AtlasAssetBase)AssetDatabase.LoadAssetAtPath(reloadAtlasPath, typeof(AtlasAssetBase)); 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 newSkeletonMecanim.skeleton.Update(0);
skin = skin ?? data.DefaultSkin ?? data.Skins.Items[0]; newSkeletonMecanim.skeleton.UpdateWorldTransform();
anim.zSpacing = SpineEditorUtilities.Preferences.defaultZSpacing; newSkeletonMecanim.LateUpdate();
anim.Initialize(false); return newSkeletonMecanim;
anim.skeleton.SetSkin(skin);
anim.initialSkinName = skin.Name;
anim.skeleton.Update(0);
anim.skeleton.UpdateWorldTransform();
anim.LateUpdate();
return anim;
} }
#endif #endif
#endregion #endregion

View File

@ -100,6 +100,13 @@ namespace Spine.Unity {
stateData = null; 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> /// <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) { public SkeletonData GetSkeletonData (bool quiet) {
if (skeletonJSON == null) { if (skeletonJSON == null) {
@ -187,6 +194,18 @@ namespace Spine.Unity {
FillStateData(); 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 () { internal Atlas[] GetAtlasArray () {
var returnList = new System.Collections.Generic.List<Atlas>(atlasAssets.Length); var returnList = new System.Collections.Generic.List<Atlas>(atlasAssets.Length);
for (int i = 0; i < atlasAssets.Length; i++) { for (int i = 0; i < atlasAssets.Length; i++) {
@ -215,25 +234,6 @@ namespace Spine.Unity {
return json.ReadSkeletonData(input); 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 #endregion
void Reset () { void Reset () {
Clear(); Clear();
} }