Unity在XR设备中获取手柄的按键信息

我们在平常的XR设备开发中,尤其适用VR设备的时候,会用到手柄的操作。

我们知道Oculus SDK提供了OVRInput,能够获取得到手柄的按键信息。

// public variable that can be set to LTouch or RTouch in the Unity Inspector
public Controller controller;

// returns a float of the Hand Trigger’s current state on the Oculus Touch controller
// specified by the controller variable.
OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, controller);

// returns true if the primary button (“A” or “X”) is pressed on the Oculus Touch controller
// specified by the controller variable.
OVRInput.Get(OVRInput.Button.One, controller);

因为我们开发的应用肯定不单单只在oculus上运行,如果在其他的VR设备上运行的话,那要对接各个VR设备的SDK吗?

不用慌,其实Untiy已经把这个事情帮我做了,Unity封装了一下XR Input接口,就是抽象出来一层,能够在各种VR设备上使用。

那如何使用呢?我们先看看Unity封装的各个按键的对应关系。已经支持了很多市面上的VR设备。主要还是Oculus。你可以去官网上进行参考:Unity - Manual: Unity XR Input

我对使用方法进行了封装,挂载GameObject上就可以使用了。大家可以自行使用啊。

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.XR;

[System.Serializable]
public enum InputFeature
{
    //
    // ժҪ:
    //     The primary face button being pressed on a device, or sole button if only one
    //     is available.
    primaryButton,
    //
    // ժҪ:
    //     The primary face button being touched on a device.
    primaryTouch,
    //
    // ժҪ:
    //     The secondary face button being pressed on a device.
    secondaryButton,
    //
    // ժҪ:
    //     The secondary face button being touched on a device.
    secondaryTouch,
    //
    // ժҪ:
    //     A binary measure of whether the device is being gripped.
    gripButton,
    //
    // ժҪ:
    //     A binary measure of whether the index finger is activating the trigger.
    triggerButton,
    //
    // ժҪ:
    //     Represents a menu button, used to pause, go back, or otherwise exit gameplay.
    menuButton,
    //
    // ժҪ:
    //     Represents the primary 2D axis being clicked or otherwise depressed.
    primary2DAxisClick,
    //
    // ժҪ:
    //     Represents the primary 2D axis being touched.
    primary2DAxisTouch,
    //
    // ժҪ:
    //     Represents the secondary 2D axis being clicked or otherwise depressed.
    secondary2DAxisClick,
    //
    // ժҪ:
    //     Represents the secondary 2D axis being touched.
    secondary2DAxisTouch,
    //
    // ժҪ:
    //     Use this property to test whether the user is currently wearing and/or interacting
    //     with the XR device. The exact behavior of this property varies with each type
    //     of device: some devices have a sensor specifically to detect user proximity,
    //     however you can reasonably infer that a user is present with the device when
    //     the property is UserPresenceState.Present.
    userPresence
}

public class XRControllerButtonWatcher : MonoBehaviour
{
    private Dictionary<InputFeature, InputFeatureUsage<bool>> inputFeatureUsageMap = new Dictionary<InputFeature, InputFeatureUsage<bool>>() {
        { InputFeature.primaryButton, CommonUsages.primaryButton },
        { InputFeature.primaryTouch, CommonUsages.primaryTouch },
        { InputFeature.secondaryButton, CommonUsages.secondaryButton },
        { InputFeature.secondaryTouch, CommonUsages.secondaryTouch },
        { InputFeature.gripButton, CommonUsages.gripButton },
        { InputFeature.triggerButton, CommonUsages.triggerButton },
        { InputFeature.menuButton, CommonUsages.menuButton },
        { InputFeature.primary2DAxisClick, CommonUsages.primary2DAxisClick },
        { InputFeature.primary2DAxisTouch, CommonUsages.primary2DAxisTouch },
        { InputFeature.secondary2DAxisClick, CommonUsages.secondary2DAxisClick },
        { InputFeature.secondary2DAxisTouch, CommonUsages.secondary2DAxisTouch },
        { InputFeature.userPresence, CommonUsages.userPresence }
    };

    [SerializeField]
    public InputFeature ButtonInputFeature;

    [SerializeField]
    public InputDeviceCharacteristics inputDeviceType = InputDeviceCharacteristics.None;

    [SerializeField]
    public UnityEvent OnInputDown;

    [SerializeField]
    public UnityEvent OnInputUp;

    private InputFeatureUsage<bool> buttonInputFeatureUsage;

    private bool lastButtonState = false;
    private List<InputDevice> devicesButton;

    private void Awake()
    {
        if (OnInputDown == null)
            OnInputDown = new UnityEvent();
        if (OnInputUp == null)
            OnInputUp = new UnityEvent();

        devicesButton = new List<InputDevice>();

        if (!inputFeatureUsageMap.TryGetValue(ButtonInputFeature, out buttonInputFeatureUsage))
        {
            Debug.LogError("not found inputFeature: " + ButtonInputFeature);
        }
    }

    private void OnEnable()
    {
        Init();
    }

    private void OnDisable()
    {
        InputDevices.deviceConnected -= InputDevices_deviceConnected;
        InputDevices.deviceDisconnected -= InputDevices_deviceDisconnected;
        devicesButton.Clear();
    }

    private void InputDevices_deviceConnected(InputDevice device)
    {
        bool discardedValue;
        if (device.TryGetFeatureValue(buttonInputFeatureUsage, out discardedValue))
        {
            Debug.Log($"add device: {device.name}");
            devicesButton.Add(device); // Add any devices that have a primary button.
        }
    }

    private void InputDevices_deviceDisconnected(InputDevice device)
    {
        if (devicesButton.Contains(device))
            devicesButton.Remove(device);
    }

    private void Update()
    {
        bool tempState = false;
        foreach (var device in devicesButton)
        {
            bool primaryButtonState = false;
            tempState = device.TryGetFeatureValue(buttonInputFeatureUsage, out primaryButtonState) // did get a value
                        && primaryButtonState // the value we got
                        || tempState; // cumulative result from other controllers
        }

        if (tempState != lastButtonState) // Button state changed since last frame
        {
            if (tempState)
                OnInputDown?.Invoke();
            else
                OnInputUp?.Invoke();

            lastButtonState = tempState;
        }
    }

    public void Init()
    {
        List<InputDevice> allDevices = new List<InputDevice>();
        if (inputDeviceType == InputDeviceCharacteristics.None)
            InputDevices.GetDevices(allDevices);
        else
            InputDevices.GetDevicesWithCharacteristics(inputDeviceType, allDevices);

        foreach (InputDevice device in allDevices)
            InputDevices_deviceConnected(device);

        InputDevices.deviceConnected += InputDevices_deviceConnected;
        InputDevices.deviceDisconnected += InputDevices_deviceDisconnected;
    }
}

使用Unity自带的XR Input可以很好的支持多种设备。不需要再一个一个对接各个VR设备的SDK中的Input了。

参考文档:

Unity - Manual: Unity XR Input

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
以下是在 Unity 引擎下检测 VR 手柄按键的代码示例: ```csharp using UnityEngine; using UnityEngine.XR; public class VRControllerInput : MonoBehaviour { private InputDevice device; private void Start() { // 获取手柄 InputDeviceCharacteristics leftControllerFilter = InputDeviceCharacteristics.Left | InputDeviceCharacteristics.Controller; device = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand); // 获取手柄 // InputDeviceCharacteristics rightControllerFilter = InputDeviceCharacteristics.Right | InputDeviceCharacteristics.Controller; // device = InputDevices.GetDeviceAtXRNode(XRNode.RightHand); } private void Update() { // 检测按键 bool triggerButtonPressed = false; bool gripButtonPressed = false; bool menuButtonPressed = false; device.TryGetFeatureValue(CommonUsages.triggerButton, out triggerButtonPressed); device.TryGetFeatureValue(CommonUsages.gripButton, out gripButtonPressed); device.TryGetFeatureValue(CommonUsages.menuButton, out menuButtonPressed); // 处理按键事件 if (triggerButtonPressed) { Debug.Log("Trigger button pressed."); } if (gripButtonPressed) { Debug.Log("Grip button pressed."); } if (menuButtonPressed) { Debug.Log("Menu button pressed."); } } } ``` 该代码片段,我们使用 `InputDevices.GetDeviceAtXRNode()` 方法获取手柄。如果需要获取手柄,只需将注释切换到获取手柄的代码即可。接着,我们在 `Update()` 方法检测手柄按键是否被按下,如果是,则输出相应的日志信息。在这个示例,我们检测了触发器、握持和菜单按钮的状态。你可以根据自己的需要添加或删除检测的按键
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XR风云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值