DeviceController.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using QFramework;
  5. using Sirenix.OdinInspector;
  6. using System.IO;
  7. public class DeviceController : MonoBehaviour
  8. {
  9. public static DeviceController instance;
  10. public string DeviceName;
  11. public Stack<PartMark> m_OperationPartMarkStruct = new Stack<PartMark>();
  12. [HideInInspector]
  13. public ShowState m_ShowState;
  14. public PartMark m_CurrentSelectPartItem;
  15. public bool ShowCustomPanel = false;
  16. public bool ifShowCoustom = true;
  17. private void Awake()
  18. {
  19. instance = this;
  20. ResKit.Init();
  21. UIKit.OpenPanel<PartLabelPanel>();
  22. }
  23. private void Start()
  24. {
  25. DeviceOfPartDataProxy deviceOfPartDataProxy = DAL.Instance.Get<DeviceOfPartDataProxy>();
  26. deviceOfPartDataProxy.ReadStepMsgInfoFromTable(DeviceName);
  27. deviceOfPartDataProxy.ReadCustomInfoFromTable(DeviceName);
  28. deviceOfPartDataProxy.CoverTableInfos();
  29. deviceOfPartDataProxy.endTableEditInfos(true);
  30. PartInfo tmpPartInfo = GetDevicePartInfos(transform.GetComponent<PartMark>());
  31. UIKit.OpenPanel<PartListPanel>(new PartListPanelData() { m_PartInfo = tmpPartInfo });
  32. }
  33. private void Update()
  34. {
  35. if (ifShowCoustom)
  36. {
  37. PartLabelCheck();
  38. ClikPartMarkCheck();
  39. }
  40. if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
  41. {
  42. if (Input.GetKeyDown(KeyCode.F4))
  43. {
  44. if (ShowCustomPanel)
  45. UIKit.ClosePanel<CustomPartPanel>();
  46. else
  47. UIKit.OpenPanel<CustomPartPanel>();
  48. ShowCustomPanel = !ShowCustomPanel;
  49. }
  50. }
  51. }
  52. /// <summary>
  53. /// 标签部分检测
  54. /// </summary>
  55. private void PartLabelCheck()
  56. {
  57. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  58. RaycastHit hit;
  59. if (Physics.Raycast(ray, out hit))
  60. {
  61. PartMark partMark = GetParentPartMarkWithPartMark(hit.collider.transform);
  62. if (m_CurrentSelectPartItem != partMark)
  63. {
  64. if (m_CurrentSelectPartItem != null)
  65. {
  66. m_CurrentSelectPartItem.CloseHighlighter();
  67. }
  68. m_CurrentSelectPartItem = partMark;
  69. m_CurrentSelectPartItem.OpenHighlighter();
  70. }
  71. // 处理射线击中的对象
  72. if (partMark != null)
  73. {
  74. UIKit.OpenPanel<PartLabelPanel>(UILevel.PopUI, new PartLabelPanelData() { partName = partMark.name });
  75. }
  76. }
  77. else
  78. {
  79. if (m_CurrentSelectPartItem != null)
  80. {
  81. m_CurrentSelectPartItem.CloseHighlighter();
  82. m_CurrentSelectPartItem = null;
  83. }
  84. UIKit.HidePanel<PartLabelPanel>();
  85. }
  86. }
  87. /// <summary>
  88. /// 点击物体检测
  89. /// </summary>
  90. private void ClikPartMarkCheck()
  91. {
  92. if (Input.GetMouseButtonDown(0))
  93. {
  94. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  95. RaycastHit hit;
  96. if (Physics.Raycast(ray, out hit))
  97. {
  98. PartMark tmpMark = GetParentPartMarkWithPartMark(hit.collider.transform);
  99. if (tmpMark != null)
  100. {
  101. m_OperationPartMarkStruct.Push(tmpMark);
  102. UIKit.GetPanel<PartListPanel>().SetPartItemState(tmpMark, false);
  103. //点击版不强制刷新镜头位置
  104. //CameraSurround.instance.SetCameraPosition(this.transform);
  105. }
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// 查找携带PartMark的节点
  111. /// </summary>
  112. /// <param name="traget"></param>
  113. /// <returns></returns>
  114. private PartMark GetParentPartMarkWithPartMark(Transform traget)
  115. {
  116. PartMark tmpMark = traget.GetComponent<PartMark>();
  117. if (tmpMark == null && traget.parent != null)
  118. {
  119. tmpMark = GetParentPartMarkWithPartMark(traget.parent);
  120. }
  121. return tmpMark;
  122. }
  123. public void OnDisable()
  124. {
  125. UIKit.ClosePanel<PartLabelPanel>();
  126. }
  127. public PartInfo GetDevicePartInfos(PartMark partMark)
  128. {
  129. //最父级
  130. PartInfo partInfo = new PartInfo(partMark.transform.name, partMark);
  131. //字级
  132. foreach (var item in partMark.m_ChildPartMarks)
  133. {
  134. partInfo.childPartInfos.Add(GetDevicePartInfos(item));
  135. }
  136. return partInfo;
  137. }
  138. [Button("为所有节点添加标记")]
  139. private void AddPartMark()
  140. {
  141. Transform[] tmpTransforms = GetComponentsInChildren<Transform>();
  142. foreach (var item in tmpTransforms)
  143. {
  144. if (item.GetComponent<PartMark>() == null)
  145. {
  146. item.gameObject.AddComponent<PartMark>();
  147. }
  148. }
  149. }
  150. }