目次
・AssetとMapとActionの違い
・離したタイミングを検知
・callbackの受け取り方
AssetとMapとActionの違い
InputActionsAssetは一つのゲームに一つです。
MapはInputActionsの中にたくさんあります。これはシーン/用途ごとに作ります。
ActionはMapの中の細かい行動を規定します。Mapから推論可能な名前をつけてあげると使いやすいです。
離したタイミングを検知
新しいActionを作ります。
バイナリ系入力検知にはButtonを使います。
InteractionsではPressedを選択します。すると項目が開くので、そこでReleasedOnlyを選びましょう。
どっちもはオススメしません。callbackの受け取り先で入力内容がどちらか正確に判別することは不可能です。
callbackの受け取り方
SerializeFieldでInputActionAssetを受け取ります。
OnEnable関数で該当MapをFindActionMap(string)で探し当てます。
最後にそれをEnableして、それぞれのActionのperformedにcallbackを設定してあげたら完成です。
サンプルコード
using UnityEngine;
using UnityEngine.InputSystem;
namespace Core.Input
{
public class RhythmReader : MonoBehaviour
{
[SerializeField]
private InputActionAsset inputActionAsset;
private InputActionMap _playerActions;
private void OnEnable()
{
_playerActions = inputActionAsset.FindActionMap("Player");
_playerActions.Enable();
_playerActions["RhythmUp"].performed += RhythmUp;
_playerActions["RhythmDown"].performed += RhythmDown;
}
private void OnDisable()
{
_playerActions.Disable();
}
private void RhythmUp(InputAction.CallbackContext context)
{
Debug.Log("Rhythm up");
}
private void RhythmDown(InputAction.CallbackContext context)
{
Debug.Log("Rhythm down");
}
}
}