123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using QFramework;
- using Sirenix.OdinInspector;
- public class DeviceController : MonoBehaviour
- {
- public static DeviceController instance;
- public string DeviceName;
- public Stack<PartMark> m_OperationPartMarkStruct = new Stack<PartMark>();
- [HideInInspector]
- public ShowState m_ShowState;
- private void Awake()
- {
- instance = this;
- ResKit.Init();
- UIKit.OpenPanel<PartLabelPanel>();
- }
- private void Start()
- {
- DeviceOfPartDataProxy deviceOfPartDataProxy = DAL.Instance.Get<DeviceOfPartDataProxy>();
- deviceOfPartDataProxy.ReadStepMsgInfoFromTable(DeviceName);
- PartInfo tmpPartInfo = GetDevicePartInfos(transform.GetComponent<PartMark>());
- UIKit.OpenPanel<PartListPanel>(new PartListPanelData() { m_PartInfo = tmpPartInfo });
- }
- private void Update()
- {
- PartLabelCheck();
- ClikPartMarkCheck();
- }
- /// <summary>
- /// 标签部分检测
- /// </summary>
- private void PartLabelCheck()
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit))
- {
- PartMark partMark = GetParentPartMarkWithPartMark(hit.collider.transform);
- // 处理射线击中的对象
- if (partMark != null)
- {
- UIKit.OpenPanel<PartLabelPanel>(UILevel.PopUI, new PartLabelPanelData() { partName = partMark.name });
- }
- }
- else
- {
- UIKit.HidePanel<PartLabelPanel>();
- }
- }
- /// <summary>
- /// 点击物体检测
- /// </summary>
- private void ClikPartMarkCheck()
- {
- if (Input.GetMouseButtonDown(0))
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit))
- {
- PartMark tmpMark = GetParentPartMarkWithPartMark(hit.collider.transform);
- if (tmpMark != null)
- {
- m_OperationPartMarkStruct.Push(tmpMark);
- UIKit.GetPanel<PartListPanel>().SetPartItemState(tmpMark, false);
- CameraSurround.instance.SetCameraPosition();
- }
- }
- }
- }
- /// <summary>
- /// 查找携带PartMark的节点
- /// </summary>
- /// <param name="traget"></param>
- /// <returns></returns>
- private PartMark GetParentPartMarkWithPartMark(Transform traget)
- {
- PartMark tmpMark = traget.GetComponent<PartMark>();
- if (tmpMark == null && traget.parent != null)
- {
- tmpMark = GetParentPartMarkWithPartMark(traget.parent);
- }
- return tmpMark;
- }
- public void OnDisable()
- {
- UIKit.ClosePanel<PartLabelPanel>();
- }
- public PartInfo GetDevicePartInfos(PartMark partMark)
- {
- //最父级
- PartInfo partInfo = new PartInfo(partMark.transform.name, partMark);
- //字级
- foreach (var item in partMark.m_ChildPartMarks)
- {
- partInfo.childPartInfos.Add(GetDevicePartInfos(item));
- }
- return partInfo;
- }
- [Button("为所有节点添加标记")]
- private void AddPartMark()
- {
- Transform[] tmpTransforms = GetComponentsInChildren<Transform>();
- foreach (var item in tmpTransforms)
- {
- if (item.GetComponents<PartMark>() == null)
- {
- item.gameObject.AddComponent<PartMark>();
- }
- }
- }
- }
|