CanvasTouchHandler.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using EZXR.Glass.SixDof;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using EZXR.Glass.Inputs;
  6. using System.Threading;
  7. namespace EZXR.Glass.Inputs
  8. {
  9. public class CanvasTouchHandler : MonoBehaviour
  10. {
  11. public bool canvasCollider = true;
  12. /// <summary>
  13. /// 两次点击的最小时间间隔
  14. /// </summary>
  15. float clickInterval = 0.2f;
  16. /// <summary>
  17. /// 上次点击的时刻
  18. /// </summary>
  19. float timer_LastClick;
  20. private Camera eventCamera;
  21. private bool startPinchChanged = false;
  22. private bool endPinchChanged = false;
  23. private void Awake()
  24. {
  25. eventCamera = Application.isEditor ? HMDPoseTracker.Instance.centerCamera : HMDPoseTracker.Instance.leftCamera;
  26. Canvas canvas = GetComponent<Canvas>();
  27. canvas.renderMode = UnityEngine.RenderMode.WorldSpace;
  28. canvas.worldCamera = eventCamera;
  29. }
  30. private void OnEnable()
  31. {
  32. if (canvasCollider)
  33. {
  34. var depth = 0.02f / transform.localScale.z;
  35. var sizeDelta = gameObject.GetComponent<RectTransform>().sizeDelta;
  36. if (gameObject.GetComponent<BoxCollider>() == null)
  37. {
  38. gameObject.AddComponent<BoxCollider>().size = new Vector3(sizeDelta.x, sizeDelta.y, depth);
  39. gameObject.GetComponent<BoxCollider>().center = new Vector3(0, 0, 0.005f);
  40. //canvas.gameObject.GetComponent<BoxCollider>().isTrigger = true;
  41. }
  42. else
  43. {
  44. gameObject.GetComponent<BoxCollider>().size = new Vector3(sizeDelta.x, sizeDelta.y, depth);
  45. gameObject.GetComponent<BoxCollider>().center = new Vector3(0, 0, 0.005f);
  46. //canvas.gameObject.GetComponent<BoxCollider>().isTrigger = true;
  47. }
  48. }
  49. }
  50. private void Update()
  51. {
  52. if (timer_LastClick < clickInterval)
  53. {
  54. timer_LastClick += Time.deltaTime;
  55. }
  56. else
  57. {
  58. UpdatePinching();
  59. }
  60. }
  61. /// <summary>
  62. /// 手势(食指尖)触摸Enter
  63. /// </summary>
  64. /// <param name="other">(食)指尖Collider</param>
  65. private void OnTriggerEnter(Collider other)
  66. {
  67. Debug.Log($"CanvasTouchHandler, OnTriggerEnter {other.name} ({Time.frameCount})");
  68. if (timer_LastClick >= clickInterval)
  69. {
  70. if (other.name.Contains("Left_Index_4"))
  71. {
  72. float angle = Vector3.Dot(Vector3.Project(other.transform.position - transform.position, -transform.forward).normalized, -transform.forward);
  73. Debug.Log("the angle: " + angle);
  74. if (1 - angle < 0.1f)
  75. {
  76. startPinchChanged = true;
  77. InputSystem.leftHand.isCloseContactingUGUI = true;
  78. //InputSystem.leftHand.RaycastInteraction = false;
  79. }
  80. }
  81. else if (other.name.Contains("Right_Index_4"))
  82. {
  83. float angle = Vector3.Dot(Vector3.Project(other.transform.position - transform.position, -transform.forward).normalized, -transform.forward);
  84. Debug.Log("the angle: " + angle);
  85. if (1 - angle < 0.1f)
  86. {
  87. startPinchChanged = true;
  88. InputSystem.rightHand.isCloseContactingUGUI = true;
  89. //InputSystem.rightHand.RaycastInteraction = false;
  90. }
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// 手势(食指尖)触摸Exit
  96. /// </summary>
  97. /// <param name="other">(食)指尖Collider</param>
  98. private void OnTriggerExit(Collider other)
  99. {
  100. Debug.Log($"CanvasTouchHandler, OnTriggerExit {other.name} ({Time.frameCount})");
  101. if (timer_LastClick >= clickInterval)
  102. {
  103. if (other.name.Contains("Left_Index_4"))
  104. {
  105. endPinchChanged = true;
  106. //ControllerManager.leftHand.isCloseContactingUGUI = false;
  107. }
  108. else if (other.name.Contains("Right_Index_4"))
  109. {
  110. endPinchChanged = true;
  111. //ControllerManager.rightHand.isCloseContactingUGUI = false;
  112. }
  113. }
  114. }
  115. private void UpdatePinching()
  116. {
  117. if (InputSystem.leftHand.isCloseContactingUGUI)
  118. {
  119. if (InputSystem.leftHand.startPinch)
  120. InputSystem.leftHand.startPinch = false;
  121. if (InputSystem.leftHand.endPinch)
  122. {
  123. InputSystem.leftHand.endPinch = false;
  124. InputSystem.leftHand.isCloseContactingUGUI = false;
  125. }
  126. }
  127. if (InputSystem.rightHand.isCloseContactingUGUI)
  128. {
  129. if (InputSystem.rightHand.startPinch)
  130. InputSystem.rightHand.startPinch = false;
  131. if (InputSystem.rightHand.endPinch)
  132. {
  133. InputSystem.rightHand.endPinch = false;
  134. InputSystem.rightHand.isCloseContactingUGUI = false;
  135. }
  136. }
  137. if (startPinchChanged)
  138. {
  139. startPinchChanged = false;
  140. if (InputSystem.leftHand.isCloseContactingUGUI)
  141. InputSystem.leftHand.startPinch = true;
  142. if (InputSystem.rightHand.isCloseContactingUGUI)
  143. InputSystem.rightHand.startPinch = true;
  144. }
  145. if (endPinchChanged)
  146. {
  147. endPinchChanged = false;
  148. if (InputSystem.leftHand.isCloseContactingUGUI)
  149. {
  150. InputSystem.leftHand.endPinch = true;
  151. InputSystem.leftHand.isCloseContactingUGUI = false;
  152. //InputSystem.leftHand.enableRayInteraction = true; //未起作用
  153. }
  154. if (InputSystem.rightHand.isCloseContactingUGUI)
  155. {
  156. InputSystem.rightHand.endPinch = true;
  157. InputSystem.rightHand.isCloseContactingUGUI = false;
  158. //InputSystem.rightHand.enableRayInteraction = true; //未起作用
  159. }
  160. timer_LastClick = 0;
  161. Debug.Log("endPinchChanged reset");
  162. }
  163. }
  164. }
  165. }