ToolAni_BanShou.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 (isLuoSiRot)
  67. {
  68. if (toolAnchorAngle < maxAngle)
  69. {
  70. timer += Time.deltaTime;
  71. toolAnchorAngle += Mathf.Abs(rotAngle) * Time.deltaTime / duration;
  72. tmpModel.position = Vector3.Lerp(tmpModelAnchor.position, tmpModelAnchor.position + tmpModelAnchor.TransformDirection(localMovePos), timer / duration);
  73. tmpModel.rotation = tmpModelAnchor.rotation;
  74. tmpModel.RotateAround(tmpModelAnchor.position, tmpModelAnchor.TransformDirection(localMovePos), rotAngle * timer / duration);
  75. tmpToolAnchor.position = tmpModel.position;
  76. tmpToolAnchor.rotation = tmpModelAnchor.rotation;
  77. tmpToolAnchor.RotateAround(tmpModelAnchor.position, tmpModelAnchor.TransformDirection(localMovePos), toolAnchorAngle * (rotAngle > 0 ? 1 : -1));
  78. }
  79. else
  80. {
  81. toolHighLerp += Time.deltaTime * 5;
  82. if (toolHighLerp > 1)
  83. {
  84. toolHighLerp = 1;
  85. isLuoSiRot = false;
  86. }
  87. tmpToolAnchor.position = Vector3.Lerp(tmpModel.position, tmpModel.position + tmpModelAnchor.TransformDirection(localMovePos).normalized * moveHigh, toolHighLerp);
  88. }
  89. }
  90. else //扳手回转
  91. {
  92. if (toolAnchorAngle > 0)
  93. {
  94. toolAnchorAngle -= Mathf.Abs(rotAngle) * Time.deltaTime / duration;
  95. tmpToolAnchor.rotation = tmpModelAnchor.transform.rotation;
  96. tmpToolAnchor.RotateAround(tmpModelAnchor.position, tmpModelAnchor.TransformDirection(localMovePos), toolAnchorAngle * (rotAngle > 0 ? 1 : -1));
  97. }
  98. else
  99. {
  100. toolHighLerp -= Time.deltaTime * 5;
  101. if (toolHighLerp < 0)
  102. {
  103. toolHighLerp = 0;
  104. isLuoSiRot = true;
  105. }
  106. tmpToolAnchor.transform.position = Vector3.Lerp(tmpModel.position, tmpModel.position + tmpModelAnchor.TransformDirection(localMovePos).normalized * moveHigh, toolHighLerp);
  107. }
  108. }
  109. if (timer > duration)
  110. {
  111. timer = 0;
  112. tmpModel.position = tmpModelAnchor.position;
  113. tmpModel.rotation = tmpModelAnchor.rotation;
  114. tmpToolAnchor.position = tmpModelAnchor.position;
  115. tmpToolAnchor.rotation = tmpModelAnchor.rotation;
  116. finishedCallBack?.Invoke();
  117. }
  118. }
  119. public override void FinishedState(ToolAnimationItem toolAnimationItem, bool reverse)
  120. {
  121. toolAnimationItem.toolAnchor.gameObject.SetActive(false);
  122. timer = 0;
  123. StopPlay(toolAnimationItem, reverse);
  124. }
  125. public override void InitState(ToolAnimationItem toolAnimationItem, bool reverse)
  126. {
  127. toolAnimationItem.model.SetActive(true);
  128. if (currentModel != null)
  129. {
  130. currentModel.gameObject.SetActive(true);
  131. }
  132. if (currentToolAnchor != null)
  133. {
  134. if (currentToolAnchor == toolAnimationItem.toolAnchor)
  135. {
  136. currentToolAnchor.gameObject.SetActive(false);
  137. }
  138. else
  139. {
  140. toolAnimationItem.Destroy(currentToolAnchor.gameObject);
  141. }
  142. }
  143. }
  144. public override void PlayTrigger(ToolAnimationItem toolAnimationItem, bool reverse)
  145. {
  146. timer = 0;
  147. isLuoSiRot = true;
  148. toolAnchorAngle = 0;
  149. if (Application.isPlaying)
  150. {
  151. if (AnimationManager.Instance.AnimationActiveObj != null)
  152. {
  153. InitTargetToolAnchor(AnimationManager.Instance.AnimationActiveObj.transform.position, toolAnimationItem);
  154. }
  155. }
  156. else
  157. {
  158. #if UNITY_EDITOR
  159. if (Selection.activeGameObject != null)
  160. {
  161. InitTargetToolAnchor(Selection.activeGameObject.transform.position, toolAnimationItem);
  162. }
  163. else
  164. {
  165. InitTargetToolAnchor(toolAnimationItem.modelAnchor.position, toolAnimationItem);
  166. }
  167. #endif
  168. }
  169. }
  170. public override void StopPlay(ToolAnimationItem toolAnimationItem, bool reverse)
  171. {
  172. timer = 0;
  173. if (currentModel != null)
  174. {
  175. currentModel.position = currentModelAnchor.position;
  176. currentModel.rotation = currentModelAnchor.rotation;
  177. }
  178. if (currentToolAnchor != null)
  179. {
  180. currentToolAnchor.position = currentModelAnchor.position;
  181. currentToolAnchor.rotation = currentModelAnchor.rotation;
  182. }
  183. if (currentToolAnchor != toolAnimationItem.toolAnchor)
  184. {
  185. toolAnimationItem.isPlaying = false;
  186. if (currentToolAnchor != null)
  187. {
  188. toolAnimationItem.Destroy(currentToolAnchor.gameObject);
  189. }
  190. if (Application.isPlaying && currentModel != null)
  191. {
  192. currentModel.gameObject.SetActive(false);
  193. }
  194. }
  195. else
  196. {
  197. toolAnimationItem.isPlaying = false;
  198. currentToolAnchor.gameObject.SetActive(false);
  199. if (Application.isPlaying && currentModel != null)
  200. {
  201. currentModel.gameObject.SetActive(false);
  202. }
  203. }
  204. }
  205. /// <summary>
  206. /// 根据当前CheckPos选择的物体最近的锚点执行动画
  207. /// </summary>
  208. /// <param name="toolAnimationItem"></param>
  209. public void InitTargetToolAnchor(Vector3 CheckPos, ToolAnimationItem toolAnimationItem)
  210. {
  211. currentModel = toolAnimationItem.model.transform;
  212. currentModelAnchor = toolAnimationItem.modelAnchor;
  213. Vector3 centerPoint = Vector3.zero;
  214. if (m_SimpleMoveItem != null)
  215. {
  216. foreach (var item in m_SimpleMoveItem.moveObjs)
  217. {
  218. centerPoint += item.Value.position;
  219. if (Vector3.Distance(item.Key.transform.position, currentModel.position) < 0.001f) continue;
  220. if (Vector3.Distance(CheckPos, currentModelAnchor.position) > Vector3.Distance(CheckPos, item.Value.position))
  221. {
  222. currentModelAnchor = item.Value;
  223. currentModel = item.Key.transform;
  224. }
  225. }
  226. centerPoint = centerPoint / m_SimpleMoveItem.moveObjs.Count;
  227. }
  228. if (currentModelAnchor != toolAnimationItem.modelAnchor)
  229. {
  230. currentToolAnchor = GameObject.Instantiate(toolAnimationItem.toolAnchor);
  231. currentToolAnchor.transform.parent = currentModelAnchor;
  232. currentToolAnchor.transform.localPosition = Vector3.zero;
  233. currentToolAnchor.transform.localRotation = Quaternion.identity;
  234. currentToolAnchor.transform.localScale = Vector3.one;
  235. if (m_UseCenterReset)
  236. {
  237. float angle = Vector3.Angle(toolAnimationItem.modelAnchor.position - centerPoint, currentModelAnchor.position - centerPoint);
  238. Vector3 oldDir = currentModelAnchor.InverseTransformDirection(toolAnimationItem.modelAnchor.position - centerPoint);
  239. Vector3 newDir = currentModelAnchor.InverseTransformDirection(currentModelAnchor.position - centerPoint);
  240. float a = Vector3.Dot(Vector3.Cross(oldDir, newDir), localMovePos);
  241. for (int i = 0; i < currentToolAnchor.childCount; i++)
  242. {
  243. currentToolAnchor.GetChild(i).RotateAround(currentModelAnchor.position, currentModelAnchor.TransformDirection(localMovePos), angle * (a > 0 ? 1 : -1));
  244. }
  245. }
  246. }
  247. else
  248. {
  249. currentToolAnchor = toolAnimationItem.toolAnchor;
  250. toolAnimationItem.toolAnchor.gameObject.SetActive(true);
  251. }
  252. currentToolAnchor.gameObject.SetActive(true);
  253. }
  254. #if UNITY_EDITOR
  255. public override void EditorPlayUpdate(ToolAnimationItem toolAnimationItem, bool reverse)
  256. {
  257. if (currentToolAnchor != toolAnimationItem.toolAnchor)
  258. {
  259. PlayBanShouAnimationByFrame(currentModel, currentModelAnchor, currentToolAnchor, () =>
  260. {
  261. toolAnimationItem.isPlaying = false;
  262. if (currentToolAnchor != null)
  263. {
  264. toolAnimationItem.Destroy(currentToolAnchor.gameObject);
  265. }
  266. });
  267. }
  268. else
  269. {
  270. PlayBanShouAnimationByFrame(toolAnimationItem.model.transform, toolAnimationItem.modelAnchor, toolAnimationItem.toolAnchor,
  271. () => { toolAnimationItem.isPlaying = false; });
  272. }
  273. }
  274. public override void OnDrawGizmos(ToolAnimationItem toolAnimationItem)
  275. {
  276. Gizmos.color = Color.yellow;
  277. Gizmos.DrawLine(toolAnimationItem.modelAnchor.position, toolAnimationItem.modelAnchor.position + toolAnimationItem.modelAnchor.TransformDirection(localMovePos));
  278. }
  279. #endif
  280. }