【Unity】【C#】InputSystem の Mouse クラスと (旧)Input.GetMouseButton の対応 
2020/10/04 Sun [edit]
InputSystem も Verified (正式版) となり、(旧)InputManager との混在も可能でもあるが、いずれ InputSystem の方法を中心として使いたいので調査中。
(旧)InputManager と InputSystem との対応は以下のマニュアルにも書かれているが、具体的に試してみた結果を書いておこう。
(参考)Migrating from the old input system
今回は InputSystem.Mouse クラスと、旧 Input.GetMouseButton でのクリック検出をフレーム単位での値で比較してみた。また、いつものように一覧表としてもまとめてみた。
●InputSystem 結果(一覧表)
■(旧)InputManager 版でのマウスクリック遷移の値
●InputManager 結果(一覧表)
(※) Unity 2019.4.11f1 / InputSystem 1.0.0 / Windows10(x64) で確認
■InputSystem 版でのマウスクリック遷移の値
●試したコード InputSystem 版(とりあえず、左クリックのみ)
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
public class InputSystemMouseTest : MonoBehaviour
{
#if ENABLE_INPUT_SYSTEM
bool pressing; //押下中フラグ
private void Update()
{
if (!pressing && Mouse.current.leftButton.wasPressedThisFrame) //押した
{
pressing = true;
Debug.Log($"frame: {Time.frameCount}, wasPressedThisFrame = {Mouse.current.leftButton.wasPressedThisFrame}"); //True
Debug.Log($"frame: {Time.frameCount}, isPressed = {Mouse.current.leftButton.isPressed}"); //True
Debug.Log($"frame: {Time.frameCount}, wasReleasedThisFrame = {Mouse.current.leftButton.wasReleasedThisFrame}"); //False
}
else if (pressing && Mouse.current.leftButton.wasReleasedThisFrame) //離した
{
pressing = false;
Debug.Log($"frame: {Time.frameCount}, wasPressedThisFrame = {Mouse.current.leftButton.wasPressedThisFrame}"); //False
Debug.Log($"frame: {Time.frameCount}, isPressed = {Mouse.current.leftButton.isPressed}"); //False
Debug.Log($"frame: {Time.frameCount}, wasReleasedThisFrame = {Mouse.current.leftButton.wasReleasedThisFrame}"); //True
}
else if (pressing) //継続中
{
Debug.Log($"frame: {Time.frameCount}, wasPressedThisFrame = {Mouse.current.leftButton.wasPressedThisFrame}"); //False
Debug.Log($"frame: {Time.frameCount}, isPressed = {Mouse.current.leftButton.isPressed}"); //True
Debug.Log($"frame: {Time.frameCount}, wasReleasedThisFrame = {Mouse.current.leftButton.wasReleasedThisFrame}"); //False
}
else
{
//押してないとき
}
}
#endif
}
簡単に説明すれば、マウスの左ボタンをクリックしたときのイベント(押した瞬間 → 押してる間 → 離した瞬間)の1フレーム内で、各関数・プロパティの値を比較したものだ。
●InputSystem 結果(一覧表)
押した | 継続中 | 離した | |
---|---|---|---|
Mouse.current.leftButton.wasPressedThisFrame | True | False | False |
Mouse.current.leftButton.isPressed | True | True | False |
Mouse.current.leftButton.wasReleasedThisFrame | False | False | True |
これは後述する(旧)InputManager 結果と一致する。どうやらマウスクリック遷移の判定として完全に代替できそうだ。
ちなみにここでは、左ボタン(leftButton)のみではあるが、他のボタンなら Mouse.current.〇〇 を rightButton, middleButton, backButton, ForwardButton に変えれば良い。詳しくは公式マニュアルを見て欲しい。
・Mouse support (InputSystem)
また、前回調査したスマホなどのタッチの拡張メソッド(IsActive(), IsEndedOrCanceled())にも対応できそうだ。
■(旧)InputManager 版でのマウスクリック遷移の値
●試したコード (旧)InputManager 版(とりあえず、左クリックのみ)
using UnityEngine;
public class InputManagerMouseTest : MonoBehaviour
{
bool pressing; //押下中フラグ
private void Update()
{
if (!pressing && Input.GetMouseButtonDown(0)) //押した
{
pressing = true;
Debug.Log($"frame: {Time.frameCount}, GetMouseButtonDown = {Input.GetMouseButtonDown(0)}"); //True
Debug.Log($"frame: {Time.frameCount}, GetMouseButton = {Input.GetMouseButton(0)}"); //True
Debug.Log($"frame: {Time.frameCount}, GetMouseButtonUp = {Input.GetMouseButtonUp(0)}"); //False
}
else if (pressing && Input.GetMouseButtonUp(0)) //離した
{
pressing = false;
Debug.Log($"frame: {Time.frameCount}, GetMouseButtonDown = {Input.GetMouseButtonDown(0)}"); //False
Debug.Log($"frame: {Time.frameCount}, GetMouseButton = {Input.GetMouseButton(0)}"); //False
Debug.Log($"frame: {Time.frameCount}, GetMouseButtonUp = {Input.GetMouseButtonUp(0)}"); //True
}
else if (pressing) //継続中
{
Debug.Log($"frame: {Time.frameCount}, GetMouseButtonDown = {Input.GetMouseButtonDown(0)}"); //False
Debug.Log($"frame: {Time.frameCount}, GetMouseButton = {Input.GetMouseButton(0)}"); //True
Debug.Log($"frame: {Time.frameCount}, GetMouseButtonUp = {Input.GetMouseButtonUp(0)}"); //False
}
else
{
//押してないとき
}
}
}
●(旧)InputManager 結果(一覧表)
押した | 継続中 | 離した | |
---|---|---|---|
Input.GetMouseButtonDown(0) | True | False | False |
Input.GetMouseButton(0) | True | True | False |
Input.GetMouseButtonUp(0) | False | False | True |
これは前述したInputSystem の結果と一致する。
ちなみに Input.GetMouseButton~ 系では、引数の int は 0: 左ボタン, 1: 右ボタン, 2: 中ボタン, 3:戻るボタン, 4:進むボタン となっているので、整数値 → ButtonControl (leftButton, rightButton, middleButton, backButton, ForwardButton) を返す拡張メソッドでも作れば、簡単に変換できそうだ(もしかして、別の書き方もあるかも知れないが…※まだ調査中)。
また、マウスのボタンは KeyCode でも取れるので、その辺りと比較してみるのも良い。以下に参考資料を載せておこう。
・5ボタンマウスの KeyCode 図解
マニュアルを見てると、「No corresponding API yet.」(まだ対応するAPIがない)というのがいくつもあるので、全て新旧一致できるわけではないようだが、よく使われる機能なら完全互換でいけそうなものも多いね。
ただ、今まで使っていた (旧)InputManager のコード(※主に Input) は全て書き換えないとならないので、大変な労力がいるのが難点。なので、しばらくは一部機能だけ使う感じになるだろう。
いずれ、スワイプ, ピンチ, 長押しなどのライブラリも InputSystem に対応しようと思っているが、ラッパー関数でも作らないと、旧新互換で作るのは骨が折れるね(笑)。
(関連記事)
【Unity】【C#】InputSystem でマウスのホイール(scroll)取得と (旧)Input との対応
【Unity】【C#】InputSystem.TouchPhase の IsActive(), IsEndedOrCanceled() [拡張メソッド] の具体値
【Unity】【C#】(旧)KeyCode と InputSystem.Key の対応
【Unity】【C#】InputSystem の displayName や control path から Key (キーコード) を取得する
【Unity】【C#】InputSystem の Keyboard クラスと (旧)Input.GetKey の対応
【Unity】【C#】InputSystem.Key をリアルタイムで調べる / Windows 日本語キーボードでの Key 一覧
【Unity】5ボタンマウスの KeyCode 図解
- 関連記事
トラックバック
トラックバックURL
→http://fantom1x.blog130.fc2.com/tb.php/366-beebeb17
この記事にトラックバックする(FC2ブログユーザー)
| h o m e |