Game Development
Input Action asset
Learn to create an Input Action asset and define actions.
What is an Input Action asset?
An Input Action asset is an asset that keeps actions and bindings in one file; its extension is .inputactions. You create it with Create > Input Actions in the Project window, and double-clicking opens the Input Actions editor.
{
"name": "Jump",
"type": "Button",
"bindings": [
{ "path": "<Keyboard>/space" }
]
}
The file content is JSON, so it is easy to track with version control.
Action Map, Action and Binding
An Action Map collects the actions of one game state; for example the Player map holds movement and jumping. An action is a single input function and its type is Button, Value or Pass Through. A binding connects the action to a physical control, and composites like 2DVector merge the WASD keys into a single Vector2 value.
moveAction.AddBinding("<Gamepad>/leftStick");
moveAction.AddCompositeBinding("2DVector")
.With("Up", "<Keyboard>/w");
Exercise Define a Jump action
Write a MonoBehaviour named EylemTanimla. In Start create an InputAction named Jump with type InputActionType.Button. Declare a string array named yollar holding <Keyboard>/space and <Gamepad>/buttonSouth. Loop over the array with foreach; for each path call jump.AddBinding(yol) and print it with Debug.Log("Baglama: " + yol). Finally call jump.Enable().
Show example solution
using UnityEngine;
using UnityEngine.InputSystem;
public class EylemTanimla : MonoBehaviour
{
void Start()
{
InputAction jump = new InputAction(name: "Jump", type: InputActionType.Button);
string[] yollar = { "<Keyboard>/space", "<Gamepad>/buttonSouth" };
foreach (string yol in yollar)
{
jump.AddBinding(yol);
Debug.Log("Baglama: " + yol);
}
jump.Enable();
}
}
Test yourself
-
What binding path connects the space key to the Jump action?
- <Mouse>/leftButton
- <Keyboard>/space
- <Keyboard>/escape
- <Gamepad>/buttonSouth
Show answer
<Keyboard>/space — A binding path joins the device name and the control name; the space key is the space control of the Keyboard device, so the path is <Keyboard>/space.
-
Bind the left analog stick
moveAction.AddBinding("____");
Show answer
moveAction.AddBinding("<Gamepad>/leftStick");
-
Action asset parts
- Action Map
- The section that groups actions
- Action
- A single input function
- Binding
- Link from action to physical control
- Composite
- WASD keys merging into one action
Next lesson: The PlayerInput component