AnimatorComponent.cs 4.4 KB

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