[unity] Disambiguate and avoid magic method call.

This commit is contained in:
pharan 2016-11-06 22:31:37 +08:00
parent 28580ad111
commit 467a4e3836
5 changed files with 76 additions and 67 deletions

View File

@ -100,7 +100,11 @@ namespace Spine.Unity {
} }
#endregion #endregion
public virtual void Reset () { void Reset () {
Clear();
}
public virtual void Clear () {
atlas = null; atlas = null;
} }
@ -108,13 +112,13 @@ namespace Spine.Unity {
public virtual Atlas GetAtlas () { public virtual Atlas GetAtlas () {
if (atlasFile == null) { if (atlasFile == null) {
Debug.LogError("Atlas file not set for atlas asset: " + name, this); Debug.LogError("Atlas file not set for atlas asset: " + name, this);
Reset(); Clear();
return null; return null;
} }
if (materials == null || materials.Length == 0) { if (materials == null || materials.Length == 0) {
Debug.LogError("Materials not set for atlas asset: " + name, this); Debug.LogError("Materials not set for atlas asset: " + name, this);
Reset(); Clear();
return null; return null;
} }
@ -142,8 +146,8 @@ namespace Spine.Unity {
Vector3[] verts = new Vector3[4]; Vector3[] verts = new Vector3[4];
Vector2[] uvs = new Vector2[4]; Vector2[] uvs = new Vector2[4];
Color[] colors = new Color[4] { Color.white, Color.white, Color.white, Color.white }; Color[] colors = { Color.white, Color.white, Color.white, Color.white };
int[] triangles = new int[6] { 0, 1, 2, 2, 3, 0 }; int[] triangles = { 0, 1, 2, 2, 3, 0 };
float left, right, top, bottom; float left, right, top, bottom;
left = region.width / -2f; left = region.width / -2f;

View File

@ -43,16 +43,24 @@ namespace Spine.Unity.Editor {
[CustomEditor(typeof(AtlasAsset))] [CustomEditor(typeof(AtlasAsset))]
public class AtlasAssetInspector : UnityEditor.Editor { public class AtlasAssetInspector : UnityEditor.Editor {
private SerializedProperty atlasFile, materials; SerializedProperty atlasFile, materials;
private AtlasAsset atlasAsset; AtlasAsset atlasAsset;
readonly GUIContent SpriteSlicesLabel = new GUIContent( GUIContent spriteSlicesLabel;
"Apply Regions as Texture Sprite Slices", GUIContent SpriteSlicesLabel {
SpineEditorUtilities.Icons.unityIcon, get {
"Adds Sprite slices to atlas texture(s). " + if (spriteSlicesLabel == null) {
"Updates existing slices if ones with matching names exist. \n\n" + spriteSlicesLabel = new GUIContent(
"If your atlas was exported with Premultiply Alpha, " + "Apply Regions as Texture Sprite Slices",
"your SpriteRenderer should use the generated Spine _Material asset (or any Material with a PMA shader) instead of Sprites-Default."); SpineEditorUtilities.Icons.unityIcon,
"Adds Sprite slices to atlas texture(s). " +
"Updates existing slices if ones with matching names exist. \n\n" +
"If your atlas was exported with Premultiply Alpha, " +
"your SpriteRenderer should use the generated Spine _Material asset (or any Material with a PMA shader) instead of Sprites-Default.");
}
return spriteSlicesLabel;
}
}
static List<AtlasRegion> GetRegions (Atlas atlas) { static List<AtlasRegion> GetRegions (Atlas atlas) {
FieldInfo regionsField = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.NonPublic); FieldInfo regionsField = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.NonPublic);
@ -100,8 +108,12 @@ namespace Spine.Unity.Editor {
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(atlasFile); EditorGUILayout.PropertyField(atlasFile);
EditorGUILayout.PropertyField(materials, true); EditorGUILayout.PropertyField(materials, true);
if (EditorGUI.EndChangeCheck()) if (EditorGUI.EndChangeCheck()) {
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
atlasAsset.Clear();
atlasAsset.GetAtlas();
}
if (materials.arraySize == 0) { if (materials.arraySize == 0) {
EditorGUILayout.LabelField(new GUIContent("Error: Missing materials", SpineEditorUtilities.Icons.warning)); EditorGUILayout.LabelField(new GUIContent("Error: Missing materials", SpineEditorUtilities.Icons.warning));
@ -235,6 +247,7 @@ namespace Spine.Unity.Editor {
#else #else
if (atlasFile.objectReferenceValue != null) { if (atlasFile.objectReferenceValue != null) {
EditorGUILayout.LabelField("Atlas Regions", EditorStyles.boldLabel); EditorGUILayout.LabelField("Atlas Regions", EditorStyles.boldLabel);
int baseIndent = EditorGUI.indentLevel;
var regions = AtlasAssetInspector.GetRegions(atlasAsset.GetAtlas()); var regions = AtlasAssetInspector.GetRegions(atlasAsset.GetAtlas());
AtlasPage lastPage = null; AtlasPage lastPage = null;
@ -247,29 +260,24 @@ namespace Spine.Unity.Editor {
lastPage = regions[i].page; lastPage = regions[i].page;
Material mat = ((Material)lastPage.rendererObject); Material mat = ((Material)lastPage.rendererObject);
if (mat != null) { if (mat != null) {
EditorGUI.indentLevel = baseIndent;
GUILayout.BeginHorizontal(); using (new GUILayout.HorizontalScope())
{ using (new EditorGUI.DisabledGroupScope(true))
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.ObjectField(mat, typeof(Material), false, GUILayout.Width(250)); EditorGUILayout.ObjectField(mat, typeof(Material), false, GUILayout.Width(250));
EditorGUI.EndDisabledGroup(); EditorGUI.indentLevel = baseIndent + 1;
EditorGUI.indentLevel++;
}
GUILayout.EndHorizontal();
} else { } else {
EditorGUILayout.HelpBox("Page missing material!", MessageType.Warning); EditorGUILayout.HelpBox("Page missing material!", MessageType.Warning);
} }
} }
EditorGUILayout.LabelField(new GUIContent(regions[i].name, SpineEditorUtilities.Icons.image)); EditorGUILayout.LabelField(new GUIContent(regions[i].name, SpineEditorUtilities.Icons.image));
} }
EditorGUI.indentLevel--; EditorGUI.indentLevel = baseIndent;
} }
#endif #endif
if (serializedObject.ApplyModifiedProperties() || SpineInspectorUtility.UndoRedoPerformed(Event.current)) { if (serializedObject.ApplyModifiedProperties() || SpineInspectorUtility.UndoRedoPerformed(Event.current))
atlasAsset.Reset(); atlasAsset.Clear();
}
} }
static public void UpdateSpriteSlices (Texture texture, Atlas atlas) { static public void UpdateSpriteSlices (Texture texture, Atlas atlas) {

View File

@ -35,7 +35,8 @@ using Spine;
namespace Spine.Unity { namespace Spine.Unity {
public class SkeletonDataAsset : ScriptableObject { public class SkeletonDataAsset : ScriptableObject {
public AtlasAsset[] atlasAssets; #region Inspector
public AtlasAsset[] atlasAssets = new AtlasAsset[0];
#if SPINE_TK2D #if SPINE_TK2D
public tk2dSpriteCollectionData spriteCollection; public tk2dSpriteCollectionData spriteCollection;
public float scale = 1f; public float scale = 1f;
@ -43,13 +44,19 @@ namespace Spine.Unity {
public float scale = 0.01f; public float scale = 0.01f;
#endif #endif
public TextAsset skeletonJSON; public TextAsset skeletonJSON;
public String[] fromAnimation; public string[] fromAnimation = new string[0];
public String[] toAnimation; public string[] toAnimation = new string[0];
public float[] duration; public float[] duration = new float[0];
public float defaultMix; public float defaultMix;
public RuntimeAnimatorController controller; public RuntimeAnimatorController controller;
private SkeletonData skeletonData;
private AnimationStateData stateData; void Reset () {
Clear();
}
#endregion
SkeletonData skeletonData;
AnimationStateData stateData;
#region Runtime Instantiation #region Runtime Instantiation
/// <summary> /// <summary>
@ -62,7 +69,7 @@ namespace Spine.Unity {
/// Creates a runtime SkeletonDataAsset.</summary> /// Creates a runtime SkeletonDataAsset.</summary>
public static SkeletonDataAsset CreateRuntimeInstance (TextAsset skeletonDataFile, AtlasAsset[] atlasAssets, bool initialize, float scale = 0.01f) { public static SkeletonDataAsset CreateRuntimeInstance (TextAsset skeletonDataFile, AtlasAsset[] atlasAssets, bool initialize, float scale = 0.01f) {
SkeletonDataAsset skeletonDataAsset = ScriptableObject.CreateInstance<SkeletonDataAsset>(); SkeletonDataAsset skeletonDataAsset = ScriptableObject.CreateInstance<SkeletonDataAsset>();
skeletonDataAsset.Reset(); skeletonDataAsset.Clear();
skeletonDataAsset.skeletonJSON = skeletonDataFile; skeletonDataAsset.skeletonJSON = skeletonDataFile;
skeletonDataAsset.atlasAssets = atlasAssets; skeletonDataAsset.atlasAssets = atlasAssets;
skeletonDataAsset.scale = scale; skeletonDataAsset.scale = scale;
@ -74,12 +81,7 @@ namespace Spine.Unity {
} }
#endregion #endregion
void OnEnable () { public void Clear () {
if (atlasAssets == null)
atlasAssets = new AtlasAsset[0];
}
public void Reset () {
skeletonData = null; skeletonData = null;
stateData = null; stateData = null;
} }
@ -89,20 +91,20 @@ namespace Spine.Unity {
atlasAssets = new AtlasAsset[0]; atlasAssets = new AtlasAsset[0];
if (!quiet) if (!quiet)
Debug.LogError("Atlas not set for SkeletonData asset: " + name, this); Debug.LogError("Atlas not set for SkeletonData asset: " + name, this);
Reset(); Clear();
return null; return null;
} }
if (skeletonJSON == null) { if (skeletonJSON == null) {
if (!quiet) if (!quiet)
Debug.LogError("Skeleton JSON file not set for SkeletonData asset: " + name, this); Debug.LogError("Skeleton JSON file not set for SkeletonData asset: " + name, this);
Reset(); Clear();
return null; return null;
} }
#if !SPINE_TK2D #if !SPINE_TK2D
if (atlasAssets.Length == 0) { if (atlasAssets.Length == 0) {
Reset(); Clear();
return null; return null;
} }
#else #else
@ -115,12 +117,12 @@ namespace Spine.Unity {
Atlas[] atlasArr = new Atlas[atlasAssets.Length]; Atlas[] atlasArr = new Atlas[atlasAssets.Length];
for (int i = 0; i < atlasAssets.Length; i++) { for (int i = 0; i < atlasAssets.Length; i++) {
if (atlasAssets[i] == null) { if (atlasAssets[i] == null) {
Reset(); Clear();
return null; return null;
} }
atlasArr[i] = atlasAssets[i].GetAtlas(); atlasArr[i] = atlasAssets[i].GetAtlas();
if (atlasArr[i] == null) { if (atlasArr[i] == null) {
Reset(); Clear();
return null; return null;
} }
} }
@ -179,19 +181,14 @@ namespace Spine.Unity {
} }
public void FillStateData () { public void FillStateData () {
if (stateData == null) if (stateData != null) {
return; stateData.defaultMix = defaultMix;
stateData.DefaultMix = defaultMix; for (int i = 0, n = fromAnimation.Length; i < n; i++) {
if (fromAnimation[i].Length == 0 || toAnimation[i].Length == 0)
// For compatibility with runtime-instantiated SkeletonDataAsset. continue;
if (fromAnimation == null || toAnimation == null) stateData.SetMix(fromAnimation[i], toAnimation[i], duration[i]);
return; }
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]);
} }
} }

View File

@ -134,9 +134,9 @@ namespace Spine.Unity.Editor {
if (component.skeletonDataAsset != null) { if (component.skeletonDataAsset != null) {
foreach (AtlasAsset aa in component.skeletonDataAsset.atlasAssets) { foreach (AtlasAsset aa in component.skeletonDataAsset.atlasAssets) {
if (aa != null) if (aa != null)
aa.Reset(); aa.Clear();
} }
component.skeletonDataAsset.Reset(); component.skeletonDataAsset.Clear();
} }
component.Initialize(true); component.Initialize(true);
} }
@ -180,9 +180,9 @@ namespace Spine.Unity.Editor {
if (component.skeletonDataAsset != null) { if (component.skeletonDataAsset != null) {
foreach (AtlasAsset aa in component.skeletonDataAsset.atlasAssets) { foreach (AtlasAsset aa in component.skeletonDataAsset.atlasAssets) {
if (aa != null) if (aa != null)
aa.Reset(); aa.Clear();
} }
component.skeletonDataAsset.Reset(); component.skeletonDataAsset.Clear();
} }
component.Initialize(true); component.Initialize(true);
} }

View File

@ -545,7 +545,7 @@ namespace Spine.Unity.Editor {
bool abortSkeletonImport = false; bool abortSkeletonImport = false;
foreach (string sp in skeletonPaths) { foreach (string sp in skeletonPaths) {
if (!reimport && CheckForValidSkeletonData(sp)) { if (!reimport && CheckForValidSkeletonData(sp)) {
ResetExistingSkeletonData(sp); ClearExistingSkeletonData(sp);
continue; continue;
} }
@ -610,7 +610,7 @@ namespace Spine.Unity.Editor {
// Any post processing of images // Any post processing of images
} }
static void ResetExistingSkeletonData (string skeletonJSONPath) { static void ClearExistingSkeletonData (string skeletonJSONPath) {
string dir = Path.GetDirectoryName(skeletonJSONPath); string dir = Path.GetDirectoryName(skeletonJSONPath);
TextAsset textAsset = (TextAsset)AssetDatabase.LoadAssetAtPath(skeletonJSONPath, typeof(TextAsset)); TextAsset textAsset = (TextAsset)AssetDatabase.LoadAssetAtPath(skeletonJSONPath, typeof(TextAsset));
DirectoryInfo dirInfo = new DirectoryInfo(dir); DirectoryInfo dirInfo = new DirectoryInfo(dir);
@ -625,7 +625,7 @@ namespace Spine.Unity.Editor {
if (Selection.activeObject == skeletonDataAsset) if (Selection.activeObject == skeletonDataAsset)
Selection.activeObject = null; Selection.activeObject = null;
skeletonDataAsset.Reset(); skeletonDataAsset.Clear();
string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(skeletonDataAsset)); string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(skeletonDataAsset));
string lastHash = EditorPrefs.GetString(guid + "_hash"); string lastHash = EditorPrefs.GetString(guid + "_hash");
@ -970,7 +970,7 @@ namespace Spine.Unity.Editor {
if (AssetDatabase.GetAssetPath(atlasAsset) == "") if (AssetDatabase.GetAssetPath(atlasAsset) == "")
AssetDatabase.CreateAsset(atlasAsset, atlasPath); AssetDatabase.CreateAsset(atlasAsset, atlasPath);
else else
atlasAsset.Reset(); atlasAsset.Clear();
EditorUtility.SetDirty(atlasAsset); EditorUtility.SetDirty(atlasAsset);
AssetDatabase.SaveAssets(); AssetDatabase.SaveAssets();
@ -1098,7 +1098,7 @@ namespace Spine.Unity.Editor {
AssetDatabase.SaveAssets(); AssetDatabase.SaveAssets();
} else { } else {
skeletonDataAsset.atlasAssets = atlasAssets; skeletonDataAsset.atlasAssets = atlasAssets;
skeletonDataAsset.Reset(); skeletonDataAsset.Clear();
skeletonDataAsset.GetSkeletonData(true); skeletonDataAsset.GetSkeletonData(true);
} }