1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.XR; using System;
public class XRInputManager : MonoBehaviour { [SerializeField] InputDevice leftHandController, rightHandController; [SerializeField] bool buttonPressed_A, buttonPressed_B, buttonPressed_X, buttonPressed_Y; [SerializeField] bool buttonTouched_A, buttonTouched_B, buttonTouched_X, buttonTouched_Y; [SerializeField] Vector2 leftJoyStickAngle, rightJoyStickAngle; [SerializeField] float leftTriggerValue, rightTriggerValue; [SerializeField] float leftGripValue, rightGripValue; public event EventHandler<ControllerEventArgs> OnControllerValueChanged; public void devicesInit() { var foundDevices = new List<UnityEngine.XR.InputDevice>(); InputDevices.GetDevicesAtXRNode(UnityEngine.XR.XRNode.LeftHand, foundDevices);
if (foundDevices.Count == 1) { leftHandController = foundDevices[0]; DebugBoard.Debug(string.Format("Device name '{0}' with role '{1}'", leftHandController.name, leftHandController.role.ToString())); Debug.Log(string.Format("Device name '{0}' with role '{1}'", leftHandController.name, leftHandController.role.ToString())); } else if (foundDevices.Count > 1) { Debug.Log("Found more than one left hand!"); } // find right hand controller foundDevices = new List<UnityEngine.XR.InputDevice>(); InputDevices.GetDevicesAtXRNode(UnityEngine.XR.XRNode.RightHand, foundDevices); if (foundDevices.Count == 1) { rightHandController = foundDevices[0]; DebugBoard.Debug(string.Format("Device name '{0}' with role '{1}'", rightHandController.name, rightHandController.role.ToString())); Debug.Log(string.Format("Device name '{0}' with role '{1}'", rightHandController.name, rightHandController.role.ToString())); } else if (foundDevices.Count > 1) { Debug.Log("Found more than one right hand!"); } } // Start is called before the first frame update void Start() { devicesInit(); }
public bool getButtonValue(InputDevice device, InputFeatureUsage<bool> button) { bool buttonPressed; if (device.isValid == false) { devicesInit(); } device.TryGetFeatureValue(button,out buttonPressed); return buttonPressed; }
public Vector2 getJoyStickValue(InputDevice device,InputFeatureUsage<Vector2> joyStick) { Vector2 joyStickAngle; device.TryGetFeatureValue(joyStick, out joyStickAngle); return joyStickAngle; }
public float getTriggerValue(InputDevice device, InputFeatureUsage<float> trigger) { float triggerValue; device.TryGetFeatureValue(trigger, out triggerValue); return triggerValue; }
// Update is called once per frame void Update() { buttonPressed_A = getButtonValue(rightHandController, CommonUsages.primaryButton); buttonPressed_B = getButtonValue(rightHandController, CommonUsages.secondaryButton); buttonTouched_A = getButtonValue(rightHandController, CommonUsages.primaryTouch); buttonTouched_B = getButtonValue(rightHandController, CommonUsages.secondaryTouch);
buttonPressed_X = getButtonValue(leftHandController, CommonUsages.primaryButton); buttonPressed_Y = getButtonValue(leftHandController, CommonUsages.secondaryButton); buttonTouched_X = getButtonValue(leftHandController, CommonUsages.primaryTouch); buttonTouched_Y = getButtonValue(leftHandController, CommonUsages.secondaryTouch);
//joysticks leftJoyStickAngle = getJoyStickValue(leftHandController, CommonUsages.primary2DAxis); rightJoyStickAngle = getJoyStickValue(rightHandController, CommonUsages.primary2DAxis);
//trigger leftTriggerValue = getTriggerValue(leftHandController, CommonUsages.trigger); rightTriggerValue = getTriggerValue(rightHandController, CommonUsages.trigger); //Grip leftGripValue = getTriggerValue(leftHandController, CommonUsages.grip); rightGripValue = getTriggerValue(rightHandController, CommonUsages.grip);
ControllerEventArgs controllerEventArgs = new ControllerEventArgs(); controllerEventArgs.BtnAPressed = buttonPressed_A; controllerEventArgs.BtnATouched = buttonTouched_A; controllerEventArgs.BtnBPressed = buttonPressed_B; controllerEventArgs.BtnBTouched = buttonTouched_B;
controllerEventArgs.BtnXPressed = buttonPressed_X; controllerEventArgs.BtnXTouched = buttonTouched_X; controllerEventArgs.BtnYPressed = buttonPressed_Y; controllerEventArgs.BtnYTouched = buttonTouched_Y;
controllerEventArgs.leftJoystickAngle = leftJoyStickAngle; controllerEventArgs.rightJoystickAngle = rightJoyStickAngle;
controllerEventArgs.leftTriggerValue = leftTriggerValue; controllerEventArgs.rightTriggerValue = rightTriggerValue;
controllerEventArgs.leftGripValue = leftGripValue; controllerEventArgs.rightGripValue = rightGripValue;
OnControllerValueChanged?.Invoke(this, controllerEventArgs); }
public class ControllerEventArgs : EventArgs { public bool BtnAPressed; public bool BtnATouched; public bool BtnBPressed; public bool BtnBTouched;
public bool BtnXPressed; public bool BtnXTouched; public bool BtnYPressed; public bool BtnYTouched;
public Vector2 leftJoystickAngle; public Vector2 rightJoystickAngle;
public float leftTriggerValue; public float rightTriggerValue; public float leftGripValue; public float rightGripValue; } }
|