瀏覽代碼

Opt-in C# events for SkeletonAnimator.

John 9 年之前
父節點
當前提交
dba0245753
共有 1 個文件被更改,包括 29 次插入13 次删除
  1. 29 13
      spine-unity/Assets/spine-unity/SkeletonAnimator.cs

+ 29 - 13
spine-unity/Assets/spine-unity/SkeletonAnimator.cs

@@ -1,9 +1,9 @@
-
-
 /*****************************************************************************
  * SkeletonAnimator created by Mitch Thompson
  * Full irrevocable rights and permissions granted to Esoteric Software
 *****************************************************************************/
+//#define USE_SPINE_EVENTS // Uncomment this define to use C# events to handle Spine events. (Does not disable Unity AnimationClip Events)
+
 using UnityEngine;
 using System.Collections.Generic;
 
@@ -33,17 +33,21 @@ namespace Spine.Unity {
 		protected event UpdateBonesDelegate _UpdateWorld;
 		protected event UpdateBonesDelegate _UpdateComplete;
 
-		public Skeleton Skeleton {
-			get {
-				return this.skeleton;
-			}
-		}
+		public Skeleton Skeleton { get { return this.skeleton; } }
 
 		readonly Dictionary<int, Spine.Animation> animationTable = new Dictionary<int, Spine.Animation>();
 		readonly Dictionary<AnimationClip, int> clipNameHashCodeTable = new Dictionary<AnimationClip, int>();
 		Animator animator;
 		float lastTime;
 
+		#if USE_SPINE_EVENTS
+		public delegate void SkeletonAnimatorEventDelegate (Spine.Event firedEvent);
+		public event SkeletonAnimatorEventDelegate AnimationEvent;
+		public readonly ExposedList<Spine.Event> events = new ExposedList<Spine.Event>();
+		#else
+		public readonly ExposedList<Spine.Event> events = null;
+		#endif
+
 		public override void Initialize (bool overwrite) {
 			if (valid && !overwrite)
 				return;
@@ -108,7 +112,7 @@ namespace Spine.Unity {
 							continue;
 
 						float time = stateInfo.normalizedTime * info.clip.length;
-						animationTable[GetAnimationClipNameHashCode(info.clip)].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, stateInfo.loop, null, weight);
+						animationTable[GetAnimationClipNameHashCode(info.clip)].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, stateInfo.loop, events, weight);
 					}
 					#if UNITY_5
 					if (nextStateInfo.fullPathHash != 0) {
@@ -122,7 +126,7 @@ namespace Spine.Unity {
 								continue;
 
 							float time = nextStateInfo.normalizedTime * info.clip.length;
-							animationTable[GetAnimationClipNameHashCode(info.clip)].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, nextStateInfo.loop, null, weight);
+							animationTable[GetAnimationClipNameHashCode(info.clip)].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, nextStateInfo.loop, events, weight);
 						}
 					}
 				} else if (mode >= MixMode.MixNext) {
@@ -136,7 +140,7 @@ namespace Spine.Unity {
 							continue;
 
 						float time = stateInfo.normalizedTime * info.clip.length;
-						animationTable[GetAnimationClipNameHashCode(info.clip)].Apply(skeleton, Mathf.Max(0, time - deltaTime), time, stateInfo.loop, null);
+						animationTable[GetAnimationClipNameHashCode(info.clip)].Apply(skeleton, Mathf.Max(0, time - deltaTime), time, stateInfo.loop, events);
 						break;
 					}
 
@@ -148,7 +152,7 @@ namespace Spine.Unity {
 							continue;
 
 						float time = stateInfo.normalizedTime * info.clip.length;
-						animationTable[GetAnimationClipNameHashCode(info.clip)].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, stateInfo.loop, null, weight);
+						animationTable[GetAnimationClipNameHashCode(info.clip)].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, stateInfo.loop, events, weight);
 					}
 
 					c = 0;
@@ -166,7 +170,7 @@ namespace Spine.Unity {
 									continue;
 
 								float time = nextStateInfo.normalizedTime * info.clip.length;
-								animationTable[GetAnimationClipNameHashCode(info.clip)].Apply(skeleton, Mathf.Max(0, time - deltaTime), time, nextStateInfo.loop, null);
+								animationTable[GetAnimationClipNameHashCode(info.clip)].Apply(skeleton, Mathf.Max(0, time - deltaTime), time, nextStateInfo.loop, events);
 								break;
 							}
 						}
@@ -179,12 +183,24 @@ namespace Spine.Unity {
 								continue;
 
 							float time = nextStateInfo.normalizedTime * info.clip.length;
-							animationTable[GetAnimationClipNameHashCode(info.clip)].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, nextStateInfo.loop, null, weight);
+							animationTable[GetAnimationClipNameHashCode(info.clip)].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, nextStateInfo.loop, events, weight);
 						}
 					}
 				}
 			}
 
+			#if USE_SPINE_EVENTS
+			int eventsCount = events.Count;
+			if (eventsCount > 0) {
+				var eventsItems = events.Items;
+				for (int i = 0; i < eventsCount; i++) {
+					if (AnimationEvent != null)
+						AnimationEvent(eventsItems[i]);
+				}
+				events.Clear(false);
+			}
+			#endif
+
 			if (_UpdateLocal != null)
 				_UpdateLocal(this);