ToolAni_BanShou.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using ChivaXR;
  2. using Sirenix.OdinInspector;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. #if UNITY_EDITOR
  7. using UnityEditor;
  8. #endif
  9. using UnityEngine;
  10. public class ToolAni_BanShou : ToolAnimationBase
  11. {
  12. #region 扳手交互参数
  13. [LabelText("相对移动位置")]
  14. public Vector3 localMovePos = new Vector3(0, 0, 0);
  15. [LabelText("相对旋转角度")]
  16. public float rotAngle = 0;
  17. [LabelText("扳手单轮角度")]
  18. public float maxAngle = 40;
  19. [LabelText("运行时长(不包含工具动画)")]
  20. public float duration = 1;
  21. [LabelText("螺丝抬起高度")]
  22. public float moveHigh = 0.02f;
  23. [LabelText("共享锚点")]
  24. public SimpleMoveItem m_SimpleMoveItem;
  25. [InfoBox("开启后,工具会随所有锚点的中心点进行朝向校准,关闭则使用默认朝向")]
  26. [LabelText("是否开启工具校准")]
  27. public bool m_UseCenterReset = true;
  28. //工具锚点旋转角度
  29. private float toolAnchorAngle = 0;
  30. private float timer = 0;
  31. private bool isLuoSiRot = true;
  32. private float toolHighLerp = 0;
  33. [HideInInspector]
  34. public Transform currentModel;
  35. [HideInInspector]
  36. public Transform currentModelAnchor;
  37. public Transform currentToolAnchor;
  38. #endregion
  39. public override void PlayUpdate(ToolAnimationItem toolAnimationItem, bool reverse)
  40. {
  41. if (currentToolAnchor != toolAnimationItem.toolAnchor)
  42. {
  43. PlayBanShouAnimationByFrame(currentModel, currentModelAnchor, currentToolAnchor, () =>
  44. {
  45. toolAnimationItem.isPlaying = false;
  46. if (currentToolAnchor != null)
  47. {
  48. toolAnimationItem.Destroy(currentToolAnchor.gameObject);
  49. }
  50. currentModel.gameObject.SetActive(false);
  51. });
  52. }
  53. else
  54. {
  55. PlayBanShouAnimationByFrame(toolAnimationItem.model.transform, toolAnimationItem.modelAnchor, toolAnimationItem.toolAnchor,
  56. () =>
  57. {
  58. toolAnimationItem.isPlaying = false;
  59. currentToolAnchor.gameObject.SetActive(false);
  60. currentModel.gameObject.SetActive(false);
  61. });
  62. }
  63. }
  64. public void PlayBanShouAnimationByFrame(Transform tmpModel, Transform tmpModelAnchor, Transform tmpToolAnchor, Action finishedCallBack)
  65. {
  66. if (tmpModel == null || tmpModelAnchor == null) return;
  67. if (isLuoSiRot)
  68. {
  69. if (toolAnchorAngle < maxAngle)
  70. {
  71. timer += Time.deltaTime;
  72. toolAnchorAngle += Mathf.Abs(rotAngle) * Time.deltaTime / duration;
  73. tmpModel.position = Vector3.Lerp(tmpModelAnchor.position, tmpModelAnchor.position + tmpModelAnchor.TransformDirection(localMovePos), timer / duration);
  74. tmpModel.rotation = tmpModelAnchor.rotation;
  75. tmpModel.RotateAround(tmpModelAnchor.position, tmpModelAnchor.TransformDirection(localMovePos), rotAngle * timer / duration);
  76. tmpToolAnchor.position = tmpModel.position;
  77. tmpToolAnchor.rotation = tmpModelAnchor.rotation;
  78. tmpToolAnchor.RotateAround(tmpModelAnchor.position, tmpModelAnchor.TransformDirection(localMovePos), toolAnchorAngle * (rotAngle > 0 ? 1 : -1));
  79. }
  80. else
  81. {
  82. toolHighLerp += Time.deltaTime * 5;
  83. if (toolHighLerp > 1)
  84. {
  85. toolHighLerp = 1;
  86. isLuoSiRot = false;
  87. }
  88. tmpToolAnchor.position = Vector3.Lerp(tmpModel.position, tmpModel.position + tmpModelAnchor.TransformDirection(localMovePos).normalized * moveHigh, toolHighLerp);
  89. }
  90. }
  91. else //扳手回转
  92. {
  93. if (toolAnchorAngle > 0)
  94. {
  95. toolAnchorAngle -= Mathf.Abs(rotAngle) * Time.deltaTime / duration;
  96. tmpToolAnchor.rotation = tmpModelAnchor.transform.rotation;
  97. tmpToolAnchor.RotateAround(tmpModelAnchor.position, tmpModelAnchor.TransformDirection(localMovePos), toolAnchorAngle * (rotAngle > 0 ? 1 : -1));
  98. }
  99. else
  100. {
  101. toolHighLerp -= Time.deltaTime * 5;
  102. if (toolHighLerp < 0)
  103. {
  104. toolHighLerp = 0;
  105. isLuoSiRot = true;
  106. }
  107. tmpToolAnchor.transform.position = Vector3.Lerp(tmpModel.position, tmpModel.position + tmpModelAnchor.TransformDirection(localMovePos).normalized * moveHigh, toolHighLerp);
  108. }
  109. }
  110. if (timer > duration)
  111. {
  112. timer = 0;
  113. tmpModel.position = tmpModelAnchor.position;
  114. tmpModel.rotation = tmpModelAnchor.rotation;
  115. tmpToolAnchor.position = tmpModelAnchor.position;
  116. tmpToolAnchor.rotation = tmpModelAnchor.rotation;
  117. finishedCallBack?.Invoke();
  118. }
  119. }
  120. public override void FinishedState(ToolAnimationItem toolAnimationItem, bool reverse)
  121. {
  122. toolAnimationItem.toolAnchor.gameObject.SetActive(false);
  123. timer = 0;
  124. StopPlay(toolAnimationItem, reverse);
  125. }
  126. public override void InitState(ToolAnimationItem toolAnimationItem, bool reverse)
  127. {
  128. toolAnimationItem.model.SetActive(true);
  129. if (currentModel != null)
  130. {
  131. currentModel.gameObject.SetActive(true);
  132. }
  133. if (currentToolAnchor != null)
  134. {
  135. if (currentToolAnchor == toolAnimationItem.toolAnchor)
  136. {
  137. currentToolAnchor.gameObject.SetActive(false);
  138. }
  139. else
  140. {
  141. toolAnimationItem.Destroy(currentToolAnchor.gameObject);
  142. }
  143. }
  144. }
  145. public override void PlayTrigger(ToolAnimationItem toolAnimationItem, bool reverse)
  146. {
  147. timer = 0;
  148. isLuoSiRot = true;
  149. toolAnchorAngle = 0;
  150. if (Application.isPlaying)
  151. {
  152. if (AnimationManager.Instance.AnimationActiveObj != null)
  153. {
  154. InitTargetToolAnchor(AnimationManager.Instance.AnimationActiveObj.transform.position, toolAnimationItem);
  155. }
  156. }
  157. else
  158. {
  159. #if UNITY_EDITOR
  160. if (Selection.activeGameObject != null)
  161. {
  162. InitTargetToolAnchor(Selection.activeGameObject.transform.position, toolAnimationItem);
  163. }
  164. else
  165. {
  166. InitTargetToolAnchor(toolAnimationItem.modelAnchor.position, toolAnimationItem);
  167. }
  168. #endif
  169. }
  170. }
  171. public override void StopPlay(ToolAnimationItem toolAnimationItem, bool reverse)
  172. {
  173. timer = 0;
  174. if (currentModel != null)
  175. {
  176. currentModel.position = currentModelAnchor.position;
  177. currentModel.rotation = currentModelAnchor.rotation;
  178. }
  179. if (currentToolAnchor != null)
  180. {
  181. currentToolAnchor.position = currentModelAnchor.position;
  182. currentToolAnchor.rotation = currentModelAnchor.rotation;
  183. }
  184. if (currentToolAnchor != toolAnimationItem.toolAnchor)
  185. {
  186. toolAnimationItem.isPlaying = false;
  187. if (currentToolAnchor != null)
  188. {
  189. toolAnimationItem.Destroy(currentToolAnchor.gameObject);
  190. }
  191. if (Application.isPlaying && currentModel != null)
  192. {
  193. currentModel.gameObject.SetActive(false);
  194. }
  195. }
  196. else
  197. {
  198. toolAnimationItem.isPlaying = false;
  199. currentToolAnchor.gameObject.SetActive(false);
  200. if (Application.isPlaying && currentModel != null)
  201. {
  202. currentModel.gameObject.SetActive(false);
  203. }
  204. }
  205. }
  206. /// <summary>
  207. /// 根据当前CheckPos选择的物体最近的锚点执行动画
  208. /// </summary>
  209. /// <param name="toolAnimationItem"></param>
  210. public void InitTargetToolAnchor(Vector3 CheckPos, ToolAnimationItem toolAnimationItem)
  211. {
  212. currentModel = toolAnimationItem.model.transform;
  213. currentModelAnchor = toolAnimationItem.modelAnchor;
  214. Vector3 centerPoint = Vector3.zero;
  215. if (m_SimpleMoveItem != null)
  216. {
  217. foreach (var item in m_SimpleMoveItem.moveObjs)
  218. {
  219. centerPoint += item.Value.position;
  220. if (Vector3.Distance(item.Key.transform.position, currentModel.position) < 0.001f) continue;
  221. if (Vector3.Distance(CheckPos, currentModelAnchor.position) > Vector3.Distance(CheckPos, item.Value.position))
  222. {
  223. currentModelAnchor = item.Value;
  224. currentModel = item.Key.transform;
  225. }
  226. }
  227. centerPoint = centerPoint / m_SimpleMoveItem.moveObjs.Count;
  228. }
  229. if (currentModelAnchor != toolAnimationItem.modelAnchor)
  230. {
  231. currentToolAnchor = GameObject.Instantiate(toolAnimationItem.toolAnchor);
  232. currentToolAnchor.transform.parent = currentModelAnchor;
  233. currentToolAnchor.transform.localPosition = Vector3.zero;
  234. currentToolAnchor.transform.localRotation = Quaternion.identity;
  235. currentToolAnchor.transform.localScale = Vector3.one;
  236. if (m_UseCenterReset)
  237. {
  238. float angle = Vector3.Angle(toolAnimationItem.modelAnchor.position - centerPoint, currentModelAnchor.position - centerPoint);
  239. Vector3 oldDir = currentModelAnchor.InverseTransformDirection(toolAnimationItem.modelAnchor.position - centerPoint);
  240. Vector3 newDir = currentModelAnchor.InverseTransformDirection(currentModelAnchor.position - centerPoint);
  241. float a = Vector3.Dot(Vector3.Cross(oldDir, newDir), localMovePos);
  242. for (int i = 0; i < currentToolAnchor.childCount; i++)
  243. {
  244. currentToolAnchor.GetChild(i).RotateAround(currentModelAnchor.position, currentModelAnchor.TransformDirection(localMovePos), angle * (a > 0 ? 1 : -1));
  245. }
  246. }
  247. }
  248. else
  249. {
  250. currentToolAnchor = toolAnimationItem.toolAnchor;
  251. toolAnimationItem.toolAnchor.gameObject.SetActive(true);
  252. }
  253. currentToolAnchor.gameObject.SetActive(true);
  254. }
  255. #if UNITY_EDITOR
  256. public override void EditorPlayUpdate(ToolAnimationItem toolAnimationItem, bool reverse)
  257. {
  258. if (currentToolAnchor != toolAnimationItem.toolAnchor)
  259. {
  260. PlayBanShouAnimationByFrame(currentModel, currentModelAnchor, currentToolAnchor, () =>
  261. {
  262. toolAnimationItem.isPlaying = false;
  263. if (currentToolAnchor != null)
  264. {
  265. toolAnimationItem.Destroy(currentToolAnchor.gameObject);
  266. }
  267. });
  268. }
  269. else
  270. {
  271. PlayBanShouAnimationByFrame(toolAnimationItem.model.transform, toolAnimationItem.modelAnchor, toolAnimationItem.toolAnchor,
  272. () => { toolAnimationItem.isPlaying = false; });
  273. }
  274. }
  275. public override void OnDrawGizmos(ToolAnimationItem toolAnimationItem)
  276. {
  277. Gizmos.color = Color.yellow;
  278. Gizmos.DrawLine(toolAnimationItem.modelAnchor.position, toolAnimationItem.modelAnchor.position + toolAnimationItem.modelAnchor.TransformDirection(localMovePos));
  279. }
  280. #endif
  281. }