123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- using EZXR.Glass.SixDof;
- using EZXR.Glass.Inputs;
- using Wheels.Unity;
- namespace EZXR.Glass.UI
- {
- public class UGUIHandler : MonoBehaviour
- {
- #region singleton
- private static UGUIHandler instance;
- public static UGUIHandler Instance
- {
- get
- {
- return instance;
- }
- }
- #endregion
- Camera eventCamera;
- private void Awake()
- {
- instance = this;
- }
- // Start is called before the first frame update
- void Start()
- {
- GetComponent<EventSystem>().pixelDragThreshold = 80;
- eventCamera = Application.isEditor ? HMDPoseTracker.Instance.centerCamera : HMDPoseTracker.Instance.leftCamera;
- }
- // Update is called once per frame
- void Update()
- {
- //左手
- if (InputSystem.leftHand && InputSystem.leftHand.Exist)
- {
- if (InputSystem.leftHand.isCloseContactingUGUI)//近距离交互
- {//当前只有手有近距离交互,所以此处直接用ARHandManager
- if (ARHandManager.leftHand.InLeftView(HandJointType.Index_4))
- {
- eventCamera = HMDPoseTracker.Instance.leftCamera;
- }
- else if (ARHandManager.leftHand.InRightView(HandJointType.Index_4))
- {
- eventCamera = HMDPoseTracker.Instance.rightCamera;
- }
- foreach (Canvas canvas in CanvasHandler.canvases)
- {
- canvas.worldCamera = eventCamera;
- }
- HandInputModule.touches[0].position = eventCamera.WorldToScreenPoint(ARHandManager.leftHand.GetJointData(HandJointType.Index_4).position);
- }
- else//远距离射线交互
- {
- if (InputSystem.leftHand.rayPoint_End.position.InView(HMDPoseTracker.Instance.leftCamera))
- {
- eventCamera = HMDPoseTracker.Instance.leftCamera;
- }
- else if (InputSystem.leftHand.rayPoint_End.position.InView(HMDPoseTracker.Instance.rightCamera))
- {
- eventCamera = HMDPoseTracker.Instance.rightCamera;
- }
- foreach (Canvas canvas in CanvasHandler.canvases)
- {
- canvas.worldCamera = eventCamera;
- }
- HandInputModule.touches[0].position = eventCamera.WorldToScreenPoint(InputSystem.leftHand.rayPoint_End.position);
- }
- }
- else
- {
- HandInputModule.touches[0].position = Vector2.zero;
- }
- //右手
- if (InputSystem.rightHand && InputSystem.rightHand.Exist)
- {
- if (InputSystem.rightHand.isCloseContactingUGUI)//近距离交互
- {//当前只有手有近距离交互,所以此处直接用ARHandManager
- if (ARHandManager.rightHand.InLeftView(HandJointType.Index_4))
- {
- eventCamera = HMDPoseTracker.Instance.leftCamera;
- }
- else if (ARHandManager.rightHand.InRightView(HandJointType.Index_4))
- {
- eventCamera = HMDPoseTracker.Instance.rightCamera;
- }
- foreach (Canvas canvas in CanvasHandler.canvases)
- {
- canvas.worldCamera = eventCamera;
- }
- HandInputModule.touches[1].position = eventCamera.WorldToScreenPoint(ARHandManager.rightHand.GetJointData(HandJointType.Index_4).position);
- }
- else//远距离射线交互
- {
- if (InputSystem.rightHand.rayPoint_End.position.InView(HMDPoseTracker.Instance.leftCamera))
- {
- eventCamera = HMDPoseTracker.Instance.leftCamera;
- }
- else if (InputSystem.rightHand.rayPoint_End.position.InView(HMDPoseTracker.Instance.rightCamera))
- {
- eventCamera = HMDPoseTracker.Instance.rightCamera;
- }
- foreach (Canvas canvas in CanvasHandler.canvases)
- {
- canvas.worldCamera = eventCamera;
- }
- HandInputModule.touches[1].position = eventCamera.WorldToScreenPoint(InputSystem.rightHand.rayPoint_End.position);
- }
- }
- else
- {
- HandInputModule.touches[1].position = Vector2.zero;
- }
- //if (Input.GetKeyDown(KeyCode.Q))
- // Debug.Log($"HandControllerSession, ctrlTouch: ({HandInputModule.touches[1].position.x}, {HandInputModule.touches[1].position.y})");
- }
- //public void ConfigAllCanvas()
- //{
- // eventCamera = Application.isEditor ? HMDPoseTracker.Instance.centerCamera : HMDPoseTracker.Instance.leftCamera;
- // canvas = FindObjectsOfType<Canvas>();
- // foreach (Canvas item in canvas)
- // {
- // if (item.transform.parent != null && item.transform.parent.GetComponent<PhoneUIRenderer>() != null)
- // {
- // continue;
- // }
- // if (item.gameObject.GetComponent<BoxCollider>() == null)
- // {
- // item.gameObject.AddComponent<BoxCollider>().size = item.GetComponent<RectTransform>().sizeDelta;
- // }
- // item.worldCamera = eventCamera;
- // }
- //}
- }
- }
|