摘要:
本文主要介紹如何使用 Input Actions(Unity 新輸入系統)+ OpenXR 來實現 VR手柄控制(監聽ABXY按鈕、搖桿、抓握等操作)。
🎮 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 Name | Action Type | Control Type | Binding Path |
---|---|---|---|
select | Button | Button | <XRController>{RightHand}/trigger |
activate | Button | Button | <XRController>{RightHand}/grip |
position | Value | Vector3 | <XRController>{RightHand}/devicePosition |
rotation | Value | Quaternion | <XRController>{RightHand}/deviceRotation |
joystick | Value | Vector2 | <XRController>{RightHand}/thumbstick |
primaryButton | Button | Button | <XRController>{RightHand}/primaryButton |
secondaryButton | Button | Button | <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 文件怎么辦?
可以這樣做:
- 復制一份
XRI Default Input Actions.inputactions
- 重命名為你自己的(比如
XRControls.inputactions
) - 添加上述的 ABXY Action
- 在
Project Settings > XR Interaction Toolkit
中替換為你的版本
? 總結
通過 Unity 的 Input System + OpenXR,可以優雅地實現對手柄各種輸入(按鈕、搖桿、位置等)的監聽和處理。其優勢是:
- 自動適配各種頭顯(Oculus、Index、Pico)
- 支持多平臺一致性
- 易于拓展和可視化調試
同時補充支持 ABXY 按鈕,只需擴展綁定路徑與監聽邏輯即可,無需重新實現控制器系統。