예제 #1
0
    private void OnEnable()
    {
        bool paddleInLeftHand = System.Convert.ToBoolean(PlayerPrefs.GetInt("PaddleInLeftHand"));

        var controllers = new List <UnityEngine.XR.InputDevice>();

        UnityEngine.XR.InputDevices.GetDevicesAtXRNode(
            paddleInLeftHand ? UnityEngine.XR.XRNode.LeftHand : UnityEngine.XR.XRNode.RightHand,
            controllers
            );

        if (controllers.Count != 1)
        {
            string handType = paddleInLeftHand ? "left" : "right";
            throw new System.Exception($"Error: Found {controllers.Count} {handType} hands.");
        }
        _controller = controllers[0];

        // Make sure the controller supports haptics, or else we can't do vibrations.
        UnityEngine.XR.HapticCapabilities capabilities;
        if (!_controller.TryGetHapticCapabilities(out capabilities) || !capabilities.supportsImpulse)
        {
            this.enabled = false;
        }
    }
    private void AssignControllers()
    {
        if (cType == ControllerType.LeftController)
        {
            var leftHandDevices = new List <UnityEngine.XR.InputDevice>();
            UnityEngine.XR.InputDevices.GetDevicesAtXRNode(UnityEngine.XR.XRNode.LeftHand, leftHandDevices);
            if (leftHandDevices.Count == 1)
            {
                controller = leftHandDevices[0];
            }
        }
        else if (cType == ControllerType.RightController)
        {
            var rightHandDevices = new List <UnityEngine.XR.InputDevice>();
            UnityEngine.XR.InputDevices.GetDevicesAtXRNode(UnityEngine.XR.XRNode.RightHand, rightHandDevices);
            if (rightHandDevices.Count == 1)
            {
                controller = rightHandDevices[0];
            }
        }

        controller.TryGetHapticCapabilities(out capabilities);
    }