NameplateItem.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Sirenix.OdinInspector;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. using UnityEngine;
  8. [ExecuteInEditMode]
  9. public class NameplateItem : MonoBehaviour
  10. {
  11. public GameObject targetObj;
  12. public Transform aimTF;
  13. public Transform nameplateTF;
  14. public DrawLinkLine line;
  15. public Transform aimPoint;
  16. public Transform nameplatePoint;
  17. public float waitTime;
  18. public bool m_Highlight = false;
  19. private RayCastItem rayCastItem;
  20. public void SetInitState()
  21. {
  22. if (m_Highlight)
  23. {
  24. rayCastItem = targetObj.GetComponent<RayCastItem>() == null
  25. ? targetObj.gameObject.AddComponent<RayCastItem>()
  26. : targetObj.GetComponent<RayCastItem>();
  27. }
  28. }
  29. public void SetFinishedState()
  30. {
  31. if (line != null && aimPoint != null && nameplatePoint != null)
  32. {
  33. line.gameObject.SetActive(false);
  34. aimTF.gameObject.SetActive(false);
  35. nameplateTF.gameObject.SetActive(false);
  36. }
  37. }
  38. public void DrawLine()
  39. {
  40. if (line != null && aimPoint != null && nameplatePoint != null)
  41. {
  42. aimTF.transform.position = aimPoint.position;
  43. aimTF.transform.rotation = aimPoint.rotation;
  44. nameplateTF.transform.position = nameplatePoint.position;
  45. nameplateTF.transform.rotation = nameplatePoint.rotation;
  46. line.startpoint = aimPoint;
  47. line.endpoint = nameplatePoint;
  48. }
  49. if (line != null && aimPoint != null && nameplatePoint != null)
  50. {
  51. line.gameObject.SetActive(true);
  52. aimTF.gameObject.SetActive(true);
  53. nameplateTF.gameObject.SetActive(true);
  54. }
  55. }
  56. /// <summary>
  57. /// 设置部件高亮闪烁
  58. /// </summary>
  59. /// <param name="state"></param>
  60. public void SetHighlighterState(bool state)
  61. {
  62. if (rayCastItem == null || rayCastItem.HighlighterConstant == state) return;
  63. if (state)
  64. {
  65. rayCastItem.OpenHighlighter(Color.yellow);
  66. }
  67. else
  68. {
  69. rayCastItem.CloseHighlighter();
  70. }
  71. }
  72. #if UNITY_EDITOR
  73. [Button("添加瞄准点位置")]
  74. public void AddAimPoint()
  75. {
  76. if (aimTF == null)
  77. {
  78. Debug.LogError("未添加瞄准点");
  79. return;
  80. }
  81. if (aimPoint == null)
  82. {
  83. aimPoint = new GameObject("AimPoint").transform;
  84. }
  85. aimPoint.transform.position = aimTF.position;
  86. aimPoint.transform.rotation = aimTF.rotation;
  87. aimPoint.transform.localScale = Vector3.one;
  88. aimPoint.transform.parent = this.transform;
  89. aimPoint.transform.SetAsLastSibling();
  90. Selection.activeGameObject = aimPoint.gameObject;
  91. if (isShowPose)
  92. {
  93. GameObject cloneItem = GameObject.Instantiate(aimTF.gameObject, aimPoint);
  94. cloneItem.transform.localPosition = Vector3.zero;
  95. cloneItem.transform.localRotation = Quaternion.identity;
  96. }
  97. else
  98. {
  99. ShowMovePos();
  100. }
  101. }
  102. [Button("添加铭牌位置")]
  103. public void AddNameplatePoint()
  104. {
  105. if (nameplateTF == null)
  106. {
  107. Debug.LogError("未添加铭牌");
  108. return;
  109. }
  110. if (nameplatePoint == null)
  111. {
  112. nameplatePoint = new GameObject("NameplatePoint").transform;
  113. }
  114. nameplatePoint.transform.position = nameplateTF.position;
  115. nameplatePoint.transform.rotation = nameplateTF.rotation;
  116. nameplatePoint.transform.localScale = Vector3.one;
  117. nameplatePoint.transform.parent = this.transform;
  118. nameplatePoint.transform.SetAsLastSibling();
  119. Selection.activeGameObject = nameplatePoint.gameObject;
  120. if (isShowPose)
  121. {
  122. GameObject cloneItem = GameObject.Instantiate(nameplateTF.gameObject, nameplatePoint);
  123. cloneItem.transform.localPosition = Vector3.zero;
  124. cloneItem.transform.localRotation = Quaternion.identity;
  125. }
  126. else
  127. {
  128. ShowMovePos();
  129. }
  130. }
  131. private bool isShowPose = false;
  132. [Button("展示物体姿态")]
  133. [ShowIf("@!isShowPose")]
  134. public void ShowMovePos()
  135. {
  136. if (aimPoint != null)
  137. {
  138. GameObject cloneItem = GameObject.Instantiate(aimTF.gameObject, aimPoint);
  139. cloneItem.transform.localPosition = Vector3.zero;
  140. cloneItem.transform.localRotation = Quaternion.identity;
  141. cloneItem.SetActive(true);
  142. }
  143. if (nameplatePoint != null)
  144. {
  145. GameObject cloneItem = GameObject.Instantiate(nameplateTF.gameObject, nameplatePoint);
  146. cloneItem.transform.localPosition = Vector3.zero;
  147. cloneItem.transform.localRotation = Quaternion.identity;
  148. cloneItem.SetActive(true);
  149. }
  150. isShowPose = true;
  151. }
  152. [Button("关闭物体姿态")]
  153. [ShowIf("@isShowPose")]
  154. public void CloseMovePos()
  155. {
  156. if (aimPoint != null)
  157. {
  158. foreach (Transform item in aimPoint)
  159. {
  160. DestroyImmediate(item.gameObject);
  161. }
  162. }
  163. if (nameplatePoint != null)
  164. {
  165. foreach (Transform item in nameplatePoint)
  166. {
  167. DestroyImmediate(item.gameObject);
  168. }
  169. }
  170. isShowPose = false;
  171. }
  172. #endif
  173. }