using EZXR.Glass.SixDof; using System.Collections; using System.Collections.Generic; using UnityEngine; using EZXR.Glass.Inputs; using System.Threading; namespace EZXR.Glass.Inputs { public class CanvasTouchHandler : MonoBehaviour { public bool canvasCollider = true; /// /// 两次点击的最小时间间隔 /// float clickInterval = 0.2f; /// /// 上次点击的时刻 /// float timer_LastClick; private Camera eventCamera; private bool startPinchChanged = false; private bool endPinchChanged = false; private void Awake() { eventCamera = Application.isEditor ? HMDPoseTracker.Instance.centerCamera : HMDPoseTracker.Instance.leftCamera; Canvas canvas = GetComponent(); canvas.renderMode = UnityEngine.RenderMode.WorldSpace; canvas.worldCamera = eventCamera; } private void OnEnable() { if (canvasCollider) { var depth = 0.02f / transform.localScale.z; var sizeDelta = gameObject.GetComponent().sizeDelta; if (gameObject.GetComponent() == null) { gameObject.AddComponent().size = new Vector3(sizeDelta.x, sizeDelta.y, depth); gameObject.GetComponent().center = new Vector3(0, 0, 0.005f); //canvas.gameObject.GetComponent().isTrigger = true; } else { gameObject.GetComponent().size = new Vector3(sizeDelta.x, sizeDelta.y, depth); gameObject.GetComponent().center = new Vector3(0, 0, 0.005f); //canvas.gameObject.GetComponent().isTrigger = true; } } } private void Update() { if (timer_LastClick < clickInterval) { timer_LastClick += Time.deltaTime; } else { UpdatePinching(); } } /// /// 手势(食指尖)触摸Enter /// /// (食)指尖Collider private void OnTriggerEnter(Collider other) { Debug.Log($"CanvasTouchHandler, OnTriggerEnter {other.name} ({Time.frameCount})"); if (timer_LastClick >= clickInterval) { if (other.name.Contains("Left_Index_4")) { float angle = Vector3.Dot(Vector3.Project(other.transform.position - transform.position, -transform.forward).normalized, -transform.forward); Debug.Log("the angle: " + angle); if (1 - angle < 0.1f) { startPinchChanged = true; InputSystem.leftHand.isCloseContactingUGUI = true; //InputSystem.leftHand.RaycastInteraction = false; } } else if (other.name.Contains("Right_Index_4")) { float angle = Vector3.Dot(Vector3.Project(other.transform.position - transform.position, -transform.forward).normalized, -transform.forward); Debug.Log("the angle: " + angle); if (1 - angle < 0.1f) { startPinchChanged = true; InputSystem.rightHand.isCloseContactingUGUI = true; //InputSystem.rightHand.RaycastInteraction = false; } } } } /// /// 手势(食指尖)触摸Exit /// /// (食)指尖Collider private void OnTriggerExit(Collider other) { Debug.Log($"CanvasTouchHandler, OnTriggerExit {other.name} ({Time.frameCount})"); if (timer_LastClick >= clickInterval) { if (other.name.Contains("Left_Index_4")) { endPinchChanged = true; //ControllerManager.leftHand.isCloseContactingUGUI = false; } else if (other.name.Contains("Right_Index_4")) { endPinchChanged = true; //ControllerManager.rightHand.isCloseContactingUGUI = false; } } } private void UpdatePinching() { if (InputSystem.leftHand.isCloseContactingUGUI) { if (InputSystem.leftHand.startPinch) InputSystem.leftHand.startPinch = false; if (InputSystem.leftHand.endPinch) { InputSystem.leftHand.endPinch = false; InputSystem.leftHand.isCloseContactingUGUI = false; } } if (InputSystem.rightHand.isCloseContactingUGUI) { if (InputSystem.rightHand.startPinch) InputSystem.rightHand.startPinch = false; if (InputSystem.rightHand.endPinch) { InputSystem.rightHand.endPinch = false; InputSystem.rightHand.isCloseContactingUGUI = false; } } if (startPinchChanged) { startPinchChanged = false; if (InputSystem.leftHand.isCloseContactingUGUI) InputSystem.leftHand.startPinch = true; if (InputSystem.rightHand.isCloseContactingUGUI) InputSystem.rightHand.startPinch = true; } if (endPinchChanged) { endPinchChanged = false; if (InputSystem.leftHand.isCloseContactingUGUI) { InputSystem.leftHand.endPinch = true; InputSystem.leftHand.isCloseContactingUGUI = false; //InputSystem.leftHand.enableRayInteraction = true; //未起作用 } if (InputSystem.rightHand.isCloseContactingUGUI) { InputSystem.rightHand.endPinch = true; InputSystem.rightHand.isCloseContactingUGUI = false; //InputSystem.rightHand.enableRayInteraction = true; //未起作用 } timer_LastClick = 0; Debug.Log("endPinchChanged reset"); } } } }