[Unity] SkeletonAnimationInspector now uses SpineAnimation attribute. Bypasses Spine.AnimationState in edit mode.

This commit is contained in:
pharan 2016-02-04 23:13:33 +08:00
parent 6de6a4712d
commit d2c2ce0e14
3 changed files with 51 additions and 41 deletions

View File

@ -38,7 +38,7 @@ using Spine;
public class SkeletonAnimationInspector : SkeletonRendererInspector { public class SkeletonAnimationInspector : SkeletonRendererInspector {
protected SerializedProperty animationName, loop, timeScale, autoReset; protected SerializedProperty animationName, loop, timeScale, autoReset;
protected bool m_isPrefab; protected bool m_isPrefab;
protected GUIContent autoResetLabel; protected bool wasAnimationNameChanged;
protected override void OnEnable () { protected override void OnEnable () {
base.OnEnable(); base.OnEnable();
@ -56,43 +56,45 @@ public class SkeletonAnimationInspector : SkeletonRendererInspector {
SkeletonAnimation component = (SkeletonAnimation)target; SkeletonAnimation component = (SkeletonAnimation)target;
if (!component.valid) if (!component.valid)
return; 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) { if (Application.isPlaying) {
TrackEntry currentState = component.state.GetCurrent(0); TrackEntry current = component.state.GetCurrent(0);
if (currentState != null) { if (current != null) {
if (component.AnimationName != animationName.stringValue) { if (component.AnimationName != animationName.stringValue) {
animationName.stringValue = currentState.Animation.Name; animationName.stringValue = current.Animation.Name;
} }
} }
} }
EditorGUILayout.Space(); EditorGUILayout.Space();
EditorGUI.BeginChangeCheck();
//TODO: Refactor this to use GenericMenu and callbacks to avoid interfering with control by other behaviours. EditorGUILayout.PropertyField(animationName);
// Animation name. wasAnimationNameChanged |= EditorGUI.EndChangeCheck();
{
String[] animations = new String[component.skeleton.Data.Animations.Count + 1];
animations[0] = "<None>";
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;
}
}
EditorGUILayout.PropertyField(loop); EditorGUILayout.PropertyField(loop);
EditorGUILayout.PropertyField(timeScale); EditorGUILayout.PropertyField(timeScale);
component.timeScale = Math.Max(component.timeScale, 0); component.timeScale = Math.Max(component.timeScale, 0);

View File

@ -5,17 +5,19 @@
*****************************************************************************/ *****************************************************************************/
using UnityEngine; using UnityEngine;
using UnityEditor; using UnityEditor;
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using Spine; using Spine;
public struct SpineDrawerValuePair { public struct SpineDrawerValuePair {
public string str; public string str;
public SerializedProperty property; public SerializedProperty property;
public SpineDrawerValuePair(string val, SerializedProperty property) { public SpineDrawerValuePair (string val, SerializedProperty property) {
this.str = val; this.str = val;
this.property = property; this.property = property;
} }
@ -23,6 +25,7 @@ public struct SpineDrawerValuePair {
public abstract class SpineTreeItemDrawerBase<T> : PropertyDrawer where T:SpineAttributeBase { public abstract class SpineTreeItemDrawerBase<T> : PropertyDrawer where T:SpineAttributeBase {
protected SkeletonDataAsset skeletonDataAsset; protected SkeletonDataAsset skeletonDataAsset;
protected T TargetAttribute { get { return (T)attribute; } } protected T TargetAttribute { get { return (T)attribute; } }
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) { public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
@ -72,7 +75,7 @@ public abstract class SpineTreeItemDrawerBase<T> : PropertyDrawer where T:SpineA
return; return;
GenericMenu menu = new GenericMenu(); GenericMenu menu = new GenericMenu();
PopulateMenu (menu, property, this.TargetAttribute, data); PopulateMenu(menu, property, this.TargetAttribute, data);
menu.ShowAsContext(); menu.ShowAsContext();
} }
@ -84,7 +87,7 @@ public abstract class SpineTreeItemDrawerBase<T> : PropertyDrawer where T:SpineA
pair.property.serializedObject.ApplyModifiedProperties(); pair.property.serializedObject.ApplyModifiedProperties();
} }
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { public override float GetPropertyHeight (SerializedProperty property, GUIContent label) {
return 18; return 18;
} }
@ -96,7 +99,7 @@ public class SpineSlotDrawer : SpineTreeItemDrawerBase<SpineSlot> {
protected override void PopulateMenu (GenericMenu menu, SerializedProperty property, SpineSlot targetAttribute, SkeletonData data) { protected override void PopulateMenu (GenericMenu menu, SerializedProperty property, SpineSlot targetAttribute, SkeletonData data) {
for (int i = 0; i < data.Slots.Count; i++) { for (int i = 0; i < data.Slots.Count; i++) {
string name = data.Slots.Items[i].Name; string name = data.Slots.Items[i].Name;
if (name.StartsWith(targetAttribute.startsWith)) { if (name.StartsWith(targetAttribute.startsWith, StringComparison.Ordinal)) {
if (targetAttribute.containsBoundingBoxes) { if (targetAttribute.containsBoundingBoxes) {
int slotIndex = i; int slotIndex = i;
@ -139,7 +142,7 @@ public class SpineSkinDrawer : SpineTreeItemDrawerBase<SpineSkin> {
for (int i = 0; i < data.Skins.Count; i++) { for (int i = 0; i < data.Skins.Count; i++) {
string name = data.Skins.Items[i].Name; 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)); menu.AddItem(new GUIContent(name), name == property.stringValue, HandleSelect, new SpineDrawerValuePair(name, property));
} }
} }
@ -150,9 +153,13 @@ public class SpineSkinDrawer : SpineTreeItemDrawerBase<SpineSkin> {
public class SpineAnimationDrawer : SpineTreeItemDrawerBase<SpineAnimation> { public class SpineAnimationDrawer : SpineTreeItemDrawerBase<SpineAnimation> {
protected override void PopulateMenu (GenericMenu menu, SerializedProperty property, SpineAnimation targetAttribute, SkeletonData data) { protected override void PopulateMenu (GenericMenu menu, SerializedProperty property, SpineAnimation targetAttribute, SkeletonData data) {
var animations = skeletonDataAsset.GetAnimationStateData().SkeletonData.Animations; var animations = skeletonDataAsset.GetAnimationStateData().SkeletonData.Animations;
// <None> item
menu.AddItem(new GUIContent("<None>"), property.stringValue == "", HandleSelect, new SpineDrawerValuePair("", property));
for (int i = 0; i < animations.Count; i++) { for (int i = 0; i < animations.Count; i++) {
string name = animations.Items[i].Name; 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)); menu.AddItem(new GUIContent(name), name == property.stringValue, HandleSelect, new SpineDrawerValuePair(name, property));
} }
} }
@ -165,7 +172,7 @@ public class SpineEventNameDrawer : SpineTreeItemDrawerBase<SpineEvent> {
var events = skeletonDataAsset.GetSkeletonData(false).Events; var events = skeletonDataAsset.GetSkeletonData(false).Events;
for (int i = 0; i < events.Count; i++) { for (int i = 0; i < events.Count; i++) {
string name = events.Items[i].Name; 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)); menu.AddItem(new GUIContent(name), name == property.stringValue, HandleSelect, new SpineDrawerValuePair(name, property));
} }
} }
@ -289,8 +296,8 @@ public class SpineBoneDrawer : SpineTreeItemDrawerBase<SpineBone> {
public class SpineAtlasRegionDrawer : PropertyDrawer { public class SpineAtlasRegionDrawer : PropertyDrawer {
Component component; Component component;
SerializedProperty atlasProp; 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) { if (property.propertyType != SerializedPropertyType.String) {
EditorGUI.LabelField(position, "ERROR:", "May only apply to type string"); EditorGUI.LabelField(position, "ERROR:", "May only apply to type string");
return; return;
@ -321,7 +328,7 @@ public class SpineAtlasRegionDrawer : PropertyDrawer {
} }
} }
void Selector (SerializedProperty property) { void Selector (SerializedProperty property) {
GenericMenu menu = new GenericMenu(); GenericMenu menu = new GenericMenu();
AtlasAsset atlasAsset = (AtlasAsset)atlasProp.objectReferenceValue; AtlasAsset atlasAsset = (AtlasAsset)atlasProp.objectReferenceValue;
@ -337,7 +344,7 @@ public class SpineAtlasRegionDrawer : PropertyDrawer {
menu.ShowAsContext(); menu.ShowAsContext();
} }
static void HandleSelect (object val) { static void HandleSelect (object val) {
var pair = (SpineDrawerValuePair)val; var pair = (SpineDrawerValuePair)val;
pair.property.stringValue = pair.str; pair.property.stringValue = pair.str;

View File

@ -68,6 +68,7 @@ public class SkeletonAnimation : SkeletonRenderer, ISkeletonAnimation {
} }
[SerializeField] [SerializeField]
[SpineAnimation]
private String _animationName; private String _animationName;
public String AnimationName { public String AnimationName {