AnimatorComponentSystem.cs 4.6 KB

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