MRが楽しい

MRやVRについて学習したことを書き残す

MagicLeap2のコントローラのボタン押下を検出する

本日は MagicLeap2 の小ネタ枠です。
MagicLeap2 のコントローラのボタン押下を検出する方法について記事にします。

前提条件

MagicLeap2 向け MRTK パッケージを利用すると、ハンドトラッキングとコントローラの動的切り替えを簡単に設定できます。
以下の記事を参考に、MagicLeap2 向け MRTK のインポートを実施してください。
bluebirdofoz.hatenablog.com

コントローラのボタン操作イベント

MagicLeap2 向け MRTK 内でコントローラの各種ボタン操作イベントが以下の通り、定義されています。
・MagicLeapMRTKController.cs

        public override MixedRealityInteractionMapping[] DefaultLeftHandedInteractions => new[]
        {
            new MixedRealityInteractionMapping(0, "Spatial Pointer", AxisType.SixDof, DeviceInputType.SpatialPointer),
            new MixedRealityInteractionMapping(1, "Select", AxisType.Digital, DeviceInputType.ButtonPress),
            new MixedRealityInteractionMapping(2, "Bumper Press", AxisType.Digital, DeviceInputType.ButtonPress),
            new MixedRealityInteractionMapping(3, "Menu", AxisType.Digital, DeviceInputType.ButtonPress),
            new MixedRealityInteractionMapping(4, "Touchpad Touch", AxisType.Digital, DeviceInputType.TouchpadTouch),
            new MixedRealityInteractionMapping(5, "Touchpad Position", AxisType.DualAxis, DeviceInputType.Touchpad),
            new MixedRealityInteractionMapping(6, "Touchpad Press", AxisType.SingleAxis, DeviceInputType.TouchpadPress),
        };

        public override MixedRealityInteractionMapping[] DefaultRightHandedInteractions => new[]
        {
            new MixedRealityInteractionMapping(0, "Spatial Pointer", AxisType.SixDof, DeviceInputType.SpatialPointer),
            new MixedRealityInteractionMapping(1, "Select", AxisType.Digital, DeviceInputType.ButtonPress),
            new MixedRealityInteractionMapping(2, "Bumper Press", AxisType.Digital, DeviceInputType.ButtonPress),
            new MixedRealityInteractionMapping(3, "Menu", AxisType.Digital, DeviceInputType.ButtonPress),
            new MixedRealityInteractionMapping(4, "Touchpad Touch", AxisType.Digital, DeviceInputType.TouchpadTouch),
            new MixedRealityInteractionMapping(5, "Touchpad Position", AxisType.DualAxis, DeviceInputType.Touchpad),
            new MixedRealityInteractionMapping(6, "Touchpad Press", AxisType.SingleAxis, DeviceInputType.TouchpadPress),
        };

このため、例えばバンパーボタンの押下イベントを受け取りたい場合は "Bumper Press" インプットアクションを受け取るだけで検出が可能です。

実装例

MRTK のプロファイルを MagicLeap2 用のプロファイルに切り替えます。
本プロファイルには MagicLeap2 用の InputAction の定義とコントローラの関連付けが行われています。

適当なオブジェクトに InputActionHandler コンポーネントを追加します。

今回はプレイヤーがどこを見ていてもバンパーボタンの押下イベントを受け取りたいので[Is Focus Required]のチェックを外して、オブジェクトを注視していないときでもイベントが発生するようにします。

[InputAction]で[Bumper Press]を選択します。本 InputAction がバンパーボタンの押下イベントになります。

[On Input Action Started]にイベントを登録すると、バンパーボタン押下時に指定のイベントが実行されます。