以下の内容はhttps://tat1kun.hatenablog.com/entry/inputsystem_translateより取得しました。


Unity6からInput systemがデフォルトになっていたので、移行ガイドを訳す


PR

現在プレビュー版が公開されているUnity6で新規プロジェクトを作成してみたところ、 デフォルトがInputSystemを使用するようになっていました。

もう実装自体から何年も経っていますが、Input Managerを未だに使い続けてる人は少なくないはずです。

私もその一人なので、新しいInputSystemに移行するために移行用ドキュメントを翻訳していきます。

移行用ドキュメント

https://docs.unity3d.com/Packages/com.unity.inputsystem@1.8/manual/Migration.html

環境

InputSystem 1.8.2

注意

翻訳は基本的にDeepLやChatGPT等で訳したものをベースとし、入力カテゴリ別に分けてまとめなおしてます。

また、本格的にInputSystemを使う場合はもっとInputSystemについてちゃんと調べて使った方が良いと思います。以下は移行ガイドの冒頭に書かれている内容を訳したものです。

Input System パッケージに初めて触れる方は、このページをドキュメントを探して訪れた場合、QuickStart ガイドと、ドキュメントの導入セクションから Concepts と Workflows ページを読むことをお勧めします。これにより、プロジェクトの入力要件に最適なワークフローを選択できるようになります。

なぜなら、Input System を使用して入力を読み取る方法は様々で、このページの直接対応させているAPI メソッドのいくつかは、最速であるが柔軟性に欠ける解決策であり、より複雑な要件を持つプロジェクトには適していない場合があるからです。

(公式ドキュメントから引用 一部DeepL ChatGPTを用いて翻訳)

加速度センサ

Input Manager (旧) Input System (新)
Input.acceleration Accelerometer.current.acceleration.ReadValue()
Input.accelerationEventCount
Input.accelerationEvents
「Acceleration events」は他の入力イベントとは個別に利用できません。以下のコードは、Accelerometer.current デバイス上のすべての入力イベントをトレースします。
    private InputEventTrace trace;

    void StartTrace()
    {
        InputSystem.EnableDevice(Accelerometer.current);

        trace = new InputEventTrace(Accelerometer.current);
        trace.Enable();
    }

    void Update()
    {
        foreach (var e in trace)
        {
            //...
        }
        trace.Clear();
    }

キー入力

Input Manager (旧) Input System (新)
Input.anyKey InputSystem.onAnyButtonPress
例:
InputSystem.onAnyButtonPress.CallOnce(ctrl => Debug.Log($"{ctrl} ボタンが押されました"));
Input.anyKeyDown Keyboard.current.anyKey.wasUpdatedThisFrame ()
Input.GetKey 対応するキーで ButtonControl.isPressed を使用してください
Input.GetKeyDown 対応するキーで ButtonControl.wasPressedThisFrame を使用してください
Input.GetKeyUp 対応するキーで ButtonControl.wasReleasedThisFrame を使用してください
Input.inputString Subscribe to the Keyboard.onTextInput event:
Keyboard.current.onTextInput += character => / ... /;
// KeyControl propertyを直接使用する場合
Keyboard.current.spaceKey.isPressed
Keyboard.current.spaceKey.wasPressedThisFrame
Keyboard.current.spaceKey.wasReleasedThisFrame

// Key enumを使用する場合
Keyboard.current[Key.Space].isPressed
Keyboard.current[Key.Space].wasPressedThisFrame
Keyboard.current[Key.Space].wasReleasedThisFrame

// キー名を使用する場合
((KeyControl)Keyboard.current["space"]).isPressed
((KeyControl)Keyboard.current["space"]).wasPressedThisFrame
((KeyControl)Keyboard.current["space"]).wasReleasedThisFrame

注: Input System はキーを物理的な配置に基づいて識別します。言語マッピングに従って入力を検知するには、KeyControl.displayName を使用してください。

マウス入力

Input Manager (旧) Input System (新)
Input.GetMouseButton 対応するマウスのボタンの ButtonControl.isPressed を使用してください
Input.GetMouseButtonDown 対応するマウスのボタンの ButtonControl.wasPressedThisFrame を使用してください
Input.GetMouseButtonUp 対応するマウスのボタンの ButtonControl.wasReleasedThisFrame を使用してください
Input.mousePosition Mouse.current.position.ReadValue() 注: タッチによるマウスシミュレーションはまだ実装されていません
Input.mousePresent Mouse.current != null.
Mouse.current.leftButton.isPressed
Mouse.current.rightButton.isPressed
Mouse.current.middleButton.isPressed

// マウスの全ボタンを通過することもできる(割り当てない)。
var controls = Mouse.current.allControls;
for (var i = 0; i < controls.Count; ++i)
{
    var button = controls[i] as ButtonControl;
    if (button != null && button.isPressed)
        /* ... */;
}

// コントロールの名前で検索することもできる。
((ButtonControl)Mouse.current["leftButton"]).isPressed

PAD入力

Input Manager (旧) Input System (新)
Input.GetAxis アクションエディタで1Dまたは2D軸としてアクションを設定し、InputAction.ReadValueを使用して軸から値または2Dベクトルを読み取ります。デフォルトの組み込み軸アクションがいくつかあります。すぐに始めるには、クイックスタートガイドを参照してください。
Input.GetAxisRaw 直接は適用できません。InputControl<>.ReadUnprocessedValue()を使用して、任意のコントロールから未処理の値を読み取ることができます。
Input.GetButton InputAction.IsPressed
Input.GetButtonDown InputAction.WasPressedThisFrame
Input.GetButtonUp InputAction.WasReleasedThisFrame
Input.GetJoystickNames これに正確に対応するAPIはない。ここでは、接続されたデバイスを発見するための様々な方法を紹介します:
// 接続されているすべてのDeviceのリストを取得する(割り当てを行わない。)
InputSystem.devices

// デバイスが追加または削除されたときに通知を受け取ります。
InputSystem.onDeviceChange +=
    (device, change) =>
    {
        if (change == InputDeviceChange.Added || change == InputDeviceChange.Removed)
        {
            Debug.Log($"Device '{device}' was {change}");
        }
    }

// すべてのゲームパッドとジョイスティックを検索します。
var devices = InputSystem.devices;
for (var i = 0; i < devices.Count; ++i)
{
    var device = devices[i];
    if (device is Joystick || device is Gamepad)
    {
        Debug.Log("Found " + device);
    }
}

タッチ入力

Input Manager (旧) Input System (新)
Input.multiTouchEnabled 対応するAPIはまだありません。
Input.simulateMouseWithTouches 対応するAPIはまだありません。
Input.stylusTouchSupported 対応するAPIはまだありません。
Input.touchCount InputSystem.EnhancedTouch.Touch.activeTouches.Count
注:最初にInputSystem.EnhancedTouchSupport.Enable()を呼び出して、拡張タッチサポートを有効にします。
Input.touches InputSystem.EnhancedTouch.Touch.activeTouches
注:最初にInputSystem.EnhancedTouch.Enable()を呼び出して、拡張タッチサポートを有効にします。
Input.touchPressureSupported 対応するAPIはまだありません。
Input.touchSupported Touchscreen.current != null

その他

加速度

Input Manager (旧) Input System (新)
Input.acceleration Accelerometer.current.acceleration.ReadValue().
Input.accelerationEventCount
Input.accelerationEvents
加速度イベントは、他の入力イベントとは個別に利用できるようにはなっていない。次のコードは、Accelerometer.currentデバイスのすべての入力イベントをトレースします。
Input.GetAccelerationEvent 上記のInput.accelerationEventsに関する注意を参照
    private InputEventTrace trace;

    void StartTrace()
    {
        InputSystem.EnableDevice(Accelerometer.current);

        trace = new InputEventTrace(Accelerometer.current);
        trace.Enable();
    }

    void Update()
    {
        foreach (var e in trace)
        {
            //...
        }
        trace.Clear();
    }

バックボタン

Input Manager (旧) Input System (新)
Input.backButtonLeavesApp 対応するAPIはまだありません。

コンパス

Input Manager (旧) Input System (新)
Input.compass 対応するAPIはまだありません。
Input.compensateSensors InputSystem.settings.compensateForScreenOrientation.
Input.compositionCursorPos Keyboard.current.SetIMECursorPosition(myPosition)
Input.compositionString Keyboard.onIMECompositionChangeイベントを購読する:
    var compositionString = "";
    Keyboard.current.onIMECompositionChange += composition =>
    {
        compositionString = composition.ToString();
    };

ジャイロ

Input Manager (旧) Input System (新)
Input.deviceOrientation 対応するAPIはまだありません。
Input.gyro UnityEngine.Gyroscopeクラスは、複数の独立したセンサーに置き換えられます。
Gyroscope : 各速度を測定する
GravitySensor : 重力の方向を測定する
AttitudeSensor : デバイスの方向を測定する
Accelerometer : デバイスにかかる総加速度を測定する
LinearAccelerationSensor : 重力を補正して装置の加わる加速度を測定する
Input.gyro.attitude AttitudeSensor.current.orientation.ReadValue().
Input.gyro.enabled Get: Gyroscope.current.enabled
Set:
InputSystem.EnableDevice(Gyroscope.current);
InputSystem.DisableDevice(Gyroscope.current);
注:InputSystemは、UnityEngine.Gyroscopeを複数の別々のセンサーデバイスに置き換えます。必要に応じて、Gyroscopeをサンプルの他のセンサーで置き換えてください。詳しくは上記のInput.gyroを参照してください。
Input.gyro.gravity GravitySensor.current.gravity.ReadValue()
Input.gyro.rotationRate Gyroscope.current.angularVelocity.ReadValue().
Input.gyro.rotationRateUnbiased 対応するAPIはまだありません。
Input.gyro.updateInterval Sensor.samplingFrequency
例:
Gyroscope.current.samplingFrequency = 1.0f / updateInterval;
注:
samplingFrequencyの単位はHzで、updateIntervalのように秒ではないので、値で1を割る必要がある。
Input SystemInput Systemは、UnityEngine.Gyroscopeを複数の別々のセンサーデバイスに置き換えます。必要に応じて、Gyroscopeをサンプルの他のセンサーで置き換えてください。詳細は上記のInput.gyroを参照してください。
Input.gyro.userAcceleration LinearAccelerationSensor.current.acceleration.acceleration.ReadValue()

IME

Input Manager (旧) Input System (新)
Input.imeCompositionMode 対応するAPIはまだありません。
Input.imeIsSelected Get: Keyboard.current.imeSelected
Set: Keyboard.current.SetIMEEnabled(true);

その他

Input Manager (旧) Input System (新)
Input.location 対応するAPIはまだありません。



以上の内容はhttps://tat1kun.hatenablog.com/entry/inputsystem_translateより取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14