AnimatorComponentSystem.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using UnityEngine;
  3. namespace ET
  4. {
  5. public class AnimatorComponentAwakeSystem : AwakeSystem<AnimatorComponent>
  6. {
  7. public override void Awake(AnimatorComponent self)
  8. {
  9. self.Awake();
  10. }
  11. }
  12. public class AnimatorComponentUpdateSystem : UpdateSystem<AnimatorComponent>
  13. {
  14. public override void Update(AnimatorComponent self)
  15. {
  16. self.Update();
  17. }
  18. }
  19. public class AnimatorComponentDestroySystem : DestroySystem<AnimatorComponent>
  20. {
  21. public override void Destroy(AnimatorComponent self)
  22. {
  23. self.animationClips = null;
  24. self.Parameter = null;
  25. self.Animator = null;
  26. }
  27. }
  28. public static class AnimatorComponentSystem
  29. {
  30. public static void Awake(this AnimatorComponent self)
  31. {
  32. Animator animator = self.GetParent<Unit>().GameObject.GetComponent<Animator>();
  33. if (animator == null)
  34. {
  35. return;
  36. }
  37. if (animator.runtimeAnimatorController == null)
  38. {
  39. return;
  40. }
  41. if (animator.runtimeAnimatorController.animationClips == null)
  42. {
  43. return;
  44. }
  45. self.Animator = animator;
  46. foreach (AnimationClip animationClip in animator.runtimeAnimatorController.animationClips)
  47. {
  48. self.animationClips[animationClip.name] = animationClip;
  49. }
  50. foreach (AnimatorControllerParameter animatorControllerParameter in animator.parameters)
  51. {
  52. self.Parameter.Add(animatorControllerParameter.name);
  53. }
  54. }
  55. public static void Update(this AnimatorComponent self)
  56. {
  57. if (self.isStop)
  58. {
  59. return;
  60. }
  61. if (self.MotionType == MotionType.None)
  62. {
  63. return;
  64. }
  65. try
  66. {
  67. self.Animator.SetFloat("MotionSpeed", self.MontionSpeed);
  68. self.Animator.SetTrigger(self.MotionType.ToString());
  69. self.MontionSpeed = 1;
  70. self.MotionType = MotionType.None;
  71. }
  72. catch (Exception ex)
  73. {
  74. throw new Exception($"动作播放失败: {self.MotionType}", ex);
  75. }
  76. }
  77. public static bool HasParameter(this AnimatorComponent self, string parameter)
  78. {
  79. return self.Parameter.Contains(parameter);
  80. }
  81. public static void PlayInTime(this AnimatorComponent self, MotionType motionType, float time)
  82. {
  83. AnimationClip animationClip;
  84. if (!self.animationClips.TryGetValue(motionType.ToString(), out animationClip))
  85. {
  86. throw new Exception($"找不到该动作: {motionType}");
  87. }
  88. float motionSpeed = animationClip.length / time;
  89. if (motionSpeed < 0.01f || motionSpeed > 1000f)
  90. {
  91. Log.Error($"motionSpeed数值异常, {motionSpeed}, 此动作跳过");
  92. return;
  93. }
  94. self.MotionType = motionType;
  95. self.MontionSpeed = motionSpeed;
  96. }
  97. public static void Play(this AnimatorComponent self, MotionType motionType, float motionSpeed = 1f)
  98. {
  99. if (!self.HasParameter(motionType.ToString()))
  100. {
  101. return;
  102. }
  103. self.MotionType = motionType;
  104. self.MontionSpeed = motionSpeed;
  105. }
  106. public static float AnimationTime(this AnimatorComponent self, MotionType motionType)
  107. {
  108. AnimationClip animationClip;
  109. if (!self.animationClips.TryGetValue(motionType.ToString(), out animationClip))
  110. {
  111. throw new Exception($"找不到该动作: {motionType}");
  112. }
  113. return animationClip.length;
  114. }
  115. public static void PauseAnimator(this AnimatorComponent self)
  116. {
  117. if (self.isStop)
  118. {
  119. return;
  120. }
  121. self.isStop = true;
  122. if (self.Animator == null)
  123. {
  124. return;
  125. }
  126. self.stopSpeed = self.Animator.speed;
  127. self.Animator.speed = 0;
  128. }
  129. public static void RunAnimator(this AnimatorComponent self)
  130. {
  131. if (!self.isStop)
  132. {
  133. return;
  134. }
  135. self.isStop = false;
  136. if (self.Animator == null)
  137. {
  138. return;
  139. }
  140. self.Animator.speed = self.stopSpeed;
  141. }
  142. public static void SetBoolValue(this AnimatorComponent self, string name, bool state)
  143. {
  144. if (!self.HasParameter(name))
  145. {
  146. return;
  147. }
  148. self.Animator.SetBool(name, state);
  149. }
  150. public static void SetFloatValue(this AnimatorComponent self, string name, float state)
  151. {
  152. if (!self.HasParameter(name))
  153. {
  154. return;
  155. }
  156. self.Animator.SetFloat(name, state);
  157. }
  158. public static void SetIntValue(this AnimatorComponent self, string name, int value)
  159. {
  160. if (!self.HasParameter(name))
  161. {
  162. return;
  163. }
  164. self.Animator.SetInteger(name, value);
  165. }
  166. public static void SetTrigger(this AnimatorComponent self, string name)
  167. {
  168. if (!self.HasParameter(name))
  169. {
  170. return;
  171. }
  172. self.Animator.SetTrigger(name);
  173. }
  174. public static void SetAnimatorSpeed(this AnimatorComponent self, float speed)
  175. {
  176. self.stopSpeed = self.Animator.speed;
  177. self.Animator.speed = speed;
  178. }
  179. public static void ResetAnimatorSpeed(this AnimatorComponent self)
  180. {
  181. self.Animator.speed = self.stopSpeed;
  182. }
  183. }
  184. }