SkeletonAnimator.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /******************************************************************************
  2. * Spine Runtimes Software License
  3. * Version 2.1
  4. *
  5. * Copyright (c) 2013, Esoteric Software
  6. * All rights reserved.
  7. *
  8. * You are granted a perpetual, non-exclusive, non-sublicensable and
  9. * non-transferable license to install, execute and perform the Spine Runtimes
  10. * Software (the "Software") solely for internal use. Without the written
  11. * permission of Esoteric Software (typically granted by licensing Spine), you
  12. * may not (a) modify, translate, adapt or otherwise create derivative works,
  13. * improvements of the Software or develop new applications using the Software
  14. * or (b) remove, delete, alter or obscure any trademarks or any copyright,
  15. * trademark, patent or other intellectual property or proprietary rights
  16. * notices on or in the Software, including any copy thereof. Redistributions
  17. * in binary or source form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. /*****************************************************************************
  31. * SkeletonAnimator created by Mitch Thompson
  32. * Full irrevocable rights and permissions granted to Esoteric Software
  33. *****************************************************************************/
  34. using UnityEngine;
  35. using System.Collections;
  36. using System.Collections.Generic;
  37. using Spine;
  38. [RequireComponent(typeof(Animator))]
  39. public class SkeletonAnimator : SkeletonRenderer, ISkeletonAnimation
  40. {
  41. public enum MixMode { AlwaysMix, MixNext, SpineStyle }
  42. public MixMode[] layerMixModes = new MixMode[0];
  43. public event UpdateBonesDelegate UpdateLocal
  44. {
  45. add { _UpdateLocal += value; }
  46. remove { _UpdateLocal -= value; }
  47. }
  48. public event UpdateBonesDelegate UpdateWorld
  49. {
  50. add { _UpdateWorld += value; }
  51. remove { _UpdateWorld -= value; }
  52. }
  53. public event UpdateBonesDelegate UpdateComplete
  54. {
  55. add { _UpdateComplete += value; }
  56. remove { _UpdateComplete -= value; }
  57. }
  58. protected event UpdateBonesDelegate _UpdateLocal;
  59. protected event UpdateBonesDelegate _UpdateWorld;
  60. protected event UpdateBonesDelegate _UpdateComplete;
  61. Dictionary<string, Spine.Animation> animationTable = new Dictionary<string, Spine.Animation>();
  62. Animator animator;
  63. public override void Reset()
  64. {
  65. base.Reset();
  66. if (!valid)
  67. return;
  68. animationTable.Clear();
  69. var data = skeletonDataAsset.GetSkeletonData(true);
  70. foreach (var a in data.Animations)
  71. {
  72. animationTable.Add(a.Name, a);
  73. }
  74. animator = GetComponent<Animator>();
  75. }
  76. void Update()
  77. {
  78. if (!valid)
  79. return;
  80. if (layerMixModes.Length != animator.layerCount)
  81. {
  82. System.Array.Resize<MixMode>(ref layerMixModes, animator.layerCount);
  83. }
  84. skeleton.Update(Time.deltaTime);
  85. //apply
  86. int layerCount = animator.layerCount;
  87. float deltaTime = Time.deltaTime;
  88. for (int i = 0; i < layerCount; i++)
  89. {
  90. float layerWeight = animator.GetLayerWeight(i);
  91. if (i == 0)
  92. layerWeight = 1;
  93. var stateInfo = animator.GetCurrentAnimatorStateInfo(i);
  94. var nextStateInfo = animator.GetNextAnimatorStateInfo(i);
  95. #if UNITY_5
  96. var clipInfo = animator.GetCurrentAnimatorClipInfo(i);
  97. var nextClipInfo = animator.GetNextAnimatorClipInfo(i);
  98. #else
  99. var clipInfo = animator.GetCurrentAnimatorClipInfo(i);
  100. var nextClipInfo = animator.GetNextAnimatorClipInfo(i);
  101. #endif
  102. MixMode mode = layerMixModes[i];
  103. if (mode == MixMode.AlwaysMix)
  104. {
  105. //always use Mix instead of Applying the first non-zero weighted clip
  106. foreach (AnimatorClipInfo info in clipInfo)
  107. {
  108. float weight = info.weight * layerWeight;
  109. if (weight == 0)
  110. continue;
  111. float time = stateInfo.normalizedTime * info.clip.length;
  112. animationTable[info.clip.name].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, stateInfo.loop, null, weight);
  113. }
  114. foreach (AnimatorClipInfo info in nextClipInfo)
  115. {
  116. float weight = info.weight * layerWeight;
  117. if (weight == 0)
  118. continue;
  119. float time = nextStateInfo.normalizedTime * info.clip.length;
  120. animationTable[info.clip.name].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, nextStateInfo.loop, null, weight);
  121. }
  122. }
  123. else if (mode >= MixMode.MixNext)
  124. {
  125. //apply first non-zero weighted clip
  126. int c = 0;
  127. for (; c < clipInfo.Length; c++)
  128. {
  129. AnimatorClipInfo info = clipInfo[c];
  130. float weight = info.weight * layerWeight;
  131. if (weight == 0)
  132. continue;
  133. float time = stateInfo.normalizedTime * info.clip.length;
  134. animationTable[info.clip.name].Apply(skeleton, Mathf.Max(0, time - deltaTime), time, stateInfo.loop, null);
  135. break;
  136. }
  137. //mix the rest
  138. for (; c < clipInfo.Length; c++)
  139. {
  140. AnimatorClipInfo info = clipInfo[c];
  141. float weight = info.weight * layerWeight;
  142. if (weight == 0)
  143. continue;
  144. float time = stateInfo.normalizedTime * info.clip.length;
  145. animationTable[info.clip.name].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, stateInfo.loop, null, weight);
  146. }
  147. c = 0;
  148. //apply next clip directly instead of mixing (ie: no crossfade, ignores mecanim transition weights)
  149. if (mode == MixMode.SpineStyle)
  150. {
  151. for (; c < nextClipInfo.Length; c++)
  152. {
  153. AnimatorClipInfo info = nextClipInfo[c];
  154. float weight = info.weight * layerWeight;
  155. if (weight == 0)
  156. continue;
  157. float time = nextStateInfo.normalizedTime * info.clip.length;
  158. animationTable[info.clip.name].Apply(skeleton, Mathf.Max(0, time - deltaTime), time, nextStateInfo.loop, null);
  159. break;
  160. }
  161. }
  162. //mix the rest
  163. for (; c < nextClipInfo.Length; c++)
  164. {
  165. AnimatorClipInfo info = nextClipInfo[c];
  166. float weight = info.weight * layerWeight;
  167. if (weight == 0)
  168. continue;
  169. float time = nextStateInfo.normalizedTime * info.clip.length;
  170. animationTable[info.clip.name].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, nextStateInfo.loop, null, weight);
  171. }
  172. }
  173. }
  174. if (_UpdateLocal != null)
  175. _UpdateLocal(this);
  176. skeleton.UpdateWorldTransform();
  177. if (_UpdateWorld != null)
  178. {
  179. _UpdateWorld(this);
  180. skeleton.UpdateWorldTransform();
  181. }
  182. if (_UpdateComplete != null)
  183. {
  184. _UpdateComplete(this);
  185. }
  186. }
  187. }