AnimatorComponent.cs 4.2 KB

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