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().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(); // foreach (Canvas item in canvas) // { // if (item.transform.parent != null && item.transform.parent.GetComponent() != null) // { // continue; // } // if (item.gameObject.GetComponent() == null) // { // item.gameObject.AddComponent().size = item.GetComponent().sizeDelta; // } // item.worldCamera = eventCamera; // } //} } }