Input Systemを作成して、Player Inputから任意のEventを起動する方法をメモ。
今回行う事
- 「Input System」の作成と設定。
- 「Player Input」からEventを起動する。
またこの記事はInput Systemの勉強の続きです。
Input Systemを作成する
Input Action Importer の生成

Createメニューから

InputActionsを選択

Input Action Importerが生成される。
生成した「Input Action Importer」を選択してInspectorからスクリプト生成をする。
Input ActionC#スクリプトの生成

GenerateC#Classにチェック

各項目に入力し→Apply(記入無しでも可)

C#スクリプトが生成される。(例では名前空間のみ入力)
Actionを作る


生成した「Input Action Importer」をダブルクリックして「Input Actions」エディタを開く。それぞれ項目に入力する。
Action Maps
「Player」や「UI」等入力のカテゴリ分けを行い、Actionsにて対応した操作を付ける。
Actions
Action名とそれに対応したキー操作(複数可)を決める。
Properties
キー操作の詳細を入力する。
- Action
- Interactions
- Processors
PlayerInputからEventを起動する。
「InputActions」と「PlayerInput」の関係図

一つのコールバックに対して、複数の操作を登録できるためマウスとキーボード、ゲームパット等の操作を一括して管理できる。
(図の画像では「キーボード:Mキー」と「マウス右クリック」を同じ「メニュー操作」としてテスト)
GameObjectにPlayerInputを取り付ける
AddComponentし、起動したいイベントを登録。
テスト
キャンバスのON/OFFを行うテストです。

「SetInvertBehaviour」はGameObjectの有効無効を切り替えるだけのスクリプトです。
イベントを設定、コントロールするInputManagerを作りました。

テスト結果

トラブルシューティング
イベントが呼び出されない
Project Settings > Player > Active Input HandlingをInput SystemもしくはBothにした後再起動する。

日本語入力が出来ない
試した時期と重なった為か原因が確定している訳ではありませんが、
全角文字が入らない場合の解決方法はUnityのバージョンを上げる事になります。
一度の入力で複数回呼び出される
「PressOnly」一回の入力で3回入力(DebugLog上で確認)が入り(見た目の挙動は正常という)現象が発生 → Input Actions 内 Schemeの設定を行う。
コメント