Scripting Actions (New Unity Input System)

Simon Pham
4 min readJan 5, 2024

--

In the previous blog posts, we set up Action Maps and Actions for the player and their dog. In today’s blog post, let’s delve into the scripting aspect of the Unity Input System.

Generating C# class for the Input Action Maps

The first step we need to take is to generate a C# class for the events and key bindings that were set up for the Action Maps. To do that, select the Input Asset in the Project window, and in the Inspector, check the “Generate C# Class” option and click “Apply”.

You will notice that a new C# script will be created for you, which will automatically update every time you modify the Input Asset.

Interacting with the new Input System

To keep things simple, I’m just gonna create a cube and name it “Player” and give it a C# script called “PlayerInput”. The process of working with an Input Action Map can be broken down into three simple steps:

Step 1: Get a reference to and initialize Input Actions
Step 2: Enable Input Action Map
Step 3: Register perform functions

Step 1: Get a reference to and initialize Input Actions

In order to communicate with the Input Actions that we created, we first need to get a reference to the Input Asset and initialize it. Here’s how to do it with code:

using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerInput : MonoBehaviour
{
private PlayerInputActions _input;

private void Start()
{
_input = new PlayerInputActions();
}
}

Step 2: Enable Input Action Map

Currently, we have two Action Maps: Player and Dog. To enable the Dog Action Map, we can just specify the name and use the Enable function.

_input.Dog.Enable();

Here’s the full code:

using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerInput : MonoBehaviour
{
private PlayerInputActions _input;

private void Start()
{
_input = new PlayerInputActions();
_input.Dog.Enable();
}
}

Step 3: Register perform functions

We’ve initialized the Input Asset and enabled the Action Map that we wanted to use, and now we need to tell Unity what we want to do when an action is performed. For example, I want to print a message when the Bark action is performed:

_input.Dog.Bark.performed += Bark_performed;  
private void Bark_performed(InputAction.CallbackContext context)
{
Debug.Log("Barking...");
}

We can quickly hit the Play button and test our code. When I press the Space button, we can see the message being printed in the Console.

If I want to register another function when the Bark action is finished, I can simply do this:

 _input.Dog.Bark.canceled += Bark_canceled;
private void Bark_canceled(InputAction.CallbackContext context)
{
Debug.Log("Done Barking!");
}

So when I press the Space key, the “Barking…” message is printed, and as soon as I release the Space key, the “Done Barking!” message is printed out.

This is particularly useful when you want different functions to execute at different times during a key press. For example, when the player hits a button, the character will start the charging process, and when they release that button, the character will fire a powered-up shot, as seen in Mega Man.

Here’s the full code:

using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerInput : MonoBehaviour
{
private PlayerInputActions _input;

private void Start()
{
_input = new PlayerInputActions();
_input.Dog.Enable();
_input.Dog.Bark.performed += Bark_performed;
_input.Dog.Bark.canceled += Bark_canceled;
}

private void Bark_performed(InputAction.CallbackContext context)
{
Debug.Log("Barking...");
}

private void Bark_canceled(InputAction.CallbackContext context)
{
Debug.Log("Done Barking!");
}
}

--

--