SpineAnimationController.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Diagnostics;
  3. using LitJson;
  4. using UnityEngine;
  5. using UnityEngine.Serialization;
  6. [RequireComponent(typeof(Animation))]
  7. public class SpineAnimationController : BaseBehaviour
  8. {
  9. public float TimeScale
  10. {
  11. get
  12. {
  13. return this._skeletonAnimation.timeScale;
  14. }
  15. }
  16. public string Skin
  17. {
  18. get
  19. {
  20. return this._skeletonAnimation.skeleton.skin.name;
  21. }
  22. set
  23. {
  24. this._skeletonAnimation.skeleton.SetSkin(value);
  25. }
  26. }
  27. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  28. public event EventHandler<SpineAnimationController.EffectArgs> OnAnimChange;
  29. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  30. public event EventHandler<SpineAnimationController.EffectArgs> OnAnimSpeedChange;
  31. private void Awake()
  32. {
  33. this._timeController = base.GetComponent<TimeController>();
  34. SkeletonAnimation skeletonAnimation;
  35. if ((skeletonAnimation = this._skeletonAnimation) == null)
  36. {
  37. skeletonAnimation = (this._skeletonAnimation = base.GetComponent<SkeletonAnimation>());
  38. }
  39. this._skeletonAnimation = skeletonAnimation;
  40. this._animation = base.GetComponent<Animation>();
  41. this.CurrentUnityAnim = string.Empty;
  42. this.CurrentSpineAnim = string.Empty;
  43. this._mappingData = ((!(this._animMapping != null)) ? null : JsonMapper.ToObject(this._animMapping.text));
  44. }
  45. public void Play(string animName, bool loop = false, bool forceChange = false, float animSpeed = 1f)
  46. {
  47. this._lastAnimSpeed = animSpeed;
  48. if (loop && this.CurrentUnityAnim == animName && MathfX.isInMiddleRange(this._lastAnimSpeed, animSpeed, 0.1f))
  49. {
  50. return;
  51. }
  52. try
  53. {
  54. this._resumeScale = animSpeed;
  55. if (SingletonMono<WorldTime>.Instance.IsFrozen)
  56. {
  57. animSpeed = 0f;
  58. if (this._timeController != null)
  59. {
  60. this._timeController.isPause = true;
  61. }
  62. }
  63. this._skeletonAnimation.timeScale = animSpeed;
  64. this._animation[animName].speed = animSpeed;
  65. if (this.OnAnimSpeedChange != null)
  66. {
  67. this.OnAnimSpeedChange(this, new SpineAnimationController.EffectArgs(animSpeed));
  68. }
  69. if (forceChange || this.CurrentUnityAnim != animName)
  70. {
  71. this._animation.Stop();
  72. this._animation[animName].wrapMode = ((!loop) ? WrapMode.Default : WrapMode.Loop);
  73. this._animation.Play(animName, PlayMode.StopAll);
  74. this.CurrentUnityAnim = animName;
  75. animName = ((this._mappingData != null) ? this._mappingData.Get<string>(animName, animName) : animName);
  76. this._skeletonAnimation.state.SetAnimation(0, animName, loop);
  77. this._skeletonAnimation.skeleton.SetToSetupPose();
  78. this._skeletonAnimation.Update(0f);
  79. if (this.OnAnimChange != null)
  80. {
  81. this.OnAnimChange(this, new SpineAnimationController.EffectArgs(animName, loop));
  82. }
  83. this.CurrentSpineAnim = animName;
  84. }
  85. this.AnimLoop = loop;
  86. this.AnimForceChange = forceChange;
  87. }
  88. catch (IndexOutOfRangeException)
  89. {
  90. UnityEngine.Debug.LogError("动画 " + animName + " 不存在");
  91. throw;
  92. }
  93. catch (NullReferenceException)
  94. {
  95. UnityEngine.Debug.LogError("动画 " + animName + " 不存在");
  96. throw;
  97. }
  98. }
  99. public void AddAnim(string animName, bool loop)
  100. {
  101. if (this._animation[animName] == null)
  102. {
  103. UnityEngine.Debug.LogError("动画 " + animName + " 不存在");
  104. return;
  105. }
  106. this._animation[animName].wrapMode = ((!loop) ? WrapMode.Default : WrapMode.Loop);
  107. this._animation.PlayQueued(animName, QueueMode.CompleteOthers);
  108. animName = ((this._mappingData != null) ? this._mappingData.Get<string>(animName, animName) : animName);
  109. this._skeletonAnimation.state.AddAnimation(0, animName, loop, 0f);
  110. this._skeletonAnimation.Update(0.01f);
  111. this.CurrentUnityAnim = animName;
  112. this.AnimLoop = loop;
  113. }
  114. public void Pause()
  115. {
  116. this.ChangeTimeScale(0f);
  117. }
  118. public void Resume()
  119. {
  120. if (Math.Abs(this._resumeScale) < 1.401298E-45f)
  121. {
  122. this._resumeScale = 1f;
  123. }
  124. this.ChangeTimeScale(this._resumeScale);
  125. }
  126. public void Resume(float animSpeed)
  127. {
  128. if (this._animation[this.CurrentUnityAnim] != null)
  129. {
  130. this._animation[this.CurrentUnityAnim].speed = animSpeed;
  131. }
  132. this._skeletonAnimation.timeScale = animSpeed;
  133. }
  134. private void ChangeTimeScale(float animSpeed)
  135. {
  136. if (this._animation[this.CurrentUnityAnim] != null)
  137. {
  138. this._animation[this.CurrentUnityAnim].speed = animSpeed;
  139. }
  140. if (this.OnAnimSpeedChange != null)
  141. {
  142. this.OnAnimSpeedChange(this, new SpineAnimationController.EffectArgs(animSpeed));
  143. }
  144. this._skeletonAnimation.timeScale = animSpeed;
  145. }
  146. public bool IsPlaying(string animName)
  147. {
  148. return this._animation.IsPlaying(animName);
  149. }
  150. public void Play(Enum stateEnum, bool loop = false, bool forceChange = false, float animSpeed = 1f)
  151. {
  152. this.Play(stateEnum.ToString(), loop, forceChange, animSpeed);
  153. }
  154. [SerializeField]
  155. [FormerlySerializedAs("animMapping")]
  156. private TextAsset _animMapping;
  157. private float _lastAnimSpeed;
  158. private Animation _animation;
  159. private JsonData _mappingData;
  160. private float _resumeScale;
  161. [SerializeField]
  162. [FormerlySerializedAs("m_skeletonAnimation")]
  163. private SkeletonAnimation _skeletonAnimation;
  164. private TimeController _timeController;
  165. public bool AnimForceChange;
  166. public bool AnimLoop;
  167. public string CurrentSpineAnim;
  168. public string CurrentUnityAnim;
  169. public class EffectArgs : EventArgs
  170. {
  171. public EffectArgs(string effectName, bool _loop)
  172. {
  173. this.EffectName = effectName;
  174. this.Loop = _loop;
  175. }
  176. public EffectArgs(float _animSpeed)
  177. {
  178. this.AnimSpeed = _animSpeed;
  179. }
  180. public readonly string EffectName;
  181. public readonly bool Loop;
  182. public float AnimSpeed;
  183. }
  184. }