Unity Input System

repository·develop·Indexed 23 days ago

https://github.com/unity-technologies/inputsystem

An extensible and customizable alternative to the legacy UnityEngine.Input API (package com.unity.inputsystem, version 1.20.1). It provides tools for configuring game actions, supporting custom input devices and binding composites, and managing local multiplayer via PlayerInput and PlayerInputManager. Features include the InputEventTrace API for recording and replaying input, VirtualMouseInput for gamepad-driven UI pointers, and utilities for creating rebinding interfaces and persisting user rebinds.

Tokens
123.9K
Snippets
152
Records
675
Agent score
80%

What's inside Unity Input System

  1. Overview of supported input device types

    develop

    The Unity Input System supports a wide variety of input devices, each requiring specific handling for its unique features (e.g., touchscreen touches or keyboard text input). You can configure and respond to these devices using the Actions Editor window or via scripting.

    Key device categories include:

    • Pointer devices: Mice, pens, or other devices tracking input on a 2D surface.
    • Keyboards: Devices using a set of key controls.
    • Joysticks: Devices with at least one input stick and one button.
    • Gamepads: Devices featuring a D-pad and four face buttons.
    • Sensors: Devices measuring environmental input.
    • HID (Human Interface Device): Peripheral devices connected via USB or Bluetooth following the HID specification.
  2. Implement responses to input in Unity

    develop

    To make your game react to user input, you must implement responses to the actions you have configured. There are several distinct patterns for responding to input depending on your requirements:

    • Action Callbacks: Set up an action to trigger specific code when a certain type of input occurs (e.g., a button press).
    • Polling: Repeatedly check the current status or value of an action within a loop (e.g., Update).
    • Player Input Component: Use this component to map actions to methods or callbacks in your scripts, and to handle local multiplayer scenarios like device filtering and screen-splitting.
    • Player Input Manager Component: Use this to manage local multiplayer games where multiple players share a single screen and multiple controllers.
    • Direct Device Reading: Directly read the state of a device's controls, bypassing the action system entirely.
    • Actions API: Use the scripting API to interact with actions programmatically.
  3. Use the Input Actions Editor to manage input configurations

    develop

    The Input Actions Editor is the primary tool for defining how user input is interpreted in your project. You can use it to:

    • Create Action Maps: Group related actions together (e.g., 'Player', 'UI', or 'Driving').
    • Configure Actions: Define the logical intent (e.g., 'Jump', 'Move', 'Fire').
    • Configure Bindings: Map specific physical controls (like a button press or axis movement) to actions.
    • Connect Controls: Link specific devices to your bindings.
    • Assign Interactions and Processors: Add logic to how inputs are handled, such as tap/hold interactions or deadzones for analog sticks.
  4. Identify supported input device types

    develop

    The Unity Input System categorizes supported hardware into several distinct types. Understanding these categories helps in determining which device-specific logic or layouts to implement in your project:

    • Pointers: Devices that track positions in 2D space (e.g., Mouse, Touch, Pen).
    • Keyboards: Devices providing key-based input.
    • Joysticks: Devices featuring at least one input stick and at least one button.
    • Gamepads: Standardized controllers typically featuring two thumbsticks, a D-pad, four face buttons, two shoulder buttons, and two trigger buttons. Common implementations include PlayStation gamepads, Switch gamepads, and Xbox gamepads.
    • Sensors: Devices that measure environmental characteristics like acceleration or orientation.

    Additionally, the Input System utilizes the Human Interface Device (HID) specification to generate layouts for controls connected via USB or Bluetooth.

  5. Access environmental data using the Sensor API

    develop

    The Sensor API allows you to measure environmental characteristics of the device running your application. You can use it to retrieve data such as device speed, orientation, or ambient temperature.

    To implement sensor functionality, you should follow these steps:

    1. Configure sensors: Set up the sensors to capture specific environmental information.
    2. Query sensors in code: Access the real-time data from the sensors connected to your application.
    3. Reference supported types: Check the supported sensor types to ensure the hardware you are targeting is compatible with the Input System.
  6. Configure and use gamepads in Unity

    develop

    The Unity Input System provides specialized support for gamepads, defined as devices typically featuring two thumbsticks, a D-pad, four face buttons, two shoulder buttons, and two triggers.

    Depending on your requirements, you can use the following workflows:

    • General Configuration: Use the standard gamepad introduction to set up devices with D-pads and face buttons.
    • Platform-Specific Configuration: Follow specific guides for PlayStation, Switch, or Xbox gamepads to ensure correct button mapping and features.
    • Programmatic Access: Use code to query connected gamepads, poll input on a frame-by-frame basis, or control haptics (rumble/motors).
    • Integration: Combine gamepad support with the Player Input component and Control Schemes to manage multiple device types seamlessly.
  7. Use Input System visualizer components

    develop

    The Visualizers sample provides two primary components for real-time monitoring of input states:

    • InputControlVisualizer: Used to monitor the state of individual InputControl elements.
    • InputActionVisualizer: Used to monitor the state of InputAction elements.

    Refer to the specific documentation for InputControlVisualizer and InputActionVisualizer for detailed usage instructions.

  8. Manage the Input Device lifecycle

    develop

    The Input System allows you to manage the lifecycle of Input Devices at runtime and in the Editor. You can perform the following core tasks to control how devices interact with your application:

    • Creation: Add new devices manually using InputSystem.AddDevice.
    • Removal: Remove disconnected or manually added devices and listen for changes via onDeviceChange notifications.
    • State Management: Reset controls to their default state using InputSystem.ResetDevice or request the current hardware state using RequestSyncCommand (on supported platforms).
    • Input Processing: Enable or disable devices to control whether they process input.
    • Focus & Backgrounding: Handle device behavior during focus loss, focus regain, or when the application moves to the background.
    • Editor Behavior: Understand how domain reloads in the Unity Editor recreate devices and reset their state.
  9. Check UI system compatibility with Input System

    develop

    Compatibility between the Input System and Unity's UI solutions depends on the UI system used and the Unity version.

    UI systemCompatibleUI Input Module component required
    UI Toolkit (2023.2+)YesNot required
    UI Toolkit (pre 2023.2)YesRequired
    Unity UI (uGUI)YesRequired
    IMGUINon/a

    Note: For UI Toolkit (2023.2+), UI actions defined in project-wide actions map directly to UI Toolkit input.

  10. Work with InputDevice instances from script

    develop

    The InputDevice class is the base class for all devices in the Input System. You can use it to interact with hardware or software-simulated devices.

    Depending on your goal, you may need different approaches:

    • Reading values: If you only need to get a device reference and read control values, use the Read devices directly workflow.
    • Advanced Scripting: For managing device lifecycles, matching descriptions to layouts, or sending commands, use the specialized scripting APIs described in the device scripting documentation.