DeviceController.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using QFramework;
  5. using Sirenix.OdinInspector;
  6. public class DeviceController : MonoBehaviour
  7. {
  8. public static DeviceController instance;
  9. public string DeviceName;
  10. public Stack<PartMark> m_OperationPartMarkStruct = new Stack<PartMark>();
  11. [HideInInspector]
  12. public ShowState m_ShowState;
  13. private void Awake()
  14. {
  15. instance = this;
  16. ResKit.Init();
  17. UIKit.OpenPanel<PartLabelPanel>();
  18. }
  19. private void Start()
  20. {
  21. DeviceOfPartDataProxy deviceOfPartDataProxy = DAL.Instance.Get<DeviceOfPartDataProxy>();
  22. deviceOfPartDataProxy.ReadStepMsgInfoFromTable(DeviceName);
  23. PartInfo tmpPartInfo = GetDevicePartInfos(transform.GetComponent<PartMark>());
  24. UIKit.OpenPanel<PartListPanel>(new PartListPanelData() { m_PartInfo = tmpPartInfo });
  25. }
  26. private void Update()
  27. {
  28. PartLabelCheck();
  29. ClikPartMarkCheck();
  30. }
  31. /// <summary>
  32. /// 标签部分检测
  33. /// </summary>
  34. private void PartLabelCheck()
  35. {
  36. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  37. RaycastHit hit;
  38. if (Physics.Raycast(ray, out hit))
  39. {
  40. PartMark partMark = GetParentPartMarkWithPartMark(hit.collider.transform);
  41. // 处理射线击中的对象
  42. if (partMark != null)
  43. {
  44. UIKit.OpenPanel<PartLabelPanel>(UILevel.PopUI, new PartLabelPanelData() { partName = partMark.name });
  45. }
  46. }
  47. else
  48. {
  49. UIKit.HidePanel<PartLabelPanel>();
  50. }
  51. }
  52. /// <summary>
  53. /// 点击物体检测
  54. /// </summary>
  55. private void ClikPartMarkCheck()
  56. {
  57. if (Input.GetMouseButtonDown(0))
  58. {
  59. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  60. RaycastHit hit;
  61. if (Physics.Raycast(ray, out hit))
  62. {
  63. PartMark tmpMark = GetParentPartMarkWithPartMark(hit.collider.transform);
  64. if (tmpMark != null)
  65. {
  66. m_OperationPartMarkStruct.Push(tmpMark);
  67. UIKit.GetPanel<PartListPanel>().SetPartItemState(tmpMark, false);
  68. CameraSurround.instance.SetCameraPosition();
  69. }
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// 查找携带PartMark的节点
  75. /// </summary>
  76. /// <param name="traget"></param>
  77. /// <returns></returns>
  78. private PartMark GetParentPartMarkWithPartMark(Transform traget)
  79. {
  80. PartMark tmpMark = traget.GetComponent<PartMark>();
  81. if (tmpMark == null && traget.parent != null)
  82. {
  83. tmpMark = GetParentPartMarkWithPartMark(traget.parent);
  84. }
  85. return tmpMark;
  86. }
  87. public void OnDisable()
  88. {
  89. UIKit.ClosePanel<PartLabelPanel>();
  90. }
  91. public PartInfo GetDevicePartInfos(PartMark partMark)
  92. {
  93. //最父级
  94. PartInfo partInfo = new PartInfo(partMark.transform.name, partMark);
  95. //字级
  96. foreach (var item in partMark.m_ChildPartMarks)
  97. {
  98. partInfo.childPartInfos.Add(GetDevicePartInfos(item));
  99. }
  100. return partInfo;
  101. }
  102. [Button("为所有节点添加标记")]
  103. private void AddPartMark()
  104. {
  105. Transform[] tmpTransforms = GetComponentsInChildren<Transform>();
  106. foreach (var item in tmpTransforms)
  107. {
  108. if (item.GetComponents<PartMark>() == null)
  109. {
  110. item.gameObject.AddComponent<PartMark>();
  111. }
  112. }
  113. }
  114. }