using GxrSdk; using SC.XR.Unity.Module_InputSystem; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.XR; using UnityEngine.XR.OpenXR.Input; public class XRInputManager : MonoBehaviour, IGxrControllerPoseHandler, IGxrButtonHandler, IGxrTouchHandler { public Transform left = null; public Transform right = null; public InputActionProperty inputEvent; public List listinput; int xy = 8; // Start is called before the first frame update void Start() { Invoke("startXR", 1f); } GameObject RightX3C01Pointer; GameObject RightX3C01Controller; void startXR() { Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.KS, true); Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.Head, false); #if UNITY_EDITOR Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.KS, false); Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.Head, true); #endif left.gameObject.SetActive(false); right.gameObject.SetActive(true); } private void Update() { if (Input.GetKeyDown(KeyCode.X)) { API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.DOWN); } if (Input.GetKeyUp(KeyCode.X)) { API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.UP); } } private void onDeviceDisConnected(UnityEngine.XR.InputDevice obj) { if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Left.ToString())) { Debug.Log("Left===>" + obj.isValid); left.gameObject.SetActive(obj.isValid); } if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Right.ToString())) { Debug.Log("Right===>" + obj.isValid); right.gameObject.SetActive(obj.isValid); } } private void OnApplicationPause(bool pause) { if (!pause) { List inputDevices = new List(); InputDevices.GetDevices(inputDevices); for (int i = 0; i < inputDevices.Count; i++) { onDeviceConnected(inputDevices[i]); } } } private void onDeviceConnected(UnityEngine.XR.InputDevice obj) { if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Left.ToString())) { Debug.Log("Left===>" + obj.isValid); left.gameObject.SetActive(obj.isValid); } if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Right.ToString())) { Debug.Log("Right===>" + obj.isValid); right.gameObject.SetActive(obj.isValid); } } private void OnCanceledEventL(InputAction.CallbackContext context) { Debug.Log("OnCanceledEventL" + context.control.name); switch (context.control.name) { case "secondarytouched": break; case "secondarybutton": API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Y, InputKeyState.UP); break; case "primarytouched": break; case "primarybutton": Debug.Log("InputKeyCode.A===》UP"); API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.UP); API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP); break; case "touchpadtouched": break; case "thumbstickclicked": break; case "thumbstick": API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickX = xy + (int)(context.ReadValue().x * 10); API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickY = xy + (int)(context.ReadValue().y * 10); break; case "grip": API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallInside = 10 - (int)((context.ReadValue()) * 10f); break; case "grippressed": API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LHallInside, InputKeyState.UP); break; case "triggerButton": // API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallFoward = 10 - (int)((context.ReadValue()) * 10f); API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP); break; case "menu": API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LFunction, InputKeyState.UP); break; } } void OnEventL(InputAction.CallbackContext context) { Debug.Log("OnEventL" + context.control.name); switch (context.control.name) { case "secondarytouched": break; case "secondarybutton": API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Y, InputKeyState.DOWN); break; case "primarytouched": break; case "primarybutton": Debug.Log("InputKeyCode.A===》DOWN"); API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.DOWN); API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN); break; case "touchpadtouched": break; case "thumbstickclicked": break; case "thumbstick": API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickX = xy + (int)(context.ReadValue().x * 10); API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickY = xy + (int)(context.ReadValue().y * 10); break; case "grip": API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallInside = 10 - (int)((context.ReadValue()) * 10f); break; case "grippressed": API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LHallInside, InputKeyState.DOWN); break; case "triggerButton": // API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallFoward = 10 - (int)((context.ReadValue()) * 10f); API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN); break; case "menu": API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LFunction, InputKeyState.DOWN); break; } } private void OnCanceledEventR(InputAction.CallbackContext context) { Debug.Log("OnCanceledEventR" + context.control.name); switch (context.control.name) { case "secondarytouched": break; case "secondarybutton": API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.B, InputKeyState.UP); API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP); break; case "primarytouched": break; case "primarybutton": API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.A, InputKeyState.UP); break; case "touchpadtouched": break; case "thumbstickclicked": break; case "thumbstick": API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickX = xy + (int)(context.ReadValue().x * 10); API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickY = xy + (int)(context.ReadValue().y * 10); break; case "grip": API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallInside = 10 - (int)((context.ReadValue()) * 10f); break; case "grippressed": API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RHallInside, InputKeyState.UP); break; case "trigger": // API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallFoward = 10 - (int)((context.ReadValue()) * 10f); API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP); break; case "menu": API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RFunction, InputKeyState.UP); break; } } void OnEventR(InputAction.CallbackContext context) { Debug.Log("OnEventR" + context.control.name); switch (context.control.name) { case "secondarytouched": break; case "secondarybutton": API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.B, InputKeyState.DOWN); API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN); break; case "primarytouched": break; case "primarybutton": API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.A, InputKeyState.DOWN); break; case "touchpadtouched": break; case "thumbstickclicked": break; case "thumbstick": API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickX = xy + (int)(context.ReadValue().x * 10); API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickY = xy + (int)(context.ReadValue().y * 10); break; case "grip": API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallInside = 10 - (int)((context.ReadValue()) * 10f); break; case "grippressed": API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RHallInside, InputKeyState.DOWN); break; case "xri_right_trigger": // API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallFoward = 10 - (int)((context.ReadValue()) * 10f); API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN); break; case "menu": API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RFunction, InputKeyState.DOWN); break; } } public void OnControllerPoseUpdated(GxrControllerPoseEventData eventData) { if (right != null) { right.rotation = eventData.CurrentRotation; right.position = new Vector3(OpenXRCamera.Instance.head.transform.position.x, OpenXRCamera.Instance.head.transform.position.y - 0.3f, OpenXRCamera.Instance.head.transform.position.z); } } void OnEnable() { // 注册控制器姿态监听器 GxrSystemAccessor.InputSystem?.RegisterHandler(this); // 注册按键事件监听器 GxrSystemAccessor.InputSystem?.RegisterHandler(this); // 注册触摸事件监听器 GxrSystemAccessor.InputSystem?.RegisterHandler(this); } void OnDisable() { // 注销控制器姿态监听器 GxrSystemAccessor.InputSystem?.UnregisterHandler(this); // 注销按键事件监听器 GxrSystemAccessor.InputSystem?.UnregisterHandler(this); // 注销触摸事件监听器 GxrSystemAccessor.InputSystem?.UnregisterHandler(this); } public void OnButtonDown(GxrButtonEventData eventData) { API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN); } public void OnButtonUp(GxrButtonEventData eventData) { API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP); } public void OnButtonClick(GxrButtonEventData eventData) { } public void OnButtonLongClick(GxrButtonEventData eventData) { } public void OnButtonDoubleClick(GxrButtonEventData eventData) { } public void OnTouchBegin(GxrTouchEventData eventData) { } public void OnTouchEnd(GxrTouchEventData eventData) { } public void OnTouchSwipe(GxrTouchEventData eventData) { } public void OnTouchPositionUpdated(GxrTouchEventData eventData) { } }