Unity XR 设置VR设备手柄按键按下事件

一、Unity设置

1、导入XR Interaction Toolkit插件,导入示例资源(如下图)。

2、设置新版XR输入事件

①打开XRI Default Input Action 面板。

②设置左手柄上的按键就点击Action Maps 列表下的 XRI LeftHand Interaction选项,设置右手柄上的按键就点击XRI RightHand Interaction。

③以设置右手柄上的按键为例,我们将设置右手柄上的 A键、B键、摇杆按下键、摇杆上下左右推动事件、R2键(扳机键)、侧柄键(抓握键)等6个按键的绑定事件方法。

首先,点击Action列表右上方的+号新建事件,将事件命名为按键名称。

④命名完成后为每个事件绑定对应的手柄按钮。

根据下面的图依次选择 XR Controller、XR Controller(RightHand)、Usage中对应的按钮。

按键对应名称
A键PrimaryButton
B键SecondaryButton
X键PrimaryButton
Y键SecondaryButton
扳机键(R2键)TirggerButton
抓握键(侧柄键)GripButton
摇杆按下键Primary2DAxisClick
摇杆上推键Primary2DAxis
摇杆下推键Primary2DAxis
摇杆左推键Primary2DAxis
摇杆右推键Primary2DAxis

全部添加完成后如下图(本图只设置右手柄按键,所以不包含XY按键):

⑤设置触发方式,这里有一个注意点,就是ABXY键、扳机键、侧柄键和摇杆中心键都是通过按下触发的,但是摇杆上下左右四个方向的键是通过推动的方式触发的,所以在设置的时候要区分开来。

ABXY键、扳机键、侧柄键和摇杆中心键都是选中Action列表下的对应选项设置 Press,每一个事件上都要设置。选项下的按钮可以不设置,但如果后面测试没反应,可以在按扭上添加试一下。

如下图:

摇杆上下左右四个方向的键是选择选项下的对应按键设置Sector。在 Sector 模块下的Directions选项中选择对应的摇杆方向,向上推就是North,向下推就是South,向左推就是West,向右推就是East,和看地图一样 上北下南左西右东 。每一个都要设置对应的方向,注意不要多选。 

如下图:

⑥最后要记得点击保存!!!

保存后就能在XRI Default Input Actions中看到相应的按钮事件项了。

二、代码编写

1、公开按钮变量

2、在Update中每帧检测

完整代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class HandControllerTest : MonoBehaviour
{
    public InputActionReference tirgger_Action;
    public InputActionReference grip_Action;
    public InputActionReference pressA_Action;
    public InputActionReference pressB_Action;
    public InputActionReference pushUp_Action;
    public InputActionReference pushDown_Action;
    public InputActionReference pushLeft_Action;
    public InputActionReference pushRight_Action;
    public InputActionReference pressRocker_Action;

    // Update is called once per frame
    void Update()
    {
        if (pressA_Action.action.WasPerformedThisFrame())
        {
            Debug.Log("A键");
        }
        if (pressB_Action.action.WasPerformedThisFrame())
        {
            Debug.Log("B键");
        }
        if (tirgger_Action.action.WasPerformedThisFrame())
        {
            Debug.Log("扳机键");
        }
        if (grip_Action.action.WasPerformedThisFrame())
        {
            Debug.Log("抓握键");
        }

        if (pushUp_Action.action.WasPerformedThisFrame())
        {
            Debug.Log("上推");
        }
        if (pushDown_Action.action.WasPerformedThisFrame())
        {
            Debug.Log("下推");
        }
        if (pushLeft_Action.action.WasPerformedThisFrame())
        {
            Debug.Log("左推");
        }
        if (pushRight_Action.action.WasPerformedThisFrame())
        {
            Debug.Log("右推");
        }
        if (pressRocker_Action.action.WasPerformedThisFrame())
        {
            Debug.Log("按下摇杆键");
        }

    }
}

3、外部赋值

这样就可以了,运行设备测试看看吧!

4、测试结果

5、第二种代码编写方式

使用注册事件的方式添加,可根据个人需求使用。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class HandControllerTest : MonoBehaviour
{
    public InputActionReference tirgger_Action;

    private void OnEnable()
    {
        SetupInteractorEvents();
    }
    private void OnDisable()
    {
        TeardownInteractorEvents();
    }
    void SetupInteractorEvents()
    {
        var teleportModeActivateAction = GetInputAction(tirgger_Action);
        if (teleportModeActivateAction != null)
        {
            teleportModeActivateAction.performed += OnDownTirggerAction;
        }
    }
    void TeardownInteractorEvents()
    {
        var teleportModeActivateAction = GetInputAction(tirgger_Action);
        if (teleportModeActivateAction != null)
        {
            teleportModeActivateAction.performed -= OnDownTirggerAction;
        }

    }

    private void OnDownTirggerAction(InputAction.CallbackContext context)
    {
        Debug.Log("按下扳机键");
    }

    static InputAction GetInputAction(InputActionReference actionReference)
    {
#pragma warning disable IDE0031 // Use null propagation -- Do not use for UnityEngine.Object types
        return actionReference != null ? actionReference.action : null;
#pragma warning restore IDE0031
    }

三、常见问题排查

1、检查手柄是否开机,是否正常连接至电脑,是否有电。

2、检查Derived Binding下是否有警告标识,如果有警告标识,则重新选择一下,或者从Usage选项切换到Optional Controls选项。 如下图:

3、检查触发事件是不是未添加或者添加位置错误。

  • 13
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
以下是在 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()` 方法中检测手柄按键是否被按下,如果是,则输出相应的日志信息。在这个示例中,我们检测了触发器、握持和菜单按钮的状态。你可以根据自己的需要添加或删除检测的按键

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

十画_824

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

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

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

打赏作者

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

抵扣说明:

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

余额充值