SpatialUIEventSystem.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using EZXR.Glass.Inputs;
  5. namespace EZXR.Glass.UI
  6. {
  7. [ExecuteInEditMode]
  8. public partial class SpatialUIEventSystem : MonoBehaviour
  9. {
  10. #region Singleton
  11. private static SpatialUIEventSystem instance;
  12. public static SpatialUIEventSystem Instance
  13. {
  14. get
  15. {
  16. if (instance == null)
  17. {
  18. instance = FindObjectOfType<SpatialUIEventSystem>();
  19. if (instance == null)
  20. {
  21. GameObject go = new GameObject("SpatialUIEventSystem");
  22. instance = go.AddComponent<SpatialUIEventSystem>();
  23. }
  24. }
  25. return instance;
  26. }
  27. }
  28. #endregion
  29. static Dictionary<Collider, SpatialSelectable> selectablesDic = new Dictionary<Collider, SpatialSelectable>();
  30. Collider lastOne_Left, lastOne_Right;
  31. private SpatialSelectable currentFocusSpatialUIElement;
  32. public SpatialSelectable CurrentFocusSpatialUIElement
  33. {
  34. get
  35. {
  36. return currentFocusSpatialUIElement;
  37. }
  38. set
  39. {
  40. currentFocusSpatialUIElement = value;
  41. }
  42. }
  43. private SpatialSelectable currentHoverSpatialUIElement;
  44. public SpatialSelectable CurrentHoverSpatialUIElement
  45. {
  46. get
  47. {
  48. return currentHoverSpatialUIElement;
  49. }
  50. set
  51. {
  52. currentHoverSpatialUIElement = value;
  53. }
  54. }
  55. partial void VolumeNavigateInit();
  56. private void Awake()
  57. {
  58. VolumeNavigateInit();
  59. if (Application.isPlaying)
  60. {
  61. instance = this;
  62. }
  63. else
  64. {
  65. //#if UNITY_EDITOR
  66. // Common.PrefabUtility.UnpackPrefabInstance(gameObject);
  67. //#endif
  68. }
  69. }
  70. void Start()
  71. {
  72. if (Application.isPlaying)
  73. {
  74. //用于得到当前手的射线检测的结果
  75. if (ARHandManager.leftHand != null)
  76. {
  77. ARHandManager.leftHand.Event_GetRayCastResult += OnRayCastHit_Left;
  78. }
  79. if (ARHandManager.rightHand != null)
  80. {
  81. ARHandManager.rightHand.Event_GetRayCastResult += OnRayCastHit_Right;
  82. }
  83. if (HandleControllerManager.leftHand != null)
  84. {
  85. HandleControllerManager.leftHand.Event_GetRayCastResult += OnRayCastHit_Left;
  86. }
  87. if (HandleControllerManager.rightHand != null)
  88. {
  89. HandleControllerManager.rightHand.Event_GetRayCastResult += OnRayCastHit_Right;
  90. }
  91. }
  92. }
  93. private void Update()
  94. {
  95. //if (Input.GetKeyDown(UnityEngine.KeyCode.Return))
  96. //{
  97. // currentFocusSpatialUIElement?.OnPointerClicked();
  98. //}
  99. }
  100. public static void RegisterCallBack(Collider collider, SpatialSelectable selectable)
  101. {
  102. if (!selectablesDic.ContainsKey(collider))
  103. {
  104. selectablesDic.Add(collider, selectable);
  105. }
  106. }
  107. public void OnRayCastHit_Left(Collider other, bool isUI)
  108. {
  109. if (lastOne_Left != null && lastOne_Left != other)
  110. {
  111. if (selectablesDic.ContainsKey(lastOne_Left))
  112. {
  113. selectablesDic[lastOne_Left].OnRayCastHit(null);
  114. }
  115. }
  116. if (isUI && other != null)
  117. {
  118. if (selectablesDic.ContainsKey(other))
  119. {
  120. selectablesDic[other].OnRayCastHit(InputSystem.leftHand);
  121. }
  122. }
  123. lastOne_Left = other;
  124. }
  125. public void OnRayCastHit_Right(Collider other, bool isUI)
  126. {
  127. if (lastOne_Right != null && lastOne_Right != other)
  128. {
  129. if (selectablesDic.ContainsKey(lastOne_Right))
  130. {
  131. selectablesDic[lastOne_Right].OnRayCastHit(null);
  132. }
  133. }
  134. if (isUI && other != null)
  135. {
  136. if (selectablesDic.ContainsKey(other))
  137. {
  138. selectablesDic[other].OnRayCastHit(InputSystem.rightHand);
  139. }
  140. }
  141. lastOne_Right = other;
  142. }
  143. }
  144. }