[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 {
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] = "<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;
}
}
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(animationName);
wasAnimationNameChanged |= EditorGUI.EndChangeCheck();
EditorGUILayout.PropertyField(loop);
EditorGUILayout.PropertyField(timeScale);
component.timeScale = Math.Max(component.timeScale, 0);

View File

@ -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<T> : 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<T> : 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<T> : 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<SpineSlot> {
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<SpineSkin> {
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<SpineSkin> {
public class SpineAnimationDrawer : SpineTreeItemDrawerBase<SpineAnimation> {
protected override void PopulateMenu (GenericMenu menu, SerializedProperty property, SpineAnimation targetAttribute, SkeletonData data) {
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++) {
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<SpineEvent> {
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<SpineBone> {
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;

View File

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