| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- using System.Collections;
- using System.Collections.Generic;
- using Sirenix.OdinInspector;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- using UnityEngine;
- [ExecuteInEditMode]
- public class NameplateItem : MonoBehaviour
- {
- public GameObject targetObj;
- public Transform aimTF;
- public Transform nameplateTF;
- public DrawLinkLine line;
- public Transform aimPoint;
- public Transform nameplatePoint;
- public float waitTime;
- public bool m_Highlight = false;
- private RayCastItem rayCastItem;
- public void SetInitState()
- {
- if (m_Highlight)
- {
- rayCastItem = targetObj.GetComponent<RayCastItem>() == null
- ? targetObj.gameObject.AddComponent<RayCastItem>()
- : targetObj.GetComponent<RayCastItem>();
- }
- }
- public void SetFinishedState()
- {
- if (line != null && aimPoint != null && nameplatePoint != null)
- {
- line.gameObject.SetActive(false);
- aimTF.gameObject.SetActive(false);
- nameplateTF.gameObject.SetActive(false);
- }
- }
- public void DrawLine()
- {
- if (line != null && aimPoint != null && nameplatePoint != null)
- {
- aimTF.transform.position = aimPoint.position;
- aimTF.transform.rotation = aimPoint.rotation;
- nameplateTF.transform.position = nameplatePoint.position;
- nameplateTF.transform.rotation = nameplatePoint.rotation;
- line.startpoint = aimPoint;
- line.endpoint = nameplatePoint;
- }
- if (line != null && aimPoint != null && nameplatePoint != null)
- {
- line.gameObject.SetActive(true);
- aimTF.gameObject.SetActive(true);
- nameplateTF.gameObject.SetActive(true);
- }
- }
- /// <summary>
- /// 设置部件高亮闪烁
- /// </summary>
- /// <param name="state"></param>
- public void SetHighlighterState(bool state)
- {
- if (rayCastItem == null || rayCastItem.HighlighterConstant == state) return;
- if (state)
- {
- rayCastItem.OpenHighlighter(Color.yellow);
- }
- else
- {
- rayCastItem.CloseHighlighter();
- }
- }
- #if UNITY_EDITOR
- [Button("添加瞄准点位置")]
- public void AddAimPoint()
- {
- if (aimTF == null)
- {
- Debug.LogError("未添加瞄准点");
- return;
- }
- if (aimPoint == null)
- {
- aimPoint = new GameObject("AimPoint").transform;
- }
- aimPoint.transform.position = aimTF.position;
- aimPoint.transform.rotation = aimTF.rotation;
- aimPoint.transform.localScale = Vector3.one;
- aimPoint.transform.parent = this.transform;
- aimPoint.transform.SetAsLastSibling();
- Selection.activeGameObject = aimPoint.gameObject;
- if (isShowPose)
- {
- GameObject cloneItem = GameObject.Instantiate(aimTF.gameObject, aimPoint);
- cloneItem.transform.localPosition = Vector3.zero;
- cloneItem.transform.localRotation = Quaternion.identity;
- }
- else
- {
- ShowMovePos();
- }
- }
- [Button("添加铭牌位置")]
- public void AddNameplatePoint()
- {
- if (nameplateTF == null)
- {
- Debug.LogError("未添加铭牌");
- return;
- }
- if (nameplatePoint == null)
- {
- nameplatePoint = new GameObject("NameplatePoint").transform;
- }
- nameplatePoint.transform.position = nameplateTF.position;
- nameplatePoint.transform.rotation = nameplateTF.rotation;
- nameplatePoint.transform.localScale = Vector3.one;
- nameplatePoint.transform.parent = this.transform;
- nameplatePoint.transform.SetAsLastSibling();
- Selection.activeGameObject = nameplatePoint.gameObject;
- if (isShowPose)
- {
- GameObject cloneItem = GameObject.Instantiate(nameplateTF.gameObject, nameplatePoint);
- cloneItem.transform.localPosition = Vector3.zero;
- cloneItem.transform.localRotation = Quaternion.identity;
- }
- else
- {
- ShowMovePos();
- }
- }
- private bool isShowPose = false;
- [Button("展示物体姿态")]
- [ShowIf("@!isShowPose")]
- public void ShowMovePos()
- {
- if (aimPoint != null)
- {
- GameObject cloneItem = GameObject.Instantiate(aimTF.gameObject, aimPoint);
- cloneItem.transform.localPosition = Vector3.zero;
- cloneItem.transform.localRotation = Quaternion.identity;
- cloneItem.SetActive(true);
- }
- if (nameplatePoint != null)
- {
- GameObject cloneItem = GameObject.Instantiate(nameplateTF.gameObject, nameplatePoint);
- cloneItem.transform.localPosition = Vector3.zero;
- cloneItem.transform.localRotation = Quaternion.identity;
- cloneItem.SetActive(true);
- }
-
- isShowPose = true;
- }
- [Button("关闭物体姿态")]
- [ShowIf("@isShowPose")]
- public void CloseMovePos()
- {
- if (aimPoint != null)
- {
- foreach (Transform item in aimPoint)
- {
- DestroyImmediate(item.gameObject);
- }
- }
- if (nameplatePoint != null)
- {
- foreach (Transform item in nameplatePoint)
- {
- DestroyImmediate(item.gameObject);
- }
- }
-
- isShowPose = false;
- }
- #endif
- }
|