Working with Unity New Input System
Scripting with the Action Maps
Continuing on from the previous article about Unity’s New Input System, we have understood how the new Action Map works and how it is able to control and also organize the New Input System. Now it is time to learn how to apply the action map system into our game by converting the Action Map Sequence into an scriptable function.
Converting Action Map Into Usable Script:
As you might have figured out, the Action Map work as a separate entity from the release of other Unity functions. In order to be able to script with the Action Map Inputs, we first need to convert the Action Maps into a an scriptable class.
To achieve this we must save our settings for the Action Map, then we are going to select the Action Map component within the Projects tab. The Hierarchy should show an updated info about the Action Map, and within there you should be able to select a check box that allows you to convert to the Action Map into an scriptable object, there is also an input slow for the name of the script that is going to be created. (by default I usually use whatever name was also given when creating the Action Map). Once you hit Apply, you will notice an brand new C# script being created into the Projects tab. With this new scriptable class, your scripts will be able to access the Action Map Inputs.
Also if you were to change anything or add anything within the Action Map after the script is created, as long as you save the Action Map, the scriptable class will also be updated with the new changes.
Applying Action Map Input into your Scripts:
Now that we have access to the Action Map Input, we can finally start applying them into our scripts. We can start by working with the basic button inputs. For this demonstration I decided to create an new script called “Player_Input”.
Before we can start using the Action Map Inputs, we first need to gain access to the New Unity Input Library. The library name is called Unity Engine Input System.
Unlike the previous version of the Input system, the new version works in a much different manor. There is several step to make the Input system functional.
- We need to gain access to the correct Action Map Input, this is important if you have multiple Action Maps (aka. Player movement, Vehicle Movement, Menu Input etc.)
- We then need to enable that Action Map
- Finally we need to assign what type of Action we want to trigger (aka, bark, jump, move etc.) to a toggle action (pressed, released, hold etc.)
To gain access to the Action Map Input I simply have to create an new Private local variable and name the Action Map script that was created not long ago and give it a local name. Then I assign the local hander with an new Input Control.
To enable the Action Map we then need to have the new input controls access the correct the name of the Action Input we require. (aka, in this demonstration I only have one Action Input, but if there was other type of input controls I would need the appropriate name space here) In this example I have the Input “Dog_Controls” (since that is the Action Input I want to be active for this script. Following that with the “Enable()” the action map will now be active during this scene.
Finally it is time to tell the input system what to do within the script. In this example, I want the controls for movement to be “preformed”.
To understand what “Preformed” means and does we first need to understand the new concept of “pressed, released and hold” are. In the New Unity Input system we now have new names for those functions called “Started, Preformed and Ended”. As the name suggest, Started and Ended can be crossed reference to Pressed and Released. While Performed is similar to the “Hold” function. So with that example above with the movement input, I am basically asking Unity to check whenever I have the movement controls active and triggered I will have the “Movement_performed” method toggled.
The “Movement_performed” method is actually created by Unity itself and by default should come with an auto fill feature within Visual Studios. Unity will also create a new method with an Input Action Call back function which is basically the “task” that occurs whenever the buttons link to the “Movement” Action Map is preformed.
In this example I just place a basic Debug Log that states Movement has occurred. If we save this script and head back into Unity and drag this script into an empty game object or player object and test it out. You will see that there will be an log of inputs within Console.
This means that the New input system is now active and working.