はじめに
XR Interaction ToolkitでコントローラーのA/B、X/Yボタン入力に対応する方法を調べた際のメモです。
開発環境
- PICO4 Ultra (Questなどでも動作すると思われます)
- Unity 6000.0.48f1
- XR Interaction Toolkit 3.2.0
サンプルについて
このサンプルは、PICO 4 UltraでA/B、X/Yボタンの入力を取得し、UnityEventとして処理できるシンプルなシステムです。
スクリプトを書かずにInspectorだけで動作を設定できるのが特徴です。
動作の仕組み
Input Action Asset: 各ボタンの物理的な入力をUnityが理解できる形式に変換
XRInputButtonReader: XR Interaction Toolkit v3.3の新しい入力システム
UnityEvent: Inspector上で動作を設定できるイベントシステム
Input Actions
Project Tabで 右クリックメニュー > Create > Input Actions
名前をButton Input Actionsとし、以下の内容でActionsを設定する。
Actions Maps : XR Controller Input
Actions:A Button
+ボタンでBindingを追加し、Bindingに PrimaryButton [RightHand Controller]
を入力して選択。
A,B,X,Y全てのボタンのBindingは以下のとおりです:
- A Button:
PrimaryButton [RightHand Controller]
- B Button:
SecondaryButton [RightHand Controller]
- X Button:
PrimaryButton [LeftHand Controller]
- Y Button:
SecondaryButton [LeftHand Controller]
ソースコード
XRControllerButtonEvents.csファイルを作成し、以下の内容をコピーして保存してください。
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.XR.Interaction.Toolkit.Inputs.Readers;
public class XRControllerButtonEvents : MonoBehaviour
{
[Header("XR Button Input Readers")]
[SerializeField] private XRInputButtonReader aButtonInput = new XRInputButtonReader("A Button");
[SerializeField] private XRInputButtonReader bButtonInput = new XRInputButtonReader("B Button");
[SerializeField] private XRInputButtonReader xButtonInput = new XRInputButtonReader("X Button");
[SerializeField] private XRInputButtonReader yButtonInput = new XRInputButtonReader("Y Button");
[Header("Button Events")]
public UnityEvent OnAButtonPressed;
public UnityEvent OnBButtonPressed;
public UnityEvent OnXButtonPressed;
public UnityEvent OnYButtonPressed;
public UnityEvent OnAButtonReleased;
public UnityEvent OnBButtonReleased;
public UnityEvent OnXButtonReleased;
public UnityEvent OnYButtonReleased;
void Start()
{
aButtonInput.EnableDirectActionIfModeUsed();
bButtonInput.EnableDirectActionIfModeUsed();
xButtonInput.EnableDirectActionIfModeUsed();
yButtonInput.EnableDirectActionIfModeUsed();
}
void Update()
{
// ボタンが押された瞬間をチェック
if (aButtonInput.ReadWasPerformedThisFrame())
{
OnAButtonPressed?.Invoke();
}
if (aButtonInput.ReadWasCompletedThisFrame())
{
OnAButtonReleased?.Invoke();
}
if (bButtonInput.ReadWasPerformedThisFrame())
{
OnBButtonPressed?.Invoke();
}
if (bButtonInput.ReadWasCompletedThisFrame())
{
OnBButtonReleased?.Invoke();
}
if (xButtonInput.ReadWasPerformedThisFrame())
{
OnXButtonPressed?.Invoke();
}
if (xButtonInput.ReadWasCompletedThisFrame())
{
OnXButtonReleased?.Invoke();
}
if (yButtonInput.ReadWasPerformedThisFrame())
{
OnYButtonPressed?.Invoke();
}
if (yButtonInput.ReadWasCompletedThisFrame())
{
OnYButtonReleased?.Invoke();
}
}
void OnDestroy()
{
aButtonInput.DisableDirectActionIfModeUsed();
bButtonInput.DisableDirectActionIfModeUsed();
xButtonInput.DisableDirectActionIfModeUsed();
yButtonInput.DisableDirectActionIfModeUsed();
}
}
シーン設定
XRControllerButtonEvents の設定
UnityEventになっていますので、Inspectorで設定可能です。
GameObjectにXRControllerButtonEventsを追加し、XR Button Input Readersに各ButtonのActionを設定。
Button Eventsにボタンを押した/離した時に実行したいイベントを登録してください。
InputActionManagerへの登録
これを忘れると、InputActionは機能しません。
InputActionsコンポーネントのActions Assetsに今回作成したButtonInputActionsを登録してください。
XROriginに既にInputActionManagerが設定されている場合、Actions Assetsに追加で登録すればOKです。
おわりに
設定は以上です。
基本的な内容ですが、普段サンプル頼りなのでちゃんと抑えておきたい所です。
参考