当前位置: 首页 > news >正文

【XR手柄交互】Unity 中使用 InputActions 实现手柄控制详解(基于 OpenXR + Unity新输入系统(Input Actions))

摘要:
本文主要介绍如何使用 Input Actions(Unity 新输入系统)+ OpenXR 来实现 VR手柄控制(监听ABXY按钮、摇杆、抓握等操作)

图片为AI生成


🎮 Unity 中使用 InputActions 实现手柄控制详解(基于 OpenXR + 新输入系统)


✅ 一、环境准备

必要组件

  • Unity 2021.3 或以上(推荐使用 LTS 版本)
  • 已启用 XR 插件管理器,并为目标平台启用 OpenXR
  • 安装以下 Package:
    • Input System
    • OpenXR Plugin

启用 OpenXR

Edit > Project Settings > XR Plug-in Management > OpenXR

然后启用 OpenXR 的 Interaction Profiles:

  • OpenXR Feature Group > Interaction Profiles
    • Oculus Touch Controller
    • Valve Index Controller
    • Microsoft Mixed Reality Controller

🧱 二、创建 Input Actions 文件

1. 创建 Action 文件

Assets 文件夹中右键点击:

Create > Input Actions

重命名为:XRControls.inputactions

双击打开,会出现如下结构编辑界面:


2. 添加 Action Map 和 Actions

示例:创建两个 Action Maps
  • XRI LeftHand
  • XRI RightHand
XRI RightHand 中添加这些动作:
Action NameAction TypeControl TypeBinding Path
selectButtonButton<XRController>{RightHand}/trigger
activateButtonButton<XRController>{RightHand}/grip
positionValueVector3<XRController>{RightHand}/devicePosition
rotationValueQuaternion<XRController>{RightHand}/deviceRotation
joystickValueVector2<XRController>{RightHand}/thumbstick
primaryButtonButtonButton<XRController>{RightHand}/primaryButton
secondaryButtonButtonButton<XRController>{RightHand}/secondaryButton

💡 注意:ABXY 按钮在默认的 XRI Default Input Actions 文件中 没有绑定,需要你手动添加绑定。

对于左手 X/Y 按钮,可添加:
  • <XRController>{LeftHand}/primaryButton → X 按钮
  • <XRController>{LeftHand}/secondaryButton → Y 按钮

配置完成后,点击 Save。


🔌 三、绑定 Action 到脚本中监听手柄输入

1. 创建脚本:XRControllerInputListener.cs

using UnityEngine;
using UnityEngine.InputSystem;public class XRControllerInputListener : MonoBehaviour
{[Header("输入绑定")]public InputActionProperty selectAction;public InputActionProperty activateAction;public InputActionProperty joystickAction;public InputActionProperty positionAction;public InputActionProperty rotationAction;[Header("主按钮(ABXY)")]public InputActionProperty rightPrimaryButton;   // A 按钮public InputActionProperty rightSecondaryButton; // B 按钮public InputActionProperty leftPrimaryButton;    // X 按钮public InputActionProperty leftSecondaryButton;  // Y 按钮void OnEnable(){selectAction.action.Enable();activateAction.action.Enable();joystickAction.action.Enable();positionAction.action.Enable();rotationAction.action.Enable();rightPrimaryButton.action.Enable();rightSecondaryButton.action.Enable();leftPrimaryButton.action.Enable();leftSecondaryButton.action.Enable();selectAction.action.performed += OnSelectPressed;activateAction.action.performed += OnGripPressed;rightPrimaryButton.action.performed += ctx => Debug.Log("A 按钮按下");rightPrimaryButton.action.canceled  += ctx => Debug.Log("A 按钮抬起");rightSecondaryButton.action.performed += ctx => Debug.Log("B 按钮按下");leftPrimaryButton.action.performed     += ctx => Debug.Log("X 按钮按下");leftSecondaryButton.action.performed   += ctx => Debug.Log("Y 按钮按下");}void OnDisable(){selectAction.action.performed -= OnSelectPressed;activateAction.action.performed -= OnGripPressed;}void OnSelectPressed(InputAction.CallbackContext ctx){Debug.Log("Trigger pressed");}void OnGripPressed(InputAction.CallbackContext ctx){Debug.Log("Grip pressed");}void Update(){// 摇杆值Vector2 joystick = joystickAction.action.ReadValue<Vector2>();if (joystick.magnitude > 0.1f){Debug.Log($"Joystick: {joystick}");}// 控制器位置Vector3 pos = positionAction.action.ReadValue<Vector3>();Quaternion rot = rotationAction.action.ReadValue<Quaternion>();transform.SetPositionAndRotation(pos, rot);}
}

2. 绑定 InputActionProperty 到 Inspector

选中绑定此脚本的 GameObject,在 Inspector 中:

  • 展开每个字段(例如 rightPrimaryButton
  • 选择 InputActionAsset 中的:
    • XRI RightHand/primaryButton
    • XRI RightHand/secondaryButton
    • XRI LeftHand/primaryButton
    • XRI LeftHand/secondaryButton

📌 常用输入绑定路径(OpenXR)

控制器部位Binding Path
扳机 Trigger<XRController>{RightHand}/trigger
抓握 Grip<XRController>{RightHand}/grip
主按钮 A/B/X/Y<XRController>{RightHand}/primaryButton
摇杆方向<XRController>{RightHand}/thumbstick
摇杆点击<XRController>{RightHand}/thumbstickClick
控制器位置<XRController>{RightHand}/devicePosition
控制器旋转<XRController>{RightHand}/deviceRotation

✅ 如果仍想使用默认 XRIActions 文件怎么办?

可以这样做:

  1. 复制一份 XRI Default Input Actions.inputactions
  2. 重命名为你自己的(比如 XRControls.inputactions
  3. 添加上述的 ABXY Action
  4. Project Settings > XR Interaction Toolkit 中替换为你的版本

✅ 总结

通过 Unity 的 Input System + OpenXR,可以优雅地实现对手柄各种输入(按钮、摇杆、位置等)的监听和处理。其优势是:

  • 自动适配各种头显(Oculus、Index、Pico)
  • 支持多平台一致性
  • 易于拓展和可视化调试

同时补充支持 ABXY 按钮,只需扩展绑定路径与监听逻辑即可,无需重新实现控制器系统。

相关文章:

  • Windows环境下常用网络命令使用
  • SIEMENS PLC程序解读 ST 语言 车型识别
  • C++面试复习日记(8)2025.4.25,malloc,free和new,delete的区别
  • HDRnet——双边滤波和仿射变换的摇身一变
  • vite+vue构建的网站项目localhost:5173打不开
  • MYSQL之数据类型
  • 从多类缺陷到高良率跃升|公差分析技术重构动力电池装配精度体系
  • Golang | HashMap实现原理
  • electron-builder 打包安装与启动手动安装,最终解决方案,之前的文章与其他的人都不用看了。
  • 面向对象编程核心:封装、继承、多态与 static 关键字深度解析
  • 使用 uv 工具快速创建 MCP 服务(Trae 配置并调用 MCP 服务)
  • 百度Create2025 AI开发者大会:模型与应用的未来已来
  • 【HTTP/2和HTTP/3的应用现状:看不见的革命】
  • Linux驱动开发快速上手指南:从理论到实战
  • 大内存生产环境tomcat-jvm配置实践
  • 常见网络安全攻击类型深度剖析(四):跨站脚本攻击(XSS)——分类、漏洞利用与前端安全防护
  • 《100天精通Python——基础篇 2025 第3天:变量与数据类型全面解析,掌握Python核心语法》
  • Ubuntu 下 Nginx 1.28.0 源码编译安装与 systemd 管理全流程指南
  • Java大师成长计划之第3天:Java中的异常处理机制
  • 第3讲:ggplot2完美入门与美化细节打磨——从基础绘制到专业级润色
  • 中青报:“猿辅导员工猝死”事件上热搜,是对健康职场环境的共同关切
  • 全国党委和政府秘书长会议在京召开,蔡奇出席并讲话
  • 还山记——走进山水、感受山水艺术的魅力
  • 美媒称特朗普考虑大幅下调对华关税、降幅或超一半,外交部回应
  • 百位名人写“茶”字,莫言王蒙贾平凹都写了
  • 世界最大全电驱可拆装环保绞吸船投入官厅水库清淤试点工程