DeviceController.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  29. RaycastHit hit;
  30. if (Physics.Raycast(ray, out hit))
  31. {
  32. PartMark partMark = GetParentPartMarkWithPartMark(hit.collider.transform);
  33. // 处理射线击中的对象
  34. if (partMark != null)
  35. {
  36. UIKit.OpenPanel<PartLabelPanel>(UILevel.PopUI, new PartLabelPanelData() { partName = partMark.name });
  37. }
  38. }
  39. else
  40. {
  41. UIKit.HidePanel<PartLabelPanel>();
  42. }
  43. if (Input.GetMouseButtonDown(0))
  44. {
  45. Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
  46. RaycastHit hit1;
  47. if (Physics.Raycast(ray1, out hit1))
  48. {
  49. PartMark tmpMark = GetParentPartMarkWithPartMark(hit.collider.transform);
  50. if (tmpMark != null)
  51. {
  52. tmpMark.SetState(false);
  53. m_OperationPartMarkStruct.Push(tmpMark);
  54. UIKit.GetPanel<PartListPanel>().SetPartItemState(tmpMark, false);
  55. CameraSurround.instance.SetCameraPosition(true);
  56. }
  57. }
  58. }
  59. }
  60. /// <summary>
  61. /// 查找携带PartMark的节点
  62. /// </summary>
  63. /// <param name="traget"></param>
  64. /// <returns></returns>
  65. private PartMark GetParentPartMarkWithPartMark(Transform traget)
  66. {
  67. PartMark tmpMark = traget.GetComponent<PartMark>();
  68. if (tmpMark == null && traget.parent != null)
  69. {
  70. tmpMark = GetParentPartMarkWithPartMark(traget.parent);
  71. }
  72. return tmpMark;
  73. }
  74. public PartInfo GetDevicePartInfos(PartMark partMark)
  75. {
  76. //最父级
  77. PartInfo partInfo = new PartInfo(partMark.transform.name, partMark);
  78. //字级
  79. foreach (var item in partMark.m_ChildPartMarks)
  80. {
  81. partInfo.childPartInfos.Add(GetDevicePartInfos(item));
  82. }
  83. return partInfo;
  84. }
  85. [Button("为所有节点添加标记")]
  86. private void AddPartMark()
  87. {
  88. Transform[] tmpTransforms = GetComponentsInChildren<Transform>();
  89. foreach (var item in tmpTransforms)
  90. {
  91. if (item.GetComponents<PartMark>() == null)
  92. {
  93. item.gameObject.AddComponent<PartMark>();
  94. }
  95. }
  96. }
  97. }