Getting Started: New Input System in Unity
Objective: Configure a Unity application to use the Input System
Understanding the New Input System
The new Input System is comprised of four architectural components that route events from player devices to your game code:
- Input Action Assets: A configuration file containing the properties of the actions and the bindings that go with them.
- Actions: The logical interpretations of the input are defined by actions. That information may be found in the Input Action Assets.
- Bindings: Bindings define the relationship between an action and the controls of an input device. This information is also available in the Input Action Assets.
- PlayerInput: A script that handles and connects action events to the relevant code logic.
Installing the New Input System
Open the Package Manager window by selecting the top menu drop-down Window > Package Manager. The select Unity Registry from the drop-down and search for the Input System package, then select the Install button to install the package.
Make sure to select ‘Yes’ to restart the Unity Editor or the New Input System will not be enabled.
Creating a PlayerInput Component & Input Actions
Getting input directly from an input device is quite easy with the new Input System. First lets go to the GameObject the player will control and add a PlayerInput Component.
Each PlayerInput Component represents one player in the game. The component must be linked to a set of Input Actions to be able to receive input. The fastest way to create a new set of actions is to click the Create Actions button in the Inspector window.
When you click Create Actions button a window will pop up requesting a name and folder to save the newly created .inputactions asset in your Project. Choose a name or just accept the defaults. Once you have saved the file a window to edit the Input Actions should appear. If the widow does not appear navigate the your Project view and double click the newly created .inputactions asset to open the window.
Don’t forget to assign the newly created input action asset to the PlayerInput Component or no matter what you do you input will not be received!
Implementing the Move Actions in Code
For this example we are going to keep the default set of actions provided when we created the input actions asset above.
Using the provided default actions we can easily setup the input system in our code.
One benefit of using the new Input System is actions for different control schemes can be tied to one function in script. This allows you to have for example a keyboard and gamepad without having to code each individually in script.