Unityの入出力管理ツール InControl で簡単にゲームコントローラーを設定
スマホでFPS/TPSの様な左右のスティックが必要なゲームでは、大抵の場合2本の仮想スティック(バーチャルスティック)を使っていると思います。
そんなゲームのテストプレイをする時、Unityエディターの Game Window では一度に片方の仮想スティックしか操作できません。スマホをPCやMacに繋いで Unity Remote を使えば良いのですが、フレームレートが落ちて操作感が変わってしまう。いちいちビルドするのも時間がかかって面倒。そこで、PS4やXbox360のコントローラーが使えると操作しやすいし、Steam向けのリリースも視野に入れることができます。
Unity標準メニューの Edit→Project Settings→Input でゲーム用コントローラーの設定ができるのですが…
手動で全てのボタンやスティックの設定をするのは大変そうです。実際、筆者は設定方法をググってもよく分かりませんでした。
そこで、今回ご紹介するのが Unity Asset Store で販売されている入出力管理ツール 「InControl」です!これを使うと、いともたやすく行われるコントローラ設定ッ!
簡単ですが、一応手順をご説明します。(InControl 1.7.3 / Unity 2018.3.14f1 / macOS 10.14.4で確認)
InControl の初期設定
この記事の執筆時点では、通常$35が半額の$17.5で購入できます!
Unity Asset Store で購入後、Unityエディターからダウンロードとインポートを行ってください。その直後の段階だと、コンソールに下記の様なエラーが出ていると思います。
1 2 3 4 |
InControl needs to modify your InputManager settings. Please run the 'InControl > Setup InputManager Settings' menu item. UnityEngine.Debug:LogError(Object) InControl.InputManagerAssetGenerator:.cctor() (at Assets/InControl/Editor/InputManagerAssetGenerator.cs:22) UnityEditor.EditorAssemblies:ProcessInitializeOnLoadAttributes() |
初期設定が必要なので、メニューの InControlから Setup InputManager Settings を選びます。コンソールに下記の様に “successfully” と表示されたら成功です。
※バージョンによっては、Edit→Project Settings→InControl→Setup InputManager Settings にある様です。
1 2 3 |
InControl has successfully modified your InputManager settings. UnityEngine.Debug:Log(Object) InControl.InputManagerAssetGenerator:GenerateInputManagerAsset() (at Assets/InControl/Editor/InputManagerAssetGenerator.cs:32) |
先ほどのメニューにあった項目 Check InputManager Settings を試してみましょう。下記の様に “fine” と表示されるはずです。
1 2 3 |
InputManager settings are fine. UnityEngine.Debug:Log(Object) InControl.InputManagerAssetGenerator:CheckInputManagerAsset() (at Assets/InControl/Editor/InputManagerAssetGenerator.cs:41) |
ちなみに、Setupを実行前にCheckを行うと下記の様に設定が必要だと表示されます。
1 2 3 |
InControl needs to modify your InputManager settings. Please run the 'InControl > Setup InputManager Settings' menu item. UnityEngine.Debug:LogError(Object) InControl.InputManagerAssetGenerator:CheckInputManagerAsset() (at Assets/InControl/Editor/InputManagerAssetGenerator.cs:45) |
これでもうゲーム用のコントローラーを認識しています。ツール内に含まれているテスト用シーンで試してみましょう。Assets/InControl/Test/フォルダ内のシーン TestInputManager を開いて Game Window で実行します。
USBケーブルで接続されたPS4のコントローラーを認識しています。画像では複数のボタンを同時に押していますがきちんと反応しています。
実際にゲーム内でプレイヤーを動かす
次は、実際にゲームの中で動かしてみましょう。
まず、メニューの GameObject→InControl→Manager を選んで、ヒエラルキーウィンドウに InControl オブジェクトを作成します。
インスペクターウィンドウはこの画像の様になっていました。特に変更せずに進めます。
次に、プレイヤー操作を管理するスクリプトの先頭に “using InControl” を追加します。
1 2 3 4 |
using System.Collections; using System.Collections.Generic; using UnityEngine; using InControl; |
さらに、Update内に次のコードを追加します。
1 2 3 4 |
void Update() { var ad = InputManager.ActiveDevice; } |
次からは、実際の環境に合わせてカスタマイズしてください。筆者の場合は、仮想スティックを画面タッチで動かすコードが下記の様になっていました(一部分)。
1 2 |
var ad = InputManager.ActiveDevice; Vector3 moveVector = (Vector3.right * joystick.Horizontal + Vector3.forward * joystick.Vertical); |
この “joystick.Horizontal” と “joystick.Vertical” の部分を置き換えます。
1 2 |
var ad = InputManager.ActiveDevice; moveVector = (Vector3.right * ad.LeftStick.X + Vector3.forward * ad.LeftStick.Y); |
Unityエディターで実行して、正常に動作するのを確認したらもう一方のスティックも設定します。
これで、快適にテストプレイができるようになりました!
必要に応じて、下記のコードをUpdateの中に入れてください。UnityのコンソールでPS4コントローラーの動作確認ができます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
void Update() { var ad = InputManager.ActiveDevice; // ボタンが押されたかを表示 if (ad.DPadLeft.WasPressed) Debug.Log("方向キー左が押された"); if (ad.DPadRight.WasPressed) Debug.Log("方向キー右が押された"); if (ad.DPadUp.WasPressed) Debug.Log("方向キー上が押された"); if (ad.DPadDown.WasPressed) Debug.Log("方向キー下が押された"); if (ad.Action1.WasPressed) Debug.Log("×ボタンが押された"); if (ad.Action2.WasPressed) Debug.Log("○ボタンが押された"); if (ad.Action3.WasPressed) Debug.Log("□ボタンが押された"); if (ad.Action4.WasPressed) Debug.Log("△ボタンが押された"); if (ad.LeftBumper.WasPressed) Debug.Log("L1ボタンが押された"); if (ad.RightBumper.WasPressed) Debug.Log("R1ボタンが押された"); if (ad.LeftTrigger.WasPressed) Debug.Log("L2ボタンが押された"); if (ad.RightTrigger.WasPressed) Debug.Log("R2ボタンが押された"); if (ad.LeftStickButton.WasPressed) Debug.Log("L3ボタンが押された"); if (ad.RightStickButton.WasPressed) Debug.Log("R3 ボタンが押された"); if (ad.GetControl(InputControlType.Options).WasPressed) Debug.Log("OPTIONSボタンが押された"); if (ad.GetControl(InputControlType.Share).WasPressed) Debug.Log("SHAREボタンが押された"); if (ad.GetControl(InputControlType.System).WasPressed) Debug.Log("PSボタンが押された"); if (ad.GetControl(InputControlType.TouchPadButton).WasPressed) Debug.Log("パッドボタンが押された"); // 左スティックの傾きを表示 Debug.Log("左スティック横方向の傾き: " + ad.LeftStick.X); // -1~1 Debug.Log("左スティック縦方向の傾き: " + ad.LeftStick.Y); // -1~1 // Debug.Log("左スティック左方向の傾き: " + ad.LeftStickLeft.Value); // 0~1 // Debug.Log("左スティック右方向の傾き: " + ad.LeftStickRight.Value); // 0~1 // Debug.Log("左スティック上方向の傾き: " + ad.LeftStickUp.Value); // 0~1 // Debug.Log("左スティック下方向の傾き: " + ad.LeftStickDown.Value); // 0~1 // 右スティックの傾きを表示 Debug.Log("右スティック横方向の傾き: " + ad.RightStick.X); // -1~1 Debug.Log("右スティック縦方向の傾き: " + ad.RightStick.Y); // -1~1 // Debug.Log("右スティック左方向の傾き: " + ad.RightStickLeft.Value); // 0~1 // Debug.Log("右スティック右方向の傾き: " + ad.RightStickRight.Value); // 0~1 // Debug.Log("右スティック上方向の傾き: " + ad.RightStickUp.Value); // 0~1 // Debug.Log("右スティック下方向の傾き: " + ad.RightStickDown.Value); // 0~1 } |
こちらの記事を参考にさせて頂きました。
ディスカッション
コメント一覧
まだ、コメントがありません