0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

InputSystemの備忘録[Unity]

Posted at

目次

・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");
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?