// Start is called before the first frame update
    void Start()
    {
        // Reset the controller values.
        Reset();

        // Validate the input device.
        InputDevice inputDevice = ControllerDevice;

        inputDevice.isValid = true;
        ControllerDevice    = inputDevice;
    }
    // Update is called once per frame
    void Update()
    {
        // Update the input device.
        InputDevice inputDevice = ControllerDevice;

        inputDevice.isValid            = true;
        inputDevice.primary2DAxis      = Primary2DAxis.Axis;
        inputDevice.primary2DAxisTouch = Primary2DAxis.Touch;
        inputDevice.primary2DAxisClick = Primary2DAxis.Click;
        inputDevice.trigger            = Trigger.Axis;
        inputDevice.triggerButton      = Trigger.Button;
        inputDevice.grip          = Grip.Axis;
        inputDevice.gripButton    = Grip.Button;
        inputDevice.primaryButton = PrimaryButton.Button;
        ControllerDevice          = inputDevice;
    }
    public static bool IsPressed(this UnityEngine.XR.Interaction.Toolkit.InputDevice device, Button button, out bool isPressed, float pressThreshold = -1.0f)
    {
        if ((int)button >= s_ButtonData.Length)
        {
            throw new ArgumentException("[InputHelpers.IsPressed] The value of <button> is out or the supported range.");
        }

        if (!device.isValid)
        {
            isPressed = false;
            return(false);
        }

        ButtonInfo info = s_ButtonData[(int)button];

        switch (info.type)
        {
        case ButtonReadType.Binary:
        {
            if (device.TryGetFeatureValue(new InputFeatureUsage <bool>(info.name), out bool value))
            {
                isPressed = value;
                return(true);
            }
        }
        break;

        case ButtonReadType.Axis1D:
        {
            if (device.TryGetFeatureValue(new InputFeatureUsage <float>(info.name), out float value))
            {
                float threshold = (pressThreshold >= 0.0f) ? pressThreshold : s_DefaultPressThreshold;
                isPressed = value >= threshold;
                return(true);
            }
        }
        break;

        case ButtonReadType.Axis2DUp:
        {
            if (device.TryGetFeatureValue(new InputFeatureUsage <Vector2>(info.name), out Vector2 value))
            {
                float threshold = (pressThreshold >= 0.0f) ? pressThreshold : s_DefaultPressThreshold;
                isPressed = value.y >= threshold;
                return(true);
            }
        }
        break;

        case ButtonReadType.Axis2DDown:
        {
            if (device.TryGetFeatureValue(new InputFeatureUsage <Vector2>(info.name), out Vector2 value))
            {
                float threshold = (pressThreshold >= 0.0f) ? pressThreshold : s_DefaultPressThreshold;
                isPressed = value.y <= -threshold;
                return(true);
            }
        }
        break;

        case ButtonReadType.Axis2DLeft:
        {
            if (device.TryGetFeatureValue(new InputFeatureUsage <Vector2>(info.name), out Vector2 value))
            {
                float threshold = (pressThreshold >= 0.0f) ? pressThreshold : s_DefaultPressThreshold;
                isPressed = value.x <= -threshold;
                return(true);
            }
        }
        break;

        case ButtonReadType.Axis2DRight:
        {
            if (device.TryGetFeatureValue(new InputFeatureUsage <Vector2>(info.name), out Vector2 value))
            {
                float threshold = (pressThreshold >= 0.0f) ? pressThreshold : s_DefaultPressThreshold;
                isPressed = value.x >= threshold;
                return(true);
            }
        }
        break;

        default:
            break;
        }
        isPressed = false;
        return(false);
    }
示例#4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="deviceNode">Device from which take the input</param>
 internal InputDeviceWrapper(XRNode deviceNode)
 {
     m_inputDevice     = InputDevices.GetDeviceAtXRNode(deviceNode);
     this.m_deviceNode = deviceNode;
     this.m_isSteamVR  = m_inputDevice.subsystem != null && m_inputDevice.subsystem.SubsystemDescriptor.id == "OpenVR Input";
 }
        string GetFeatureValue(InputDevice device, InputFeatureUsage featureUsage)
        {
            switch (featureUsage.type.ToString())
            {
            case "System.Boolean":
                bool boolValue;
                if (device.TryGetFeatureValue(featureUsage.As <bool>(), out boolValue))
                {
                    return(boolValue.ToString());
                }
                break;

            case "System.UInt32":
                uint uintValue;
                if (device.TryGetFeatureValue(featureUsage.As <uint>(), out uintValue))
                {
                    return(uintValue.ToString());
                }
                break;

            case "System.Single":
                float floatValue;
                if (device.TryGetFeatureValue(featureUsage.As <float>(), out floatValue))
                {
                    return(floatValue.ToString());
                }
                break;

            case "UnityEngine.Vector2":
                Vector2 Vector2Value;
                if (device.TryGetFeatureValue(featureUsage.As <Vector2>(), out Vector2Value))
                {
                    return(Vector2Value.ToString());
                }
                break;

            case "UnityEngine.Vector3":
                Vector3 Vector3Value;
                if (device.TryGetFeatureValue(featureUsage.As <Vector3>(), out Vector3Value))
                {
                    return(Vector3Value.ToString());
                }
                break;

            case "UnityEngine.Quaternion":
                Quaternion QuaternionValue;
                if (device.TryGetFeatureValue(featureUsage.As <Quaternion>(), out QuaternionValue))
                {
                    return(QuaternionValue.ToString());
                }
                break;

            case "UnityEngine.XR.Hand":
                Hand HandValue;
                if (device.TryGetFeatureValue(featureUsage.As <Hand>(), out HandValue))
                {
                    return(HandValue.ToString());
                }
                break;

            case "UnityEngine.XR.Bone":
                Bone BoneValue;
                if (device.TryGetFeatureValue(featureUsage.As <Bone>(), out BoneValue))
                {
                    Vector3    bonePosition;
                    Quaternion boneRotation;
                    if (BoneValue.TryGetPosition(out bonePosition) && BoneValue.TryGetRotation(out boneRotation))
                    {
                        return(string.Format("{0}, {1}", bonePosition.ToString(), boneRotation.ToString()));
                    }
                }
                break;

            case "UnityEngine.XR.Eyes":
                Eyes EyesValue;
                if (device.TryGetFeatureValue(featureUsage.As <Eyes>(), out EyesValue))
                {
                    Vector3 fixation, left, right;
                    float   leftOpen, rightOpen;
                    if (EyesValue.TryGetFixationPoint(out fixation) &&
                        EyesValue.TryGetLeftEyePosition(out left) &&
                        EyesValue.TryGetRightEyePosition(out right) &&
                        EyesValue.TryGetLeftEyeOpenAmount(out leftOpen) &&
                        EyesValue.TryGetRightEyeOpenAmount(out rightOpen))
                    {
                        return(string.Format("{0}, {1}, {2}, {3}, {4}", fixation.ToString(), left.ToString(), right.ToString(), leftOpen, rightOpen));
                    }
                }
                break;
            }

            return("");
        }
示例#6
0
        static string GetFeatureValue(InputDevice device, InputFeatureUsage featureUsage)
        {
            switch (featureUsage.type.ToString())
            {
            case "System.Boolean":
                if (device.TryGetFeatureValue(featureUsage.As <bool>(), out var boolValue))
                {
                    return(boolValue.ToString());
                }
                break;

            case "System.UInt32":
                if (device.TryGetFeatureValue(featureUsage.As <uint>(), out var uintValue))
                {
                    return(uintValue.ToString());
                }
                break;

            case "System.Single":
                if (device.TryGetFeatureValue(featureUsage.As <float>(), out var floatValue))
                {
                    return(floatValue.ToString());
                }
                break;

            case "UnityEngine.Vector2":
                if (device.TryGetFeatureValue(featureUsage.As <Vector2>(), out var vector2Value))
                {
                    return(vector2Value.ToString());
                }
                break;

            case "UnityEngine.Vector3":
                if (device.TryGetFeatureValue(featureUsage.As <Vector3>(), out var vector3Value))
                {
                    return(vector3Value.ToString());
                }
                break;

            case "UnityEngine.Quaternion":
                if (device.TryGetFeatureValue(featureUsage.As <Quaternion>(), out var quaternionValue))
                {
                    return(quaternionValue.ToString());
                }
                break;

            case "UnityEngine.XR.Hand":
                if (device.TryGetFeatureValue(featureUsage.As <Hand>(), out var handValue))
                {
                    return(handValue.ToString());
                }
                break;

            case "UnityEngine.XR.Bone":
                if (device.TryGetFeatureValue(featureUsage.As <Bone>(), out var boneValue))
                {
                    if (boneValue.TryGetPosition(out var bonePosition) && boneValue.TryGetRotation(out var boneRotation))
                    {
                        return($"{bonePosition}, {boneRotation}");
                    }
                }
                break;

            case "UnityEngine.XR.Eyes":
                if (device.TryGetFeatureValue(featureUsage.As <Eyes>(), out var eyesValue))
                {
                    if (eyesValue.TryGetFixationPoint(out var fixation) &&
                        eyesValue.TryGetLeftEyePosition(out var left) &&
                        eyesValue.TryGetRightEyePosition(out var right) &&
                        eyesValue.TryGetLeftEyeOpenAmount(out var leftOpen) &&
                        eyesValue.TryGetRightEyeOpenAmount(out var rightOpen))
                    {
                        return($"{fixation}, {left}, {right}, {leftOpen}, {rightOpen}");
                    }
                }
                break;
            }

            return("");
        }