using Immersal.AR; using SC.XR.Unity.Module_InputSystem; using UnityEngine; using UnityEngine.UI; public class MoveChange : MonoBehaviour { public Transform ControllerTracker; // 手柄 private GameObject mesh_test => SceneIOCContainer.Instance.Pull("mesh_test") as GameObject; private Transform ARSpaceForAll=>(SceneIOCContainer.Instance.Pull("ARSpaceForAll")as GameObject).transform; [SerializeField] private Toggle RotToggle; private bool isDown; private float y=> SetSceneActive.Instance.goReference_y; void Start() { ControllerTracker = GameObject.Find("SDKSystem").transform.Find("NRInput/Right/ControllerTracker"); DispatcherBase.KeyDownDelegateRegister(keyDownEventDelegate); DispatcherBase.KeyUpDelegateRegister(keyUpEventDelegate); RotToggle.onValueChanged.AddListener(RotToggleOnValueChanged); IsRot = RotToggle.isOn; } private void keyUpEventDelegate(InputKeyCode keyCode, InputDevicePartBase part) { if (keyCode != InputKeyCode.Enter || goRefrence==null) { return; } Debug.Log("keyDownEventDelegate===>" + keyCode); mesh_test.transform.parent = ARSpaceForAll; goRefrence.transform.parent = ARSpaceForAll; isDown = false; } private void keyDownEventDelegate(InputKeyCode keyCode, InputDevicePartBase part) { if(keyCode != InputKeyCode.Enter) { return; } if (!SetSceneActive.Instance.active|| goRefrence==null) return; if(!IsRot) { TempVector3 = OpenXRCamera.Instance.head.position-goRefrence.transform.position; } mesh_test.transform.parent = goRefrence.transform; /* Debug.Log("keyUpEventDelegate===>" + keyCode); #if UNITY_EDITOR //moveGame.transform.parent = OpenXRCamera.Instance.head; #else switch (DeviceType.type) { case "Nreal": moveGame.transform.parent = API_GSXR_Module_InputSystem_KS.GSXR_KSRight.transform;// OpenXRCamera.Instance.head; break; case "A01": moveGame.transform.parent = OpenXRCamera.Instance.head; break; case "Phone": moveGame.transform.parent = OpenXRCamera.Instance.head; break; case "Rhinox": moveGame.transform.parent = API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDeviceKSPartUI.transform; break; default: moveGame.transform.parent = API_GSXR_Module_InputSystem_KS.GSXR_KSRight.transform;// OpenXRCamera.Instance.head; break; } #endif */ isDown = true; } bool isCx; void Update() { if (ARSpace.isFind) { if (!isCx) { txtTip.SetActive(true); txtTip.GetComponent().TimeFloat = 3; txtTip.GetComponent().t = 0; txtTip.GetComponentInChildren().text = "空间定位成功"; } isCx = true; } else { txtTip.SetActive(true); txtTip.GetComponentInChildren().text = "请扫描周围环境以进行空间定位"; } if (isDown && SetSceneActive.Instance.active) { if(!IsRot) { goRefrence.transform.position = OpenXRCamera.Instance.head.position - TempVector3; goRefrence.transform.position = new Vector3(goRefrence.transform.position.x, y, goRefrence.transform.position.z); } else { v3.y = ControllerTracker_Y - ControllerTracker.localEulerAngles.y; goRefrence.transform.localEulerAngles +=v3; ControllerTracker_Y = ControllerTracker.localEulerAngles.y; /* goRefrence.transform.localEulerAngles = ControllerTracker.localEulerAngles - TempVector3; goRefrence.transform.localEulerAngles = new Vector3(0, goRefrence.transform.localEulerAngles.y, 0); */ } //moveGame.transform.eulerAngles = new Vector3(0, moveGame.transform.eulerAngles.y, 0); } if (Input.GetKeyDown(KeyCode.I)) { ARSpace.isFind = true; } ControllerTracker_Y = ControllerTracker.localEulerAngles.y; } private Vector3 v3 = Vector3.zero; // 用于存放手柄上次与下次的差值 private float ControllerTracker_Y = 0; // 上次手柄的旋转Y值 private bool IsRot; private Vector3 TempVector3; public GameObject goRefrence // 参照物 { get =>SceneIOCContainer.Instance.Pull("goRefrence")as GameObject; } public GameObject txtTip; private void RotToggleOnValueChanged(bool isOn) { Debug.Log("选择了RotToggleOnValueChanged"); txtTip.SetActive(true); if (isOn) { txtTip.GetComponentInChildren().text = "如需调整场景旋转角度,请按住手柄拖动场景\n场景将以锚点为圆心进行旋转"; IsRot = true; } else { txtTip.GetComponentInChildren().text = "如需移动场景,请按住手柄拖动场景并走动\n场景将跟随您的脚步进行整体移动"; IsRot = false; } } }