AnimatorComponent.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using Cysharp.Threading.Tasks;
  2. using DG.Tweening;
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.Events;
  7. namespace WS
  8. {
  9. [ObjectSystem]
  10. public class AnimatorComponentAwakeSystem : AwakeSystem<AnimatorComponent, Animator, Animator>
  11. {
  12. public override void Awake(AnimatorComponent self, Animator ani, Animator answerAni)
  13. {
  14. self.Awake(ani, answerAni);
  15. }
  16. }
  17. [ObjectSystem]
  18. public class SingleAnimatorComponentAwakeSystem : AwakeSystem<AnimatorComponent, Animator>
  19. {
  20. public override void Awake(AnimatorComponent self, Animator ani)
  21. {
  22. self.Awake(ani);
  23. }
  24. }
  25. [ObjectSystem]
  26. public class AnimatorComponentUpdateSystem : UpdateSystem<AnimatorComponent>
  27. {
  28. public override void Update(AnimatorComponent self)
  29. {
  30. self.Update();
  31. }
  32. }
  33. public class AnimatorComponent : WSComponent
  34. {
  35. ///<summary></summary>
  36. private string objName;
  37. ///<summary>动画组件</summary>
  38. private Animator Anim;
  39. ///<summary>关联物体动画组件</summary>
  40. private Animator AnswerAnim;
  41. /////<summary>关联物体动画组件</summary>
  42. //private List<Animator> answerAnimList;
  43. ///<summary>回调</summary>
  44. private UnityAction OnCallback;
  45. ///<summary>动画剪辑名称</summary>
  46. private string clipName=null;
  47. ///<summary>动画剪辑名称</summary>
  48. private string answerClipName=null;
  49. private List<AnswerObjData> answerObjList= new List<AnswerObjData>();
  50. ///<summary>并行播放</summary>
  51. private bool IsParallel;
  52. ///<summary>播放</summary>
  53. private bool isplay;
  54. public void Awake(Animator anim)
  55. {
  56. Anim = anim;
  57. }
  58. public void Awake(Animator anim, Animator answerAnim)
  59. {
  60. Anim = anim;
  61. AnswerAnim = answerAnim;
  62. }
  63. public void InitInteractAnim(string interactObjName, string animName)
  64. {
  65. objName = interactObjName;
  66. clipName = animName;
  67. Anim = GameObject.Find(objName).GetComponent<Animator>();
  68. }
  69. public void InitAnswerAnim(List<AnswerObjData> answerList)
  70. {
  71. answerObjList = answerList;
  72. }
  73. /// <summary>播放动画</summary>
  74. public void PlaySync(Animator animator, string animName)
  75. {
  76. if (!animator.gameObject.activeSelf)
  77. {
  78. Log.LogError($"{animator.gameObject.name}未启用");
  79. return;
  80. }
  81. animator.Play(animName);
  82. animator.speed = 1;
  83. }
  84. /// <summary>播放动画</summary>
  85. public async UniTask PlayAsync(Animator animator, string animName)
  86. {
  87. if (!animator.gameObject.activeSelf)
  88. {
  89. Log.LogError($"{animator.gameObject.name}未启用");
  90. return;
  91. }
  92. animator.Play(animName);
  93. animator.speed = 1;
  94. var infos = animator.runtimeAnimatorController.animationClips;
  95. for (int j = 0; j < infos.Length; j++)
  96. {
  97. var info = infos[j];
  98. if (info.name == animName)
  99. {
  100. await TimerComponent.Instance.WaitAsync(info.length);
  101. return;
  102. }
  103. }
  104. }
  105. /// <summary>播放关联动画</summary>
  106. /// <param name="name">名称</param>
  107. /// <param name="action">回调</param>
  108. public async UniTask PlayAnimSerial(UnityAction action)
  109. {
  110. try
  111. {
  112. if (Anim != null)
  113. {
  114. await PlayAsync(Anim, clipName);
  115. SceneDataSaveComponent.Instance.AddSceneData(SceneComponent.Instance.ThisScene, objName, clipName);
  116. }
  117. for (int i = 0; i < answerObjList.Count; i++)
  118. {
  119. string answerName = answerObjList[i].AnswerName;
  120. string animName = answerObjList[i].AnswerAnimName;
  121. Animator animator = GameObject.Find(answerName).GetComponent<Animator>();
  122. await PlayAsync(animator, animName);
  123. //Debug.Log(i + "****" + answerObjList.Count);
  124. //Debug.Log(answerObjList[i].AnswerAnimName);
  125. SceneDataSaveComponent.Instance.AddSceneData(SceneComponent.Instance.ThisScene, answerName, animName);
  126. }
  127. }
  128. catch (Exception e)
  129. {
  130. Debug.LogException(e);
  131. }
  132. action?.Invoke();
  133. }
  134. /// <summary>同时播放动画</summary>
  135. public async void PlayAnimParallel(UnityAction action)
  136. {
  137. float time = 0;
  138. if (Anim != null)
  139. {
  140. PlaySync(Anim, clipName);
  141. var clips = Anim.runtimeAnimatorController.animationClips;
  142. for (int j = 0; j < clips.Length; j++)
  143. {
  144. var info = clips[j];
  145. if (info.name == clipName && info.length > time)
  146. {
  147. time = info.length;
  148. }
  149. }
  150. SceneDataSaveComponent.Instance.AddSceneData(SceneComponent.Instance.ThisScene, objName, clipName);
  151. }
  152. for (int i = 0; i < answerObjList.Count; i++)
  153. {
  154. Animator animator = GameObject.Find(answerObjList[i].AnswerName).GetComponent<Animator>();
  155. string animName = answerObjList[i].AnswerAnimName;
  156. PlaySync(animator, animName);
  157. var clips = animator.runtimeAnimatorController.animationClips;
  158. for (int j = 0; j < clips.Length; j++)
  159. {
  160. var info = clips[j];
  161. if (info.name == animName && info.length > time)
  162. {
  163. time = info.length;
  164. }
  165. }
  166. SceneDataSaveComponent.Instance.AddSceneData(SceneComponent.Instance.ThisScene, answerObjList[i].AnswerName, answerObjList[i].AnswerAnimName);
  167. }
  168. await TimerComponent.Instance.WaitAsync(time);
  169. action?.Invoke();
  170. }
  171. /// <summary>播放动画</summary>
  172. /// <param name="name">名称</param>
  173. /// <param name="action">回调</param>
  174. public void PlayAnim(string name, UnityAction action)
  175. {
  176. clipName = name;
  177. Anim.Play(name);
  178. Anim.speed = 1;
  179. OnCallback = action;
  180. isplay = true;
  181. }
  182. /// <summary>同时播放动画</summary>
  183. /// <param name="name">交互物体动画</param>
  184. /// <param name="answerAni">响应物体动画</param>
  185. /// <param name="isParallel">是否并行播放动画</param>
  186. /// <param name="action">回调</param>
  187. public void PlayAnim(string name, string answerAni, bool isParallel, UnityAction action)
  188. {
  189. IsParallel = isParallel;
  190. clipName = name;
  191. answerClipName = answerAni;
  192. Anim.Play(name);
  193. Anim.speed = 1;
  194. if (isParallel)
  195. {
  196. AnswerAnim.Play(answerAni);
  197. AnswerAnim.speed = 1;
  198. }
  199. OnCallback = action;
  200. isplay = true;
  201. }
  202. public void Update()
  203. {
  204. if (Anim != null)
  205. {
  206. if (isplay)
  207. {
  208. AnimatorStateInfo stateInfo = Anim.GetCurrentAnimatorStateInfo(0);
  209. if (stateInfo.IsName(clipName))
  210. {
  211. if (stateInfo.normalizedTime >= 1f)
  212. {
  213. if (!IsParallel && AnswerAnim != null)
  214. {
  215. AnimatorStateInfo stateInfoAnswer = AnswerAnim.GetCurrentAnimatorStateInfo(0);
  216. if (!stateInfoAnswer.IsName(answerClipName))
  217. {
  218. AnswerAnim.Play(answerClipName);
  219. AnswerAnim.speed = 1;
  220. }
  221. if (stateInfoAnswer.IsName(answerClipName))
  222. {
  223. if (stateInfoAnswer.normalizedTime == 0f)
  224. {
  225. AnswerAnim.Play(answerClipName);
  226. AnswerAnim.speed = 1;
  227. }
  228. if (stateInfoAnswer.normalizedTime >= 1f)
  229. {
  230. OnCallback?.Invoke();
  231. isplay = false;
  232. }
  233. }
  234. }
  235. else
  236. {
  237. OnCallback?.Invoke();
  238. isplay = false;
  239. }
  240. }
  241. }
  242. }
  243. }
  244. }
  245. public override void Dispose()
  246. {
  247. base.Dispose();
  248. Anim = null;
  249. answerObjList.Clear();
  250. AnswerAnim = null;
  251. OnCallback = null;
  252. }
  253. }
  254. }