Migrate From Old Input System
This guide provides a listing of the APIs in UnityEngine.Input
(and related APIs in UnityEngine
) and their corresponding APIs in the new input system. Not all APIs have a corresponding version in the new API yet.
NOTE: The new APIs are currently in the
UnityEngine.InputSystem
namespace. The namespace is omitted here for brevity.UnityEngine.InputSystem
is referenced in full for easy disambiguation.
UnityEngine.Input
UnityEngine.Input.acceleration
Use Accelerometer.current.acceleration.ReadValue()
.
UnityEngine.Input.accelerationEventCount
See next section.
UnityEngine.Input.accelerationEvents
Acceleration events are not made available separately from other input events. The following code will trace all input events on the Accelerometer.current
device.
private InputEventTrace trace;
void StartTrace()
{
trace = new InputEventTrace() {deviceId = Accelerometer.current.id};
trace.Enable();
}
void Update()
{
foreach (var e in trace)
{
//...
}
trace.Clear();
}
UnityEngine.Input.anyKey
For keyboard keys:
Use Keyboard.current.anyKey.isPressed
.
For mouse buttons:
UnityEngine.Input.anyKeyDown
Use Keyboard.current.anyKey.wasUpdatedThisFrame
UnityEngine.Input.backButtonLeavesApp
No corresponding API yet.
UnityEngine.Input.compass
No corresponding API yet.
UnityEngine.Input.compensateSensors
Use InputSystem.settings.compensateForScreenOrientation
.
UnityEngine.Input.compositionCursorPos
Use Keyboard.current.SetIMECursorPosition(myPosition)
.
UnityEngine.Input.compositionString
Subscribe to the Keyboard.onIMECompositionChange
event:
var compositionString = "";
Keyboard.current.onIMECompositionChange += composition =>
{
compositionString = composition.ToString();
};
UnityEngine.Input.deviceOrientation
No corresponding API yet.
UnityEngine.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.
UnityEngine.Input.gyro.attitude
Use AttitudeSensor.current.orientation.ReadValue()
.
UnityEngine.Input.gyro.enabled
// Get.
Gyroscope.current.enabled
// Set.
InputSystem.EnableDevice(Gyroscope.current);
InputSystem.DisableDevice(Gyroscope.current);
NOTE:
UnityEngine.Gyroscope
is replaced by multiple separate sensor devices in the new Input System. SubstituteGyroscope
with other sensors in the sample as needed.
UnityEngine.Input.gyro.gravity
Use GravitySensor.current.gravity.ReadValue()
.
UnityEngine.Input.gyro.rotationRate
Use Gyroscope.current.angularVelocity.ReadValue()
.
UnityEngine.Input.gyro.rotationRateUnbiased
No corresponding API yet.
UnityEngine.Input.gyro.updateInterval
Gyroscope.current.samplingFrequency = 1.0f / updateInterval;`
Notes:
samplingFrequency
is in Hz, not in seconds asupdateInterval
, so you need to divide 1 by the value.UnityEngine.Gyroscope
is replaced by multiple separate sensor devices in the new Input System. SubstituteGyroscope
with other sensors in the sample as needed.
UnityEngine.Input.gyro.userAcceleration
Use LinearAccelerationSensor.current.acceleration.acceleration.ReadValue()
.
UnityEngine.Input.imeCompositionMode
No corresponding API yet.
UnityEngine.Input.imeIsSelected
// Get:
Keyboard.current.imeSelected
// Set:
Keyboard.current.SetIMEEnabled(true);
UnityEngine.Input.inputString
Subscribe to the Keyboard.onTextInput
event:
Keyboard.current.onTextInput +=
character => /* ... */;
UnityEngine.Input.location
No corresponding API yet.
UnityEngine.Input.mousePosition
Use Mouse.current.position.ReadValue()
.
NOTE: Mouse simulation from touch is not implemented yet.
UnityEngine.Input.mousePresent
UnityEngine.Input.multiTouchEnabled
No corresponding API yet.
UnityEngine.Input.simulateMouseWithTouches
No corresponding API yet.
UnityEngine.Input.stylusTouchSupported
No corresponding API yet.
UnityEngine.Input.touchCount
Use InputSystem.EnhancedTouch.Touch.activeTouches.Count
NOTE: Enable enhanced touch support first by calling
InputSystem.EnhancedTouch.Enable()
.
UnityEngine.Input.touches
Use InputSystem.EnhancedTouch.Touch.activeTouches
.
NOTE: Enable enhanced touch support first by calling
InputSystem.EnhancedTouch.Enable()
.
UnityEngine.Input.touchPressureSupported
No corresponding API yet.
UnityEngine.Input.touchSupported
Use Touchscreen.current != null
.
UnityEngine.Input.GetAccelerationEvent
See UnityEngine.Input.accelerationEvents
.
UnityEngine.Input.GetAxis
There is no global setup corresponding exactly to "virtual axis" setups in the old player input settings. Instead, sets of "input actions" can be set up as independent assets or put directly on your C# components.
As an example, let's recreate the following axis configuration:
Option A: Put input actions on your component
Declare one or more fields or properties type
InputAction
.public class MyComponent : MonoBehaviour { public InputAction fireAction;
Hook up a response to the action.
void Awake() { fireAction.performed += ctx => Fire(); } void Fire() { //... }
Put the component on a
GameObject
and configure bindings in the inspector by clicking the plus sign on the bindings list to add bindings and double-clicking the bindings to pick controls to bind to.Enable and disable the action as needed.
void OnEnable() { fireAction.Enable(); } void OnDisable() { fireAction.Disable(); }
Option B: Create input action asset
- Create an input action asset by right-clicking in the project browser and selecting "Create >> Input Actions" (alternatively you can go to "Assets >> Create >> Input Actions" in the main menu bar). Give a name to the asset.
- Double-click the asset to open the Input Actions editor window.
- In the "Action Maps" column click "+" to add a new action map.
- Double-click the "New action map" name to give the set a better name. E.g. "gameplay".
- In the "Actions" column click "+" to add a new action.
- Double-click the action to give it a name.
- Add bindings to the action by clicking "+" on the acttion and using the Path popup button in the right column to select controls.
Click "Save Asset". Your input action editor should now look like this:
Enable "Generate C# Wrapper Class" in the inspector for the asset and hit "Apply". Your inspector should now look something like this:
Add an instance of the generated C# wrapper class to your component.
public class MyComponent : MonoBehaviour { MyControls controls;
Create the instance and hook up a response to the fire action.
public void Awake() { controls = new MyControls(); controls.gameplay.fire.performed += ctx => Fire(); }
Enable and disable the action as appropriate.
public void OnEnable() { controls.Enable(); } public void OnDisable() { controls.Disable(); }
Hints
- To force button-like behavior on the control referenced in a binding, add a "Press" interaction to it.
You can access the control that triggered an action from the callback. Through it, you can also query its current value.
fireAction.performed += ctx => { var control = ctx.control; // Grab control. var value = ctx.GetValue<float>(); // Read value from control. // Can do control-specific checks. var button = control as ButtonControl; if (button != null && button.wasPressedThisFrame) /* ... */; }
UnityEngine.Input.GetAxisRaw
Not directly applicable. You can use access any InputControl<>.ReadUnprocessedValue()
to read unprocessed values from any control.
UnityEngine.Input.GetButton
See UnityEngine.Input.GetAxis
for how to set up a binding to a button or axis.
You can also use ButtonControl.isPressed
to detect if a button was pressed on the raw control without using bindings.
UnityEngine.input.GetButtonDown
See UnityEngine.Input.GetAxis
for how to set up a binding to a button or axis. You can use the Press
interaction to detect when a button is pressed.
You can also use ButtonControl.wasPressedThisFrame
to detect if a button was pressed on the raw control without using bindings.
UnityEngine.input.GetButtonUp
See UnityEngine.Input.GetAxis
for how to set up a binding to a button or axis. You can use the Press
interaction with a "Release Only" trigger to detect when a button is released.
You can also use ButtonControl.wasReleasedThisFrame
to detect if a button was pressed on the raw control without using bindings.
UnityEngine.Input.GetJoystickNames
There is no API that corresponds to this 100% (for good reason; GetJoystickNames
was never a good API).
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);
}
}
UnityEngine.Input.GetKey
Use 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: Keys are identified 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
.
UnityEngine.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: Keys are identified 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
.
UnityEngine.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: Keys are identified 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
.
UnityEngine.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
UnityEngine.Input.GetMouseButtonDown
Use ButtonControl.wasPressedThisFrame
on the corresponding mouse button:
Mouse.current.leftButton.wasPressedThisFrame
Mouse.current.rightButton.wasPressedThisFrame
Mouse.current.middleButton.wasPressedThisFrame
UnityEngine.Input.GetMouseButtonUp
Use ButtonControl.wasReleasedThisFrame
on the corresponding mouse button:
Mouse.current.leftButton.wasReleasedThisFrame
Mouse.current.rightButton.wasReleasedThisFrame
Mouse.current.middleButton.wasReleasedThisFrame
UnityEngine.Input.GetTouch
Use InputSystem.EnhancedTouch.Touch.activeTouches[i]
NOTE: Enable enhanced touch support first by calling
InputSystem.EnhancedTouch.Enable()
.
UnityEngine.Input.IsJoystickPreconfigured
Not directly applicable. But in general, you should assume that devices which derive from Gamepad
will correctly implement the mapping of axes and buttons to the corresponding InputControl
members of the Gamepad
class.
UnityEngine.Input.ResetInputAxes
Not directly applicable.
UnityEngine.TouchScreenKeyboard
No corresponding API yet. Use TouchScreenKeyboard
for now.