浏览代码

Fixed setting animation name without looping.

NathanSweet 12 年之前
父节点
当前提交
52696c2607

+ 5 - 5
spine-tk2d/Assets/Spine/Editor/SkeletonAnimationInspector.cs

@@ -42,7 +42,7 @@ public class SkeletonAnimationInspector : Editor {
 
 	void OnEnable () {
 		skeletonDataAsset = serializedObject.FindProperty("skeletonDataAsset");
-		animationName = serializedObject.FindProperty("animationName");
+		animationName = serializedObject.FindProperty("_animationName");
 		loop = serializedObject.FindProperty("loop");
 		useAnimationName = serializedObject.FindProperty("useAnimationName");
 		initialSkinName = serializedObject.FindProperty("initialSkinName");
@@ -53,7 +53,7 @@ public class SkeletonAnimationInspector : Editor {
 
 	override public void OnInspectorGUI () {
 		serializedObject.Update();
-		SkeletonComponent component = (SkeletonComponent)target;
+		SkeletonAnimation component = (SkeletonAnimation)target;
 
 		EditorGUIUtility.LookLikeInspector();
 		EditorGUILayout.PropertyField(skeletonDataAsset);
@@ -98,13 +98,13 @@ public class SkeletonAnimationInspector : Editor {
 			EditorGUILayout.EndHorizontal();
 
 			if (animationIndex == 0) {
-				animationName.stringValue = null;
+				component.animationName = null;
 				useAnimationName.boolValue = false;
 			} else if (animationIndex == 1) {
-				animationName.stringValue = null;
+				component.animationName = null;
 				useAnimationName.boolValue = true;
 			} else {
-				animationName.stringValue = animations[animationIndex];
+				component.animationName = animations[animationIndex];
 				useAnimationName.boolValue = true;
 			}
 		}

+ 19 - 16
spine-tk2d/Assets/Spine/SkeletonAnimation.cs

@@ -40,31 +40,34 @@ using Spine;
 [ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
 public class SkeletonAnimation : SkeletonComponent {
 	public bool useAnimationName;
-	public String animationName;
 	public bool loop;
 	public Spine.AnimationState state;
-
+	
+	public String _animationName;
+	public String animationName {
+		get {
+			TrackEntry entry = state.GetCurrent(0);
+			return entry == null ? null : entry.Animation.Name;
+		}
+		set {
+			if (!useAnimationName) return;
+			if (_animationName == value) return;
+			_animationName = value;
+			if (value == null)
+				state.ClearTrack(0);
+			else
+				state.SetAnimation(0, value, loop);
+		}
+	}
+	
 	override public void Initialize () {
 		base.Initialize(); // Call overridden method to initialize the skeleton.
 		
 		state = new Spine.AnimationState(skeletonDataAsset.GetAnimationStateData());
+		if (_animationName != null) state.SetAnimation(0, _animationName, loop);
 	}
 
 	override public void UpdateSkeleton () {
-		if (useAnimationName) {
-			// Keep AnimationState in sync with animationName and loop fields.
-			TrackEntry entry = state.GetCurrent(0);
-			if (animationName == null || animationName.Length == 0) {
-				if (entry != null && entry.Animation != null)
-					state.ClearTrack(0);
-			} else if (entry == null || entry.Animation == null || animationName != entry.Animation.Name) {
-				Spine.Animation animation = skeleton.Data.FindAnimation(animationName);
-				if (animation != null)
-					state.SetAnimation(0, animation, loop);
-			} else if (entry != null)
-				entry.Loop = loop;
-		}
-		
 		// Apply the animation.
 		state.Update(Time.deltaTime * timeScale);
 		state.Apply(skeleton);

+ 5 - 5
spine-unity/Assets/Spine/Editor/SkeletonAnimationInspector.cs

@@ -42,7 +42,7 @@ public class SkeletonAnimationInspector : Editor {
 
 	void OnEnable () {
 		skeletonDataAsset = serializedObject.FindProperty("skeletonDataAsset");
-		animationName = serializedObject.FindProperty("animationName");
+		animationName = serializedObject.FindProperty("_animationName");
 		loop = serializedObject.FindProperty("loop");
 		useAnimationName = serializedObject.FindProperty("useAnimationName");
 		initialSkinName = serializedObject.FindProperty("initialSkinName");
@@ -53,7 +53,7 @@ public class SkeletonAnimationInspector : Editor {
 
 	override public void OnInspectorGUI () {
 		serializedObject.Update();
-		SkeletonComponent component = (SkeletonComponent)target;
+		SkeletonAnimation component = (SkeletonAnimation)target;
 
 		EditorGUIUtility.LookLikeInspector();
 		EditorGUILayout.PropertyField(skeletonDataAsset);
@@ -98,13 +98,13 @@ public class SkeletonAnimationInspector : Editor {
 			EditorGUILayout.EndHorizontal();
 
 			if (animationIndex == 0) {
-				animationName.stringValue = null;
+				component.animationName = null;
 				useAnimationName.boolValue = false;
 			} else if (animationIndex == 1) {
-				animationName.stringValue = null;
+				component.animationName = null;
 				useAnimationName.boolValue = true;
 			} else {
-				animationName.stringValue = animations[animationIndex];
+				component.animationName = animations[animationIndex];
 				useAnimationName.boolValue = true;
 			}
 		}

+ 18 - 15
spine-unity/Assets/Spine/SkeletonAnimation.cs

@@ -40,31 +40,34 @@ using Spine;
 [ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
 public class SkeletonAnimation : SkeletonComponent {
 	public bool useAnimationName;
-	public String animationName;
 	public bool loop;
 	public Spine.AnimationState state;
 
+	public String _animationName;
+	public String animationName {
+		get {
+			TrackEntry entry = state.GetCurrent(0);
+			return entry == null ? null : entry.Animation.Name;
+		}
+		set {
+			if (!useAnimationName) return;
+			if (_animationName == value) return;
+			_animationName = value;
+			if (value == null)
+				state.ClearTrack(0);
+			else
+				state.SetAnimation(0, value, loop);
+		}
+	}
+
 	override public void Initialize () {
 		base.Initialize(); // Call overridden method to initialize the skeleton.
 		
 		state = new Spine.AnimationState(skeletonDataAsset.GetAnimationStateData());
+		if (_animationName != null) state.SetAnimation(0, _animationName, loop);
 	}
 
 	override public void UpdateSkeleton () {
-		if (useAnimationName) {
-			// Keep AnimationState in sync with animationName and loop fields.
-			TrackEntry entry = state.GetCurrent(0);
-			if (animationName == null || animationName.Length == 0) {
-				if (entry != null && entry.Animation != null)
-					state.ClearTrack(0);
-			} else if (entry == null || entry.Animation == null || animationName != entry.Animation.Name) {
-				Spine.Animation animation = skeleton.Data.FindAnimation(animationName);
-				if (animation != null)
-					state.SetAnimation(0, animation, loop);
-			} else if (entry != null)
-				entry.Loop = loop;
-		}
-		
 		// Apply the animation.
 		state.Update(Time.deltaTime * timeScale);
 		state.Apply(skeleton);