Rasagar/Library/PackageCache/com.unity.inputsystem/Documentation~/Migration.md
2024-08-26 23:07:20 +03:00

19 KiB

uid
input-system-migration

Migrating from the old Input Manager

This page is provided to help you match input-related API from Unity's old, built-in input (known as the Input Manager) to the corresponding API in the new Input System package.

Read the introductory documentation first

If you're new to the Input System package and have landed on this page looking for documentation, it's best to read the QuickStart Guide, and the Concepts and Workflows pages from the introduction section of the documentation, so that you can make sure you're choosing the best workflow for your project's input requirements.

This is because there are a number of different ways to read input using the Input System, and many of the directly corresponding API methods on this page might give you the quickest but least flexible solution, and may not be suitable for a project with more complex requirements.

Which system is enabled?

When installing the new Input System, Unity prompts you to enable the new input system and disable the old one. You can change this setting at any time later, by going to Edit > Project Settings > Player > Other Settings > Active Input Handling, as described here.

There are scripting symbols defined which allow you to use conditional compilation based on which system is enabled, as shown in the example below.

#if ENABLE_INPUT_SYSTEM
    // New input system backends are enabled.
#endif

#if ENABLE_LEGACY_INPUT_MANAGER
    // Old input backends are enabled.
#endif

Note: It is possible to have both systems enabled at the same time, in which case both sets of code in the example above above will be active.

List of corresponding API in the old Input Manager new Input System package

All of the new APIs listed below are in the UnityEngine.InputSystem namespace. The namespace is omitted here for brevity. UnityEngine.InputSystem is referenced in full for easy disambiguation.

Input Manager (Old) Input System (New)
Input.acceleration Accelerometer.current.acceleration.ReadValue().
Input.accelerationEventCount
Input.accelerationEvents
Acceleration events aren't made available separately from other input events. The following code traces all input events on the Accelerometer.current device.
    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 (Old) Input System (New)
Input.anyKey InputSystem.onAnyButtonPress
Example:
InputSystem.onAnyButtonPress.CallOnce(ctrl => Debug.Log($"Button {ctrl} pressed!"));
Input Manager (Old) Input System (New)
-- --
Input.anyKeyDown Keyboard.current.anyKey.wasUpdatedThisFrame
Input.backButtonLeavesApp No corresponding API yet.
Input.compass No corresponding API yet.
Input.compensateSensors InputSystem.settings.compensateForScreenOrientation.
Input.compositionCursorPos Keyboard.current.SetIMECursorPosition(myPosition)
Input.compositionString Subscribe to the Keyboard.onIMECompositionChange event:
    var compositionString = "";
    Keyboard.current.onIMECompositionChange += composition =>
    {
        compositionString = composition.ToString();
    };
Input Manager (Old) Input System (New)
Input.deviceOrientation No corresponding API yet.
Input.gyro The UnityEngine.Gyroscope class is replaced by multiple separate sensor Devices in the new Input System:
Gyroscope to measure angular velocity.
GravitySensor to measure the direction of gravity.
AttitudeSensor to measure the orientation of the device.
Accelerometer to measure the total acceleration applied to the device.
LinearAccelerationSensor to measure acceleration applied to the device, compensating for gravity.
Input.gyro.attitude AttitudeSensor.current.orientation.ReadValue().
Input.gyro.enabled Get: Gyroscope.current.enabled
Set:
InputSystem.EnableDevice(Gyroscope.current);
InputSystem.DisableDevice(Gyroscope.current);

Note: The new Input System replaces UnityEngine.Gyroscope with multiple separate sensor devices. Substitute Gyroscope with other sensors in the sample as needed. See the notes for Input.gyro above for details.
Input.gyro.gravity GravitySensor.current.gravity.ReadValue()
Input.gyro.rotationRate Gyroscope.current.angularVelocity.ReadValue().
Input.gyro.rotationRateUnbiased No corresponding API yet.
Input.gyro.updateInterval Sensor.samplingFrequency
Example:
Gyroscope.current.samplingFrequency = 1.0f / updateInterval;

Notes:
samplingFrequency is in Hz, not in seconds as updateInterval, so you need to divide 1 by the value.

The new Input System replaces UnityEngine.Gyroscope with multiple separate sensor devices. Substitute Gyroscope with other sensors in the sample as needed. See the notes for Input.gyro above for details.
Input.gyro.userAcceleration LinearAccelerationSensor.current.acceleration.acceleration.ReadValue()
Input.imeCompositionMode No corresponding API yet.
Input.imeIsSelected Get: Keyboard.current.imeSelected
Set: Keyboard.current.SetIMEEnabled(true);
Input.inputString Subscribe to the Keyboard.onTextInput event:
Keyboard.current.onTextInput += character => /* ... */;
Input.location No corresponding API yet.
Input.mousePosition Mouse.current.position.ReadValue()
Note: Mouse simulation from touch isn't implemented yet.
Input.mousePresent Mouse.current != null.
Input.multiTouchEnabled No corresponding API yet.
Input.simulateMouseWithTouches No corresponding API yet.
Input.stylusTouchSupported No corresponding API yet.
Input.touchCount InputSystem.EnhancedTouch.Touch.activeTouches.Count
Note: Enable enhanced touch support first by calling InputSystem.EnhancedTouchSupport.Enable()
Input.touches InputSystem.EnhancedTouch.Touch.activeTouches
Note: Enable enhanced touch support first by calling InputSystem.EnhancedTouch.Enable()
Input.touchPressureSupported No corresponding API yet.
Input.touchSupported Touchscreen.current != null
Input.GetAccelerationEvent See notes for Input.accelerationEvents above.
Input.GetAxis Set up an action as a 1D or 2D axis in the Actions Editor, then use InputAction.ReadValue to read the value or 2D vector from the axis. There are some default built-in axis actions. See the Quickstart Guide to get started quickly.
Input.GetAxisRaw Not directly applicable. You can use InputControl<>.ReadUnprocessedValue() to read unprocessed values from any control.
Input.GetButton InputAction.IsPressed
Input.GetButtonDown InputAction.WasPressedThisFrame
Input.GetButtonUp InputAction.WasReleasedThisFrame
Input.GetJoystickNames There is no API that corresponds to this exactly. Here are various ways to discover connected Devices:
// Query a list of all connected Devices (does not allocate; read-only access).
InputSystem.devices

// Get notified when a Device is added or removed.
InputSystem.onDeviceChange +=
    (device, change) =>
    {
        if (change == InputDeviceChange.Added || change == InputDeviceChange.Removed)
        {
            Debug.Log($"Device '{device}' was {change}");
        }
    }

// Find all gamepads and joysticks.
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 (Old) Input System (New)
Input.GetKey ButtonControl.isPressed on the corresponding key:
// Using KeyControl property directly.
Keyboard.current.spaceKey.isPressed
Keyboard.current.aKey.isPressed // etc.

// Using Key enum.
Keyboard.current[Key.Space].isPressed

// Using key name.
((KeyControl)Keyboard.current["space"]).isPressed

Note: The Input System identifies keys by physical layout, not according to the current language mapping of the keyboard. To query the name of the key according to the language mapping, use KeyControl.displayName.

Input Manager (Old) Input System (New)
Input.GetKeyDown Use ButtonControl.wasPressedThisFrame on the corresponding key:
// Using KeyControl property directly.
Keyboard.current.spaceKey.wasPressedThisFrame
Keyboard.current.aKey.wasPressedThisFrame // etc.

// Using Key enum.
Keyboard.current[Key.Space].wasPressedThisFrame

// Using key name.
((KeyControl)Keyboard.current["space"]).wasPressedThisFrame

Note: The Input System identifies keys by physical layout, not according to the current language mapping of the keyboard. To query the name of the key according to the language mapping, use KeyControl.displayName.

Input Manager (Old) Input System (New)
Input.GetKeyUp Use ButtonControl.wasReleasedThisFrame on the corresponding key:
// Using KeyControl property directly.
Keyboard.current.spaceKey.wasReleasedThisFrame
Keyboard.current.aKey.wasReleasedThisFrame // etc.

// Using Key enum.
Keyboard.current[Key.Space].wasReleasedThisFrame

// Using key name.
((KeyControl)Keyboard.current["space"]).wasReleasedThisFrame

Note: The Input System identifies keys by physical layout, not according to the current language mapping of the keyboard. To query the name of the key according to the language mapping, use KeyControl.displayName.

Input Manager (Old) Input System (New)
Input.GetMouseButton Use ButtonControl.isPressed on the corresponding mouse button:
Mouse.current.leftButton.isPressed
Mouse.current.rightButton.isPressed
Mouse.current.middleButton.isPressed

// You can also go through all buttons on the mouse (does not allocate).
var controls = Mouse.current.allControls;
for (var i = 0; i < controls.Count; ++i)
{
    var button = controls[i] as ButtonControl;
    if (button != null && button.isPressed)
        /* ... */;
}

// Or look up controls by name.
((ButtonControl)Mouse.current["leftButton"]).isPressed
Input Manager (Old) Input System (New)
Input.GetMouseButtonDown Use ButtonControl.wasPressedThisFrame on the corresponding mouse button:
Mouse.current.leftButton.wasPressedThisFrame
Mouse.current.rightButton.wasPressedThisFrame
Mouse.current.middleButton.wasPressedThisFrame
Input Manager (Old) Input System (New)
Input.GetMouseButtonUp Use ButtonControl.wasReleasedThisFrame on the corresponding mouse button:
Mouse.current.leftButton.wasReleasedThisFrame
Mouse.current.rightButton.wasReleasedThisFrame
Mouse.current.middleButton.wasReleasedThisFrame
Input Manager (Old) Input System (New)
Input.GetTouch Use InputSystem.EnhancedTouch.Touch.activeTouches[i]
Note: Enable enhanced touch support first by calling InputSystem.EnhancedTouch.Enable().
Input.IsJoystickPreconfigured Not needed. Devices which derive from Gamepad always correctly implement the mapping of axes and buttons to the corresponding InputControl members of the Gamepad class. Input.ResetInputAxes
UnityEngine.TouchScreenKeyboard No corresponding API yet. Use TouchScreenKeyboard for now.