Unity’s New Input System
Unity’s traditional input system has relied on manually building long and convoluted if-else statements into the Update() loops of our code. This is all well and good when you have only a few commands and only recognise one or two input devices, but it rapidly becomes unwieldy as you add complexity. This is where Unity’s new Input System comes to the rescue.
Instead of defining your inputs where you use them, you now create a set of Actions and Bindings — the logical meanings of an input and the input device controls. Then you have a Player Input Script that sits between this list and your code logic, linking the two together. This allows you much more ease and flexibility in how you connect different input devices, such as:
- Keyboard
- Mouse
- Pen
- TouchScreen
- Sensor
- Joystick
- GamePad
So let’s get started!
The first thing you’ll probably need to do is to install the new system — at this point Unity does not enable it by default. Go to Window > Package Manager and look through the list of available packages for Input System. Click the install button. A dialog box will appear confirming that you want to enable the new input system and disable the old one, so click Yes. Unity will now restart and when it comes back you’ll have the new input system available.
Now that we have the Input System installed, it’s time to create an Input Action. Right-click in the Project window and select Create > Input Action. Give the new action a name such as “GameControl” and then double-click it. This opens up the Input Actions window where we can start setting things up.
This window has four important features:
- Action Maps: Groups of actions that occur in the game. You can group actions for different purposes, like player, gameplay or UI.
- Actions: The list of actions and bindings associated with the selected Action Map. In this panel, you create, modify or delete actions and bindings.
- Properties: Edit the action or binding properties in this panel, such as the type of action and the controls you associated with the binding.
- Save Assets: This is a very important function. You must click Save Asset after making any changes to the Input Action Asset. If you forget to save, the setting won’t work. Thus, you won’t see the expected result and may think there’s a bug in the code.
You can switch on Auto Save to prevent the problem, but it’s quite slow.
So now we’re set up with the new system and have a basic idea of how it all hangs together. In the next article, we’ll look at using it to implement some player movement.