UGUIHandler.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. using EZXR.Glass.SixDof;
  7. using EZXR.Glass.Inputs;
  8. using Wheels.Unity;
  9. namespace EZXR.Glass.UI
  10. {
  11. public class UGUIHandler : MonoBehaviour
  12. {
  13. #region singleton
  14. private static UGUIHandler instance;
  15. public static UGUIHandler Instance
  16. {
  17. get
  18. {
  19. return instance;
  20. }
  21. }
  22. #endregion
  23. Camera eventCamera;
  24. private void Awake()
  25. {
  26. instance = this;
  27. }
  28. // Start is called before the first frame update
  29. void Start()
  30. {
  31. GetComponent<EventSystem>().pixelDragThreshold = 80;
  32. eventCamera = Application.isEditor ? HMDPoseTracker.Instance.centerCamera : HMDPoseTracker.Instance.leftCamera;
  33. }
  34. // Update is called once per frame
  35. void Update()
  36. {
  37. //左手
  38. if (InputSystem.leftHand && InputSystem.leftHand.Exist)
  39. {
  40. if (InputSystem.leftHand.isCloseContactingUGUI)//近距离交互
  41. {//当前只有手有近距离交互,所以此处直接用ARHandManager
  42. if (ARHandManager.leftHand.InLeftView(HandJointType.Index_4))
  43. {
  44. eventCamera = HMDPoseTracker.Instance.leftCamera;
  45. }
  46. else if (ARHandManager.leftHand.InRightView(HandJointType.Index_4))
  47. {
  48. eventCamera = HMDPoseTracker.Instance.rightCamera;
  49. }
  50. foreach (Canvas canvas in CanvasHandler.canvases)
  51. {
  52. canvas.worldCamera = eventCamera;
  53. }
  54. HandInputModule.touches[0].position = eventCamera.WorldToScreenPoint(ARHandManager.leftHand.GetJointData(HandJointType.Index_4).position);
  55. }
  56. else//远距离射线交互
  57. {
  58. if (InputSystem.leftHand.rayPoint_End.position.InView(HMDPoseTracker.Instance.leftCamera))
  59. {
  60. eventCamera = HMDPoseTracker.Instance.leftCamera;
  61. }
  62. else if (InputSystem.leftHand.rayPoint_End.position.InView(HMDPoseTracker.Instance.rightCamera))
  63. {
  64. eventCamera = HMDPoseTracker.Instance.rightCamera;
  65. }
  66. foreach (Canvas canvas in CanvasHandler.canvases)
  67. {
  68. canvas.worldCamera = eventCamera;
  69. }
  70. HandInputModule.touches[0].position = eventCamera.WorldToScreenPoint(InputSystem.leftHand.rayPoint_End.position);
  71. }
  72. }
  73. else
  74. {
  75. HandInputModule.touches[0].position = Vector2.zero;
  76. }
  77. //右手
  78. if (InputSystem.rightHand && InputSystem.rightHand.Exist)
  79. {
  80. if (InputSystem.rightHand.isCloseContactingUGUI)//近距离交互
  81. {//当前只有手有近距离交互,所以此处直接用ARHandManager
  82. if (ARHandManager.rightHand.InLeftView(HandJointType.Index_4))
  83. {
  84. eventCamera = HMDPoseTracker.Instance.leftCamera;
  85. }
  86. else if (ARHandManager.rightHand.InRightView(HandJointType.Index_4))
  87. {
  88. eventCamera = HMDPoseTracker.Instance.rightCamera;
  89. }
  90. foreach (Canvas canvas in CanvasHandler.canvases)
  91. {
  92. canvas.worldCamera = eventCamera;
  93. }
  94. HandInputModule.touches[1].position = eventCamera.WorldToScreenPoint(ARHandManager.rightHand.GetJointData(HandJointType.Index_4).position);
  95. }
  96. else//远距离射线交互
  97. {
  98. if (InputSystem.rightHand.rayPoint_End.position.InView(HMDPoseTracker.Instance.leftCamera))
  99. {
  100. eventCamera = HMDPoseTracker.Instance.leftCamera;
  101. }
  102. else if (InputSystem.rightHand.rayPoint_End.position.InView(HMDPoseTracker.Instance.rightCamera))
  103. {
  104. eventCamera = HMDPoseTracker.Instance.rightCamera;
  105. }
  106. foreach (Canvas canvas in CanvasHandler.canvases)
  107. {
  108. canvas.worldCamera = eventCamera;
  109. }
  110. HandInputModule.touches[1].position = eventCamera.WorldToScreenPoint(InputSystem.rightHand.rayPoint_End.position);
  111. }
  112. }
  113. else
  114. {
  115. HandInputModule.touches[1].position = Vector2.zero;
  116. }
  117. //if (Input.GetKeyDown(KeyCode.Q))
  118. // Debug.Log($"HandControllerSession, ctrlTouch: ({HandInputModule.touches[1].position.x}, {HandInputModule.touches[1].position.y})");
  119. }
  120. //public void ConfigAllCanvas()
  121. //{
  122. // eventCamera = Application.isEditor ? HMDPoseTracker.Instance.centerCamera : HMDPoseTracker.Instance.leftCamera;
  123. // canvas = FindObjectsOfType<Canvas>();
  124. // foreach (Canvas item in canvas)
  125. // {
  126. // if (item.transform.parent != null && item.transform.parent.GetComponent<PhoneUIRenderer>() != null)
  127. // {
  128. // continue;
  129. // }
  130. // if (item.gameObject.GetComponent<BoxCollider>() == null)
  131. // {
  132. // item.gameObject.AddComponent<BoxCollider>().size = item.GetComponent<RectTransform>().sizeDelta;
  133. // }
  134. // item.worldCamera = eventCamera;
  135. // }
  136. //}
  137. }
  138. }