ニュース

Top  >   16. Input Systemとイベント  >   Q, コントローラーのボタン入力を取得したい

Q, コントローラーのボタン入力を取得したい

2026/03/18

メインコンテンツへスキップ
< すべてのカテゴリに戻る

A,結論

Unity 6 では Input System と OpenXR の組み合わせを基本にし、ボタン入力は Input Action で取得するのが安全です。

原因 / 背景

  • デバイスごとの生 API を直接読むより、Action ベースで組む方が Quest、PC VR、将来の XR デバイスへ展開しやすくなります。
  • Interaction Profile を切り替えても同じアクションを使い回しやすい点が大きな利点です。

実装 / 手順

  1. Input Actions アセットを作成し、Press 型のアクションにトリガーや A/B ボタンなどを割り当てます。
  2. OpenXR の Interaction Profile を有効にし、PlayerInput または InputActionReference から参照します。
  3. 押下、保持、離した瞬間を分けて扱うと UI 操作とゲームプレイ処理を整理しやすくなります。

サンプルコード

using UnityEngine;
using UnityEngine.InputSystem;

public class XRActionButtonReader : MonoBehaviour
{
    [SerializeField] private InputActionReference actionReference;
    [SerializeField] private string debugLabel = "Action";

    private void OnEnable()
    {
        if (actionReference != null && actionReference.action != null)
        {
            actionReference.action.Enable();
        }
    }

    private void OnDisable()
    {
        if (actionReference != null && actionReference.action != null)
        {
            actionReference.action.Disable();
        }
    }

    private void Update()
    {
        if (actionReference == null || actionReference.action == null)
        {
            return;
        }

        if (actionReference.action.WasPressedThisFrame())
        {
            Debug.Log($"{debugLabel} pressed");
        }

        if (actionReference.action.IsPressed())
        {
            // 長押し中の処理
        }

        if (actionReference.action.WasReleasedThisFrame())
        {
            Debug.Log($"{debugLabel} released");
        }
    }
}

※ 実機確認が必要な項目は、Editor だけで判断せず対象デバイスでも必ず動作確認してください。

難易度:Level 2(初心者)
対象プラットフォーム:PC,Android,iOS,VR,Unity
関連キーワード:canvas,EventSystem,input system,ugui,コントローラー,ボタン,入力,判定

Level 1-2 向けバナー