Содержание
- Old vs New Input System APIs
- UnityEngine.Input
- UnityEngine.Input.acceleration
- UnityEngine.Input.accelerationEventCount
- UnityEngine.Input.accelerationEvents
- UnityEngine.Input.anyKey
- UnityEngine.Input.anyKeyDown
- UnityEngine.Input.backButtonLeavesApp
- UnityEngine.Input.compass
- UnityEngine.Input.compass.enabled
- UnityEngine.Input.compass.headingAccuracy
- UnityEngine.Input.compass.magneticHeading
- UnityEngine.Input.compass.rawVector
- UnityEngine.Input.compass.timestamp
- UnityEngine.Input.compass.trueHeading
- UnityEngine.Input.compensateSensors
- UnityEngine.Input.compositionCursorPos
- UnityEngine.Input.compositionString
- UnityEngine.Input.deviceOrientation
- UnityEngine.Input.gyro
- UnityEngine.Input.gyro.attitude
- UnityEngine.Input.gyro.enabled
- UnityEngine.Input.gyro.gravity
- UnityEngine.Input.gyro.rotationRate
- UnityEngine.Input.gyro.rotationRateUnbiased
- UnityEngine.Input.gyro.updateInterval
- UnityEngine.Input.gyro.userAcceleration
- UnityEngine.Input.imeCompositionMode
- UnityEngine.Input.imeIsSelected
- UnityEngine.Input.inputString
- UnityEngine.Input.location
- UnityEngine.Input.location.isEnabledByUser
- UnityEngine.Input.location.lastData
- UnityEngine.Input.location.lastData.altitude
- UnityEngine.Input.location.lastData.horizontalAccuracy
- UnityEngine.Input.location.lastData.latitude
- UnityEngine.Input.location.lastData.timestamp
- UnityEngine.Input.location.lastData.verticalAccuracy
- UnityEngine.Input.location.lastData.status
- UnityEngine.Input.location.Start
- UnityEngine.Input.location.Stop
- UnityEngine.Input.mousePosition
- UnityEngine.Input.mousePresent
- UnityEngine.Input.multiTouchEnabled
- UnityEngine.Input.simulateMouseWithTouches
- UnityEngine.Input.stylusTouchSupported
- UnityEngine.Input.touchCount
- UnityEngine.Input.touches
- UnityEngine.Input.touchPressureSupported
- UnityEngine.Input.touchSupported
- UnityEngine.Input.GetAccelerationEvent
- UnityEngine.Input.GetAxis
- UnityEngine.Input.GetAxisRaw
- UnityEngine.Input.GetButton
- UnityEngine.input.GetButtonDown
- UnityEngine.input.GetButtonUp
- UnityEngine.Input.GetJoystickNames
- UnityEngine.Input.GetKey
- UnityEngine.Input.GetKeyDown
- UnityEngine.Input.GetKeyUp
- UnityEngine.Input.GetMouseButton
- UnityEngine.Input.GetMouseButtonDown
- UnityEngine.Input.GetMouseButtonUp
- UnityEngine.Input.GetTouch
- UnityEngine.Input.IsJoystickPreconfigured
- UnityEngine.Input.ResetInputAxes
- UnityEngine.Handheld
Old vs New Input System APIs
This page provides a listing of the APIs in UnityEngine.Input
(and related APIs in UnityEngine
) and their corresponding APIs in the new input system.
The new APIs are currently in the UnityEngine.Experimental.Input
namespace. The namespace is omitted here for brevity. UnityEngine.Input
is referenced in full for easy disambiguation.
UnityEngine.Input
UnityEngine.Input.acceleration
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 listen to all input events and filter out state changes on Accelerometer
devices.
InputSystem.onEvent +=
eventPtr =>
{
if (eventPtr.IsA<StateEvent>() || eventPtr.IsA<DeltaStateEvent>())
{
var accelerometer = InputSystem.TryGetDeviceById(eventPtr.deviceId) as Accelerometer;
if (accelerometer != null)
{
var acceleration = accelerometer.acceleration.ReadValueFrom(eventPtr);
//...
}
}
}
////TODO: wrap in nicer APIs
UnityEngine.Input.anyKey
ATM this only exists for the keyboard, not yet in a way where it also covers mouse buttons.
Keyboard.current.anyKey.isPressed
////REVIEW: I think we want to have this in a way where it isn't just keyboard and mouse buttons but any meaningful "user triggered control"
UnityEngine.Input.anyKeyDown
No corresponding API yet.
UnityEngine.Input.backButtonLeavesApp
No corresponding API yet.
UnityEngine.Input.compass
Compass.current
UnityEngine.Input.compass.enabled
// Get.
Compass.current.enabled
// Set.
InputSystem.EnableDevice(Compass.current);
InputSystem.DisableDevice(Compass.current);
UnityEngine.Input.compass.headingAccuracy
No corresponding API yet.
UnityEngine.Input.compass.magneticHeading
No corresponding API yet.
UnityEngine.Input.compass.rawVector
No corresponding API yet.
UnityEngine.Input.compass.timestamp
Compass.current.lastUpdateTime
////TODO: time units don't match
UnityEngine.Input.compass.trueHeading
No corresponding API yet.
UnityEngine.Input.compensateSensors
No corresponding API yet.
UnityEngine.Input.compositionCursorPos
No corresponding API yet.
UnityEngine.Input.compositionString
No corresponding API yet.
UnityEngine.Input.deviceOrientation
No corresponding API yet.
UnityEngine.Input.gyro
Gyro.current
UnityEngine.Input.gyro.attitude
Gyro.current.orientation.ReadValue()
UnityEngine.Input.gyro.enabled
// Get.
Gyro.current.enabled
// Set.
InputSystem.EnableDevice(Gyro.current);
InputSystem.DisableDevice(Gyro.current);
UnityEngine.Input.gyro.gravity
Gyro.current.gravity.ReadValue()
UnityEngine.Input.gyro.rotationRate
Gyro.current.angularVelocity.ReadValue()
UnityEngine.Input.gyro.rotationRateUnbiased
No corresponding API yet.
UnityEngine.Input.gyro.updateInterval
Gyro.current.samplingFrequency = 30.0f; // 30 Hz.
////FIXME: time units don't match
UnityEngine.Input.gyro.userAcceleration
Gyro.current.acceleration.ReadValue()
UnityEngine.Input.imeCompositionMode
No corresponding API yet.
UnityEngine.Input.imeIsSelected
No corresponding API yet.
UnityEngine.Input.inputString
Keyboard.current.onText +=
character => /* ... */;
UnityEngine.Input.location
No corresponding API yet.
UnityEngine.Input.location.isEnabledByUser
No corresponding API yet.
UnityEngine.Input.location.lastData
No corresponding API yet.
UnityEngine.Input.location.lastData.altitude
No corresponding API yet.
UnityEngine.Input.location.lastData.horizontalAccuracy
No corresponding API yet.
UnityEngine.Input.location.lastData.latitude
No corresponding API yet.
UnityEngine.Input.location.lastData.timestamp
No corresponding API yet.
UnityEngine.Input.location.lastData.verticalAccuracy
No corresponding API yet.
UnityEngine.Input.location.lastData.status
No corresponding API yet.
UnityEngine.Input.location.Start
No corresponding API yet.
UnityEngine.Input.location.Stop
No corresponding API yet.
UnityEngine.Input.mousePosition
Mouse.current.position.ReadValue()
NOTE: Mouse simulation from touch is not implemented yet.
UnityEngine.Input.mousePresent
Mouse.current != null
UnityEngine.Input.multiTouchEnabled
No corresponding API yet.
UnityEngine.Input.simulateMouseWithTouches
No corresponding API yet.
UnityEngine.Input.stylusTouchSupported
No corresponding API yet.
UnityEngine.Input.touchCount
Touchscreen.current.activeTouches.Count
UnityEngine.Input.touches
Touchscreen.current.activeTouches
UnityEngine.Input.touchPressureSupported
No corresponding API yet.
UnityEngine.Input.touchSupported
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.
DISCLAIMER: The action part of the input system is still very unfinished and will change.
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 using the "Pick" button 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.
-
Select the asset and in the inspector, click "Add New Set" to add a new set of actions.
-
Double-click the "default" name to give the set a better name. E.g. "gameplay".
-
Double-click "<Add Action...>" to add an action.
-
Double-click the action to give it a name.
-
Add bindings to the action by clicking the plus sign and using the "Pick" button to select controls.
-
Enable "Generate C# Wrapper Class" in the importer settings and hit "Apply". Your inspector should now look something like this:
-
Add a field to your component to reference the asset using the generated C# wrapper.
public class MyComponent : MonoBehaviour { public MyControls controls;
-
Drag the .inputactions asset you created onto the field in the inspector.
-
Hook up a response to the fire action.
public void Awake()
{
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" modified 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
No corresponding API yet.
UnityEngine.Input.GetButton
See UnityEngine.Input.GetAxis
.
UnityEngine.input.GetButtonDown
See UnityEngine.Input.GetAxis
.
UnityEngine.input.GetButtonUp
See UnityEngine.Input.GetAxis
.
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
// 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
// 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
// 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
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
Mouse.current.leftButton.wasPressedThisFrame
Mouse.current.rightButton.wasPressedThisFrame
Mouse.current.middleButton.wasPressedThisFrame
UnityEngine.Input.GetMouseButtonUp
Mouse.current.leftButton.wasReleasedThisFrame
Mouse.current.rightButton.wasReleasedThisFrame
Mouse.current.middleButton.wasReleasedThisFrame
UnityEngine.Input.GetTouch
Touchscreen.current.activeTouches[i]
UnityEngine.Input.IsJoystickPreconfigured
No corresponding API yet.
UnityEngine.Input.ResetInputAxes
No corresponding API yet.