Unity:InputSystem 最小の覚書
ちょっと使ってみたいだけだったのに悩みまくったから供養 (´・ω・`)
Unityしってる、プログラミングちょとわかる、レベルの記事です
InputSystemをスクリプト10行で動かしてみる
いちばん簡単なやりかた
InputSystem導入
適当にUnityのプロジェクトを作成
PackageManager>Packages:UnityRegistry>InputSystem インストール
Edit>ProjectSettings>InputSystemPackage CrateSettingsAsset
Projectパネル> Assets直下にInputSystem.inputsettingsが追加される

InputActionsを編集
Projectパネル>右クリック>Create> InputActions
作ったInputActionsをダブルクリック エディタが開く
ActionMaps +を押して追加(ActionMap名:Test)
Actions 名前を編集(Action名:Test)
Actions>Path>左上:Listenボタンを押す> 適当なキーを押す
SaveAsset 保存


スクリプトを作る
Projectパネル>右クリック>Create> C#Script
スクリプト名はとりあえず TestScript
TestScriptをダブルクリック VisualStudio(か何か)開くので編集
using UnityEngine;
using UnityEngine.InputSystem;
public class TestScript : MonoBehaviour
{
public void OnTest(InputValue value) //メソッド名は必ず On<Action名> にする
{
Debug.Log("Test");
}
}
最低限、3つ必要
using UnityEngine.InputSystem;
OnTestメソッド(On+さっき作ったアクション名 のメソッド)
OnTestメソッドの引数 InputValue
Sceneに配置
Hierarchy>右クリック> CreateEmpty
Emptyを選択
Inspector>AddComponent> PlayerInputをアタッチ
PlayerInput>Actions ProjectからInputActionsをドロップ
TestScriptもドロップ


実行確認
実行し、Consoleパネルにログが表示されることを確認

アクションマップを切り替えてみる
プレイヤー操作→メニュー操作に切り替えたいときとか
アクションマップ追加
InputActionsをダブルクリック
ActionMaps +を押して追加
Actions 適当に追加
SaveAsset

TestScriptを編集
using UnityEngine;
using UnityEngine.InputSystem;
public class TestScript : MonoBehaviour
{
[SerializeField] private InputActionAsset _input;
private InputActionMap _test;
private InputActionMap _test2;
private void Start()
{
_test = _input.FindActionMap("Test");//アクションマップを取得
_test2 = _input.FindActionMap("Test2");
}
public void OnTest(InputValue value)
{
_test.Disable(); //アクションマップを有効化
_test2.Enable(); //アクションマップを無効化
Debug.Log("Test -> Test2");
}
public void OnTest2(InputValue value)
{
_test.Enable();
_test2.Disable();
Debug.Log("Test2 -> Test");
}
}
追加で必要なもの
InputActionAsset …さっきアクションマップを追加したファイル
InputActionMapx2 …切り替えたいアクションマップ
Start() …アクションマップをここで取得
OnTest(InputValue value) …とりあえずアクションマップを切り替える
OnTest2(InputValue value) …とりあえずアクションマップを切り替える
Scene配置
Empty選択
Inspector>TestScript> InputActionAssetをドロップ


実行確認
設定したキーを押し、ログを確認

別のEmptyから入力を受け取る
↑のやり方だと、InpytSystemと同じEmptyにアタッチしないと機能しない
別のEnptyで入力を受け取るにはどうすれば?という
Emptyを選択
Inspector>TestScript>右端メニュー RemoveComponent
Hierarchy>右クリック>CreateEmpty 別のEmptyを追加
追加したEmptyにTestScriptをアタッチ
スクリプトにInputActionsをドロップ

TestScriptを編集
using UnityEngine;
using UnityEngine.InputSystem;
public class TestScript : MonoBehaviour
{
[SerializeField] private InputActionAsset _input;
private InputActionMap _test;
private void OnEnable()
{
_test = _input.FindActionMap("Test");
_test.Enable();
_test["Test"].performed += Test; //イベントリスナーを登録
}
private void OnDisable()
{
_test["Test"].performed -= Test; //イベントリスナーを削除
}
//メソッド名は適当でよく、引数にInputAction.CallbackContextを設定
private void Test(InputAction.CallbackContext context)
{
Debug.Log("Test");
}
}
必要なのはこのへん
OnEnable …アクションマップの取得と有効化
OnDisable …スクリプトが無効になったらイベント購読中止
_test["Test"].performed += Test; …TestアクションにTest()を登録
Test(InputAction.CallbackContext context) …Testから呼びたいメソッド
実行確認

詳しいやり方は他の方の良き記事がたくさんあるので
レベルアップ増改築はそちらをご参照ください。
初心者がぱっと使ってみたい~と思ったときのハードルの高さ
何とかならんものか(´・ω・`)