diff --git a/spine-unity/Assets/spine-unity/Editor/SkeletonAnimationInspector.cs b/spine-unity/Assets/spine-unity/Editor/SkeletonAnimationInspector.cs index c009dca73..9cef3e48d 100644 --- a/spine-unity/Assets/spine-unity/Editor/SkeletonAnimationInspector.cs +++ b/spine-unity/Assets/spine-unity/Editor/SkeletonAnimationInspector.cs @@ -38,7 +38,7 @@ using Spine; public class SkeletonAnimationInspector : SkeletonRendererInspector { protected SerializedProperty animationName, loop, timeScale, autoReset; protected bool m_isPrefab; - protected GUIContent autoResetLabel; + protected bool wasAnimationNameChanged; protected override void OnEnable () { base.OnEnable(); @@ -56,43 +56,45 @@ public class SkeletonAnimationInspector : SkeletonRendererInspector { SkeletonAnimation component = (SkeletonAnimation)target; if (!component.valid) return; + + if (wasAnimationNameChanged) { + if (!Application.isPlaying) { + component.state.ClearTrack(0); + component.skeleton.SetToSetupPose(); + } - //catch case where SetAnimation was used to set track 0 without using AnimationName + Spine.Animation animationToUse = component.skeleton.Data.FindAnimation(animationName.stringValue); + + if (!Application.isPlaying) { + if (animationToUse != null) animationToUse.Apply(component.skeleton, 0f, 0f, false, null); + component.Update(); + component.LateUpdate(); + SceneView.RepaintAll(); + } else { + if (animationToUse != null) + component.state.SetAnimation(0, animationToUse, loop.boolValue); + else + component.state.ClearTrack(0); + } + + wasAnimationNameChanged = false; + } + + // Reflect animationName serialized property in the inspector even if SetAnimation API was used. if (Application.isPlaying) { - TrackEntry currentState = component.state.GetCurrent(0); - if (currentState != null) { + TrackEntry current = component.state.GetCurrent(0); + if (current != null) { if (component.AnimationName != animationName.stringValue) { - animationName.stringValue = currentState.Animation.Name; + animationName.stringValue = current.Animation.Name; } } } EditorGUILayout.Space(); - - //TODO: Refactor this to use GenericMenu and callbacks to avoid interfering with control by other behaviours. - // Animation name. - { - String[] animations = new String[component.skeleton.Data.Animations.Count + 1]; - animations[0] = ""; - int animationIndex = 0; - for (int i = 0; i < animations.Length - 1; i++) { - String name = component.skeleton.Data.Animations.Items[i].Name; - animations[i + 1] = name; - if (name == animationName.stringValue) - animationIndex = i + 1; - } - - animationIndex = EditorGUILayout.Popup("Animation", animationIndex, animations); - - String selectedAnimationName = animationIndex == 0 ? null : animations[animationIndex]; - if (component.AnimationName != selectedAnimationName) { - component.AnimationName = selectedAnimationName; - animationName.stringValue = selectedAnimationName; - } - - - } - + EditorGUI.BeginChangeCheck(); + EditorGUILayout.PropertyField(animationName); + wasAnimationNameChanged |= EditorGUI.EndChangeCheck(); + EditorGUILayout.PropertyField(loop); EditorGUILayout.PropertyField(timeScale); component.timeScale = Math.Max(component.timeScale, 0); diff --git a/spine-unity/Assets/spine-unity/Editor/SpineAttributeDrawers.cs b/spine-unity/Assets/spine-unity/Editor/SpineAttributeDrawers.cs index 7cdc9080e..ddf90af0a 100644 --- a/spine-unity/Assets/spine-unity/Editor/SpineAttributeDrawers.cs +++ b/spine-unity/Assets/spine-unity/Editor/SpineAttributeDrawers.cs @@ -5,17 +5,19 @@ *****************************************************************************/ using UnityEngine; using UnityEditor; +using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using Spine; + public struct SpineDrawerValuePair { public string str; public SerializedProperty property; - public SpineDrawerValuePair(string val, SerializedProperty property) { + public SpineDrawerValuePair (string val, SerializedProperty property) { this.str = val; this.property = property; } @@ -23,6 +25,7 @@ public struct SpineDrawerValuePair { public abstract class SpineTreeItemDrawerBase : PropertyDrawer where T:SpineAttributeBase { protected SkeletonDataAsset skeletonDataAsset; + protected T TargetAttribute { get { return (T)attribute; } } public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) { @@ -72,7 +75,7 @@ public abstract class SpineTreeItemDrawerBase : PropertyDrawer where T:SpineA return; GenericMenu menu = new GenericMenu(); - PopulateMenu (menu, property, this.TargetAttribute, data); + PopulateMenu(menu, property, this.TargetAttribute, data); menu.ShowAsContext(); } @@ -84,7 +87,7 @@ public abstract class SpineTreeItemDrawerBase : PropertyDrawer where T:SpineA pair.property.serializedObject.ApplyModifiedProperties(); } - public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { + public override float GetPropertyHeight (SerializedProperty property, GUIContent label) { return 18; } @@ -96,7 +99,7 @@ public class SpineSlotDrawer : SpineTreeItemDrawerBase { protected override void PopulateMenu (GenericMenu menu, SerializedProperty property, SpineSlot targetAttribute, SkeletonData data) { for (int i = 0; i < data.Slots.Count; i++) { string name = data.Slots.Items[i].Name; - if (name.StartsWith(targetAttribute.startsWith)) { + if (name.StartsWith(targetAttribute.startsWith, StringComparison.Ordinal)) { if (targetAttribute.containsBoundingBoxes) { int slotIndex = i; @@ -139,7 +142,7 @@ public class SpineSkinDrawer : SpineTreeItemDrawerBase { for (int i = 0; i < data.Skins.Count; i++) { string name = data.Skins.Items[i].Name; - if (name.StartsWith(targetAttribute.startsWith)) + if (name.StartsWith(targetAttribute.startsWith, StringComparison.Ordinal)) menu.AddItem(new GUIContent(name), name == property.stringValue, HandleSelect, new SpineDrawerValuePair(name, property)); } } @@ -150,9 +153,13 @@ public class SpineSkinDrawer : SpineTreeItemDrawerBase { public class SpineAnimationDrawer : SpineTreeItemDrawerBase { protected override void PopulateMenu (GenericMenu menu, SerializedProperty property, SpineAnimation targetAttribute, SkeletonData data) { var animations = skeletonDataAsset.GetAnimationStateData().SkeletonData.Animations; + + // item + menu.AddItem(new GUIContent(""), property.stringValue == "", HandleSelect, new SpineDrawerValuePair("", property)); + for (int i = 0; i < animations.Count; i++) { string name = animations.Items[i].Name; - if (name.StartsWith(targetAttribute.startsWith)) + if (name.StartsWith(targetAttribute.startsWith, StringComparison.Ordinal)) menu.AddItem(new GUIContent(name), name == property.stringValue, HandleSelect, new SpineDrawerValuePair(name, property)); } } @@ -165,7 +172,7 @@ public class SpineEventNameDrawer : SpineTreeItemDrawerBase { var events = skeletonDataAsset.GetSkeletonData(false).Events; for (int i = 0; i < events.Count; i++) { string name = events.Items[i].Name; - if (name.StartsWith(targetAttribute.startsWith)) + if (name.StartsWith(targetAttribute.startsWith, StringComparison.Ordinal)) menu.AddItem(new GUIContent(name), name == property.stringValue, HandleSelect, new SpineDrawerValuePair(name, property)); } } @@ -289,8 +296,8 @@ public class SpineBoneDrawer : SpineTreeItemDrawerBase { public class SpineAtlasRegionDrawer : PropertyDrawer { Component component; SerializedProperty atlasProp; - - public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { + + public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) { if (property.propertyType != SerializedPropertyType.String) { EditorGUI.LabelField(position, "ERROR:", "May only apply to type string"); return; @@ -321,7 +328,7 @@ public class SpineAtlasRegionDrawer : PropertyDrawer { } } - + void Selector (SerializedProperty property) { GenericMenu menu = new GenericMenu(); AtlasAsset atlasAsset = (AtlasAsset)atlasProp.objectReferenceValue; @@ -337,7 +344,7 @@ public class SpineAtlasRegionDrawer : PropertyDrawer { menu.ShowAsContext(); } - + static void HandleSelect (object val) { var pair = (SpineDrawerValuePair)val; pair.property.stringValue = pair.str; diff --git a/spine-unity/Assets/spine-unity/SkeletonAnimation.cs b/spine-unity/Assets/spine-unity/SkeletonAnimation.cs index b8479b016..e2739dded 100644 --- a/spine-unity/Assets/spine-unity/SkeletonAnimation.cs +++ b/spine-unity/Assets/spine-unity/SkeletonAnimation.cs @@ -68,6 +68,7 @@ public class SkeletonAnimation : SkeletonRenderer, ISkeletonAnimation { } [SerializeField] + [SpineAnimation] private String _animationName; public String AnimationName {