DeviceController.cs 5.0 KB

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