From cab5f211759452ce7e839db82f98d7307c918df2 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 20 May 2017 10:25:12 +0800 Subject: [PATCH] [unity] Allow negative Animator state speed (#889) --- .../Assets/spine-unity/SkeletonAnimator.cs | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/spine-unity/Assets/spine-unity/SkeletonAnimator.cs b/spine-unity/Assets/spine-unity/SkeletonAnimator.cs index 22d29613c..c71984456 100644 --- a/spine-unity/Assets/spine-unity/SkeletonAnimator.cs +++ b/spine-unity/Assets/spine-unity/SkeletonAnimator.cs @@ -88,7 +88,7 @@ namespace Spine.Unity { if (layerMixModes.Length < animator.layerCount) System.Array.Resize(ref layerMixModes, animator.layerCount); - + //skeleton.Update(Time.deltaTime); // Doesn't actually do anything, currently. (Spine 3.5). // Clear Previous @@ -132,22 +132,22 @@ namespace Spine.Unity { bool hasNext = nextStateInfo.fullPathHash != 0; AnimatorClipInfo[] clipInfo = animator.GetCurrentAnimatorClipInfo(layer); AnimatorClipInfo[] nextClipInfo = animator.GetNextAnimatorClipInfo(layer); -// UNITY 4 -// bool hasNext = nextStateInfo.nameHash != 0; -// var clipInfo = animator.GetCurrentAnimationClipState(i); -// var nextClipInfo = animator.GetNextAnimationClipState(i); + //UNITY 4 + //bool hasNext = nextStateInfo.nameHash != 0; + //var clipInfo = animator.GetCurrentAnimationClipState(i); + //var nextClipInfo = animator.GetNextAnimationClipState(i); MixMode mode = layerMixModes[layer]; if (mode == MixMode.AlwaysMix) { // Always use Mix instead of Applying the first non-zero weighted clip. for (int c = 0; c < clipInfo.Length; c++) { var info = clipInfo[c]; float weight = info.weight * layerWeight; if (weight == 0) continue; - animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, AnimationTime(stateInfo.normalizedTime, info.clip.length, stateInfo.loop), stateInfo.loop, null, weight, false, false); + animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, AnimationTime(stateInfo.normalizedTime, info.clip.length, stateInfo.loop, stateInfo.speed <0), stateInfo.loop, null, weight, false, false); } if (hasNext) { for (int c = 0; c < nextClipInfo.Length; c++) { var info = nextClipInfo[c]; float weight = info.weight * layerWeight; if (weight == 0) continue; - animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, nextStateInfo.normalizedTime * info.clip.length, nextStateInfo.loop, null, weight, false, false); + animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, AnimationTime(nextStateInfo.normalizedTime , info.clip.length,nextStateInfo.speed < 0), nextStateInfo.loop, null, weight, false, false); } } } else { // case MixNext || SpineStyle @@ -155,13 +155,13 @@ namespace Spine.Unity { int c = 0; for (; c < clipInfo.Length; c++) { var info = clipInfo[c]; float weight = info.weight * layerWeight; if (weight == 0) continue; - animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, AnimationTime(stateInfo.normalizedTime, info.clip.length, stateInfo.loop), stateInfo.loop, null, 1f, false, false); + animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, AnimationTime(stateInfo.normalizedTime, info.clip.length, stateInfo.loop, stateInfo.speed <0), stateInfo.loop, null, 1f, false, false); break; } // Mix the rest for (; c < clipInfo.Length; c++) { var info = clipInfo[c]; float weight = info.weight * layerWeight; if (weight == 0) continue; - animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, AnimationTime(stateInfo.normalizedTime, info.clip.length, stateInfo.loop), stateInfo.loop, null, weight, false, false); + animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, AnimationTime(stateInfo.normalizedTime, info.clip.length, stateInfo.loop, stateInfo.speed <0), stateInfo.loop, null, weight, false, false); } c = 0; @@ -170,14 +170,14 @@ namespace Spine.Unity { if (mode == MixMode.SpineStyle) { for (; c < nextClipInfo.Length; c++) { var info = nextClipInfo[c]; float weight = info.weight * layerWeight; if (weight == 0) continue; - animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, nextStateInfo.normalizedTime * info.clip.length, nextStateInfo.loop, null, 1f, false, false); + animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, AnimationTime(nextStateInfo.normalizedTime , info.clip.length,nextStateInfo.speed < 0), nextStateInfo.loop, null, 1f, false, false); break; } } // Mix the rest for (; c < nextClipInfo.Length; c++) { var info = nextClipInfo[c]; float weight = info.weight * layerWeight; if (weight == 0) continue; - animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, nextStateInfo.normalizedTime * info.clip.length, nextStateInfo.loop, null, weight, false, false); + animationTable[NameHashCode(info.clip)].Apply(skeleton, 0, AnimationTime(nextStateInfo.normalizedTime , info.clip.length,nextStateInfo.speed < 0), nextStateInfo.loop, null, weight, false, false); } } } @@ -200,13 +200,22 @@ namespace Spine.Unity { } } - static float AnimationTime (float normalizedTime, float clipLength, bool loop) { + static float AnimationTime (float normalizedTime, float clipLength, bool loop, bool reversed) { + if (reversed) + normalizedTime = (1-normalizedTime + (int)normalizedTime) + (int)normalizedTime; float time = normalizedTime * clipLength; if (loop) return time; const float EndSnapEpsilon = 1f/30f; // Workaround for end-duration keys not being applied. return (clipLength - time < EndSnapEpsilon) ? clipLength : time; // return a time snapped to clipLength; } + static float AnimationTime (float normalizedTime, float clipLength, bool reversed) { + if (reversed) + normalizedTime = (1-normalizedTime + (int)normalizedTime) + (int)normalizedTime; + + return normalizedTime * clipLength; + } + int NameHashCode (AnimationClip clip) { int clipNameHashCode; if (!clipNameHashCodeTable.TryGetValue(clip, out clipNameHashCode)) {