MultiSpineAnimationController.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using LitJson;
  5. using UnityEngine;
  6. public class MultiSpineAnimationController : BaseBehaviour
  7. {
  8. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  9. public event EventHandler<MultiSpineAnimationController.EffectArgs> OnAnimChange;
  10. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  11. public event EventHandler<MultiSpineAnimationController.EffectArgs> OnAnimSpeedChange;
  12. public float timeScale
  13. {
  14. get
  15. {
  16. return this.SwitchSkeletonByWeaponType(this.currentSkeleton).timeScale;
  17. }
  18. }
  19. private void Awake()
  20. {
  21. this.timeController = base.GetComponent<PlayerTimeController>();
  22. this._skeletonMeshRenderers = new List<MeshRenderer>
  23. {
  24. this.atkSkeleton.GetComponent<MeshRenderer>(),
  25. this.restAtkSkeleton.GetComponent<MeshRenderer>(),
  26. this.normalSkeleton.GetComponent<MeshRenderer>(),
  27. this.hurtSkeleton.GetComponent<MeshRenderer>(),
  28. this.upRisingkeleton.GetComponent<MeshRenderer>(),
  29. this.heavyAtkkeleton.GetComponent<MeshRenderer>()
  30. };
  31. this.m_animation = base.GetComponent<Animation>();
  32. this.currentAnim = string.Empty;
  33. this.animData = JsonMapper.ToObject(this.animMapping.text);
  34. }
  35. public void Play(string animName, PlayerAction.SkeletonType skeletonType, bool loop = false, bool forceChange = false, float animSpeed = 1f)
  36. {
  37. if (this.m_animation.GetClip(animName) == null)
  38. {
  39. UnityEngine.Debug.LogError("动画 " + animName + " 不存在");
  40. return;
  41. }
  42. this.pauseTimeScale = animSpeed;
  43. animSpeed = ((!this.timeController.isPause) ? animSpeed : 0f);
  44. this.SwitchSkeletonByWeaponType(skeletonType).timeScale = animSpeed;
  45. this.m_animation[animName].speed = animSpeed;
  46. if (this.OnAnimSpeedChange != null)
  47. {
  48. this.OnAnimSpeedChange(this, new MultiSpineAnimationController.EffectArgs(animSpeed));
  49. }
  50. if (forceChange)
  51. {
  52. this.m_animation.Stop();
  53. this.m_animation[animName].wrapMode = ((!loop) ? WrapMode.Default : WrapMode.Loop);
  54. if (this.OnAnimChange != null)
  55. {
  56. this.OnAnimChange(this, new MultiSpineAnimationController.EffectArgs(this.animData.Get<string>(animName, animName), loop));
  57. }
  58. this.m_animation.Play(animName, PlayMode.StopAll);
  59. this.SwitchSkeletonByWeaponType(skeletonType).state.SetAnimation(0, this.animData.Get<string>(animName, animName), loop);
  60. this.SwitchSkeletonByWeaponType(skeletonType).skeleton.SetToSetupPose();
  61. this.SwitchSkeletonByWeaponType(skeletonType).Update(0f);
  62. this.currentAnim = animName;
  63. }
  64. else if (this.currentAnim != animName)
  65. {
  66. this.m_animation.Stop();
  67. this.m_animation[animName].wrapMode = ((!loop) ? WrapMode.Default : WrapMode.Loop);
  68. this.m_animation.Play(animName, PlayMode.StopAll);
  69. if (this.OnAnimChange != null)
  70. {
  71. this.OnAnimChange(this, new MultiSpineAnimationController.EffectArgs(this.animData.Get<string>(animName, animName), loop));
  72. }
  73. this.SwitchSkeletonByWeaponType(skeletonType).state.SetAnimation(0, this.animData.Get<string>(animName, animName), loop);
  74. this.SwitchSkeletonByWeaponType(skeletonType).skeleton.SetToSetupPose();
  75. this.SwitchSkeletonByWeaponType(skeletonType).Update(0f);
  76. this.currentAnim = animName;
  77. }
  78. this.animLoop = loop;
  79. this.animForceChange = forceChange;
  80. }
  81. public void AddAnim(string animName, PlayerAction.SkeletonType weaponType, bool loop)
  82. {
  83. if (this.m_animation[animName] == null)
  84. {
  85. UnityEngine.Debug.LogError("动画 " + animName + " 不存在");
  86. return;
  87. }
  88. this.m_animation[animName].wrapMode = ((!loop) ? WrapMode.Default : WrapMode.Loop);
  89. this.m_animation.PlayQueued(animName, QueueMode.CompleteOthers);
  90. this.SwitchSkeletonByWeaponType(weaponType).state.AddAnimation(0, this.animData.Get<string>(animName, animName), loop, 0f);
  91. this.SwitchSkeletonByWeaponType(weaponType).Update(0.01f);
  92. this.currentAnim = animName;
  93. this.animLoop = loop;
  94. }
  95. public void Pause()
  96. {
  97. if (this.m_animation[this.currentAnim] != null)
  98. {
  99. this.m_animation[this.currentAnim].speed = 0f;
  100. }
  101. this.SwitchSkeletonByWeaponType(this.currentSkeleton).timeScale = 0f;
  102. }
  103. public void Resume()
  104. {
  105. if (this.pauseTimeScale == 0f)
  106. {
  107. this.pauseTimeScale = 1f;
  108. }
  109. if (this.m_animation[this.currentAnim] != null)
  110. {
  111. this.m_animation[this.currentAnim].speed = this.pauseTimeScale;
  112. }
  113. this.SwitchSkeletonByWeaponType(this.currentSkeleton).timeScale = this.pauseTimeScale;
  114. }
  115. public void Resume(float animSpeed)
  116. {
  117. if (this.m_animation[this.currentAnim] != null)
  118. {
  119. this.m_animation[this.currentAnim].speed = animSpeed;
  120. }
  121. this.SwitchSkeletonByWeaponType(this.currentSkeleton).timeScale = animSpeed;
  122. }
  123. public void ChangeTimeScale(float animSpeed)
  124. {
  125. if (this.m_animation[this.currentAnim] != null)
  126. {
  127. this.m_animation[this.currentAnim].speed = animSpeed;
  128. }
  129. if (this.OnAnimSpeedChange != null)
  130. {
  131. this.OnAnimSpeedChange(this, new MultiSpineAnimationController.EffectArgs(animSpeed));
  132. }
  133. this.SwitchSkeletonByWeaponType(this.currentSkeleton).timeScale = animSpeed;
  134. }
  135. public float GetAnimNormalizedTime()
  136. {
  137. return this.m_animation[this.currentAnim].normalizedTime;
  138. }
  139. public float GetAnimLength()
  140. {
  141. return this.m_animation[this.currentAnim].length;
  142. }
  143. public string GetCurrentAnimName()
  144. {
  145. return this.m_animation[this.currentAnim].name;
  146. }
  147. private SkeletonAnimation SwitchSkeletonByWeaponType(PlayerAction.SkeletonType skeleton)
  148. {
  149. this.currentSkeleton = skeleton;
  150. switch (skeleton)
  151. {
  152. case PlayerAction.SkeletonType.Normal:
  153. this.HideSkeleton(2);
  154. return this.normalSkeleton;
  155. case PlayerAction.SkeletonType.Attack:
  156. this.HideSkeleton(0);
  157. return this.atkSkeleton;
  158. case PlayerAction.SkeletonType.SpAttack:
  159. this.HideSkeleton(1);
  160. return this.restAtkSkeleton;
  161. case PlayerAction.SkeletonType.Hurt:
  162. this.HideSkeleton(3);
  163. return this.hurtSkeleton;
  164. case PlayerAction.SkeletonType.UpRising:
  165. this.HideSkeleton(4);
  166. return this.upRisingkeleton;
  167. case PlayerAction.SkeletonType.HeavyAttack:
  168. this.HideSkeleton(5);
  169. return this.heavyAtkkeleton;
  170. default:
  171. return null;
  172. }
  173. }
  174. private void HideSkeleton(int index)
  175. {
  176. for (int i = 0; i < this._skeletonMeshRenderers.Count; i++)
  177. {
  178. this._skeletonMeshRenderers[i].enabled = (i == index);
  179. }
  180. }
  181. private float pauseTimeScale;
  182. private PlayerTimeController timeController;
  183. private List<MeshRenderer> _skeletonMeshRenderers;
  184. private Animation m_animation;
  185. private PlayerAction.SkeletonType currentSkeleton;
  186. private JsonData animData;
  187. [SerializeField]
  188. private TextAsset animMapping;
  189. [SerializeField]
  190. private SkeletonAnimation atkSkeleton;
  191. [SerializeField]
  192. private SkeletonAnimation restAtkSkeleton;
  193. [SerializeField]
  194. private SkeletonAnimation normalSkeleton;
  195. [SerializeField]
  196. private SkeletonAnimation hurtSkeleton;
  197. [SerializeField]
  198. private SkeletonAnimation upRisingkeleton;
  199. [SerializeField]
  200. private SkeletonAnimation heavyAtkkeleton;
  201. public string currentAnim;
  202. private bool animLoop;
  203. private bool animForceChange;
  204. public class EffectArgs : EventArgs
  205. {
  206. public EffectArgs(string _effectName, bool _loop)
  207. {
  208. this.effectName = _effectName;
  209. this.loop = _loop;
  210. }
  211. public EffectArgs(float _animSpeed)
  212. {
  213. this.animSpeed = _animSpeed;
  214. }
  215. public string effectName;
  216. public bool loop;
  217. public float animSpeed;
  218. }
  219. }