123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- 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;
- /// <summary>
- /// 两次点击的最小时间间隔
- /// </summary>
- float clickInterval = 0.2f;
- /// <summary>
- /// 上次点击的时刻
- /// </summary>
- 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>();
- canvas.renderMode = UnityEngine.RenderMode.WorldSpace;
- canvas.worldCamera = eventCamera;
- }
- private void OnEnable()
- {
- if (canvasCollider)
- {
- var depth = 0.02f / transform.localScale.z;
- var sizeDelta = gameObject.GetComponent<RectTransform>().sizeDelta;
- if (gameObject.GetComponent<BoxCollider>() == null)
- {
- gameObject.AddComponent<BoxCollider>().size = new Vector3(sizeDelta.x, sizeDelta.y, depth);
- gameObject.GetComponent<BoxCollider>().center = new Vector3(0, 0, 0.005f);
- //canvas.gameObject.GetComponent<BoxCollider>().isTrigger = true;
- }
- else
- {
- gameObject.GetComponent<BoxCollider>().size = new Vector3(sizeDelta.x, sizeDelta.y, depth);
- gameObject.GetComponent<BoxCollider>().center = new Vector3(0, 0, 0.005f);
- //canvas.gameObject.GetComponent<BoxCollider>().isTrigger = true;
- }
- }
- }
- private void Update()
- {
- if (timer_LastClick < clickInterval)
- {
- timer_LastClick += Time.deltaTime;
- }
- else
- {
- UpdatePinching();
- }
- }
- /// <summary>
- /// 手势(食指尖)触摸Enter
- /// </summary>
- /// <param name="other">(食)指尖Collider</param>
- 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;
- }
- }
- }
- }
- /// <summary>
- /// 手势(食指尖)触摸Exit
- /// </summary>
- /// <param name="other">(食)指尖Collider</param>
- 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");
- }
- }
- }
- }
|