diff --git a/spine-unity/Assets/spine-unity/SkeletonAnimator.cs b/spine-unity/Assets/spine-unity/SkeletonAnimator.cs index 5a8600907..d2ccd63e7 100644 --- a/spine-unity/Assets/spine-unity/SkeletonAnimator.cs +++ b/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 animationTable = new Dictionary(); readonly Dictionary clipNameHashCodeTable = new Dictionary(); Animator animator; float lastTime; + #if USE_SPINE_EVENTS + public delegate void SkeletonAnimatorEventDelegate (Spine.Event firedEvent, float weight); + public event SkeletonAnimatorEventDelegate AnimationEvent; + public readonly ExposedList events = new ExposedList(); + #else + public readonly ExposedList events = null; + #endif + public override void Initialize (bool overwrite) { if (valid && !overwrite) return; @@ -108,7 +112,10 @@ 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 USE_SPINE_EVENTS + FireEvents(events, weight, this.AnimationEvent); + #endif } #if UNITY_5 if (nextStateInfo.fullPathHash != 0) { @@ -122,7 +129,10 @@ 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 + FireEvents(events, weight, this.AnimationEvent); + #endif } } } else if (mode >= MixMode.MixNext) { @@ -136,7 +146,10 @@ 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); + #if USE_SPINE_EVENTS + FireEvents(events, weight, this.AnimationEvent); + #endif break; } @@ -148,7 +161,10 @@ 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 USE_SPINE_EVENTS + FireEvents(events, weight, this.AnimationEvent); + #endif } c = 0; @@ -166,7 +182,10 @@ 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); + #if USE_SPINE_EVENTS + FireEvents(events, weight, this.AnimationEvent); + #endif break; } } @@ -179,7 +198,10 @@ 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 + FireEvents(events, weight, this.AnimationEvent); + #endif } } } @@ -211,5 +233,20 @@ namespace Spine.Unity { return clipNameHashCode; } + + #if USE_SPINE_EVENTS + static void FireEvents (ExposedList eventList, float weight, SkeletonAnimatorEventDelegate callback) { + int eventsCount = eventList.Count; + if (eventsCount > 0) { + var eventListItems = eventList.Items; + for (int i = 0; i < eventsCount; i++) { + if (callback != null) + callback(eventListItems[i], weight); + } + + eventList.Clear(false); + } + } + #endif } }