AnimatorComponentSystem.cs 4.7 KB

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