AnimatorComponent.cs 4.4 KB

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