Library -Artifacts
5.0 KiB
Quick start guide
Note: For information on how to install the new Input System, see Installation.
Getting input directly from an Input Device
The quickest way to get started in script is to read the current state directly from Input Devices. For example, the following code gets the gamepad that a player last used, and reads its current state:
using UnityEngine;
using UnityEngine.InputSystem;
public class MyPlayerScript : MonoBehaviour
{
void Update()
{
var gamepad = Gamepad.current;
if (gamepad == null)
return; // No gamepad connected.
if (gamepad.rightTrigger.wasPressedThisFrame)
{
// 'Use' code here
}
Vector2 move = gamepad.leftStick.ReadValue();
// 'Move' code here
}
}
The same approach works for other Device types (for example, Keyboard.current
or Mouse.current
).
Getting input indirectly through an Input Action
To get input directly through an Input Action, follow these steps:
- Add a
PlayerInput
component. - Create Actions.
- Script Action responses.
Step 1: Add a PlayerInput
Component
Getting input directly from an Input Device is quick and convenient, but requires a separate path for each type of Device. That also makes it harder to later change which Device Control triggers a specific event in the game.
Alternatively, you can use Actions as an intermediary between Devices and the in-game responses they trigger. The easiest way to do this is to use the PlayerInput
component. To add this component, click the Add Component button in the GameObject Inspector:
Step 2: Create Input Actions
Each PlayerInput
component represents one player in the game. To receive input, the component must be connected to a set of Input Actions. The quickest way to create a new set of Actions is to click the Create Actions… button in the Inspector window for that component. This creates an Asset pre-populated with a default set of Input Action Maps, Input Actions, and Input Bindings.
When you click the Create Actions button, Unity asks you where to create the new Asset. Choose a name and folder inside the Assets
folder of your Project (or just accept the defaults) and select Okay. This creates a new .inputactions
Asset in your Project, connects it to the PlayerInput
component, and brings up the editor window for .inputactions
files.
You can edit the default set to fit the needs of your Project. See the in-depth documentation for the Action editor for instructions on how to use this window.
Step 3: Setting up Action responses
Once the component has its Actions, you must set up a response for each Action. PlayerInput
allows you to set up responses in several ways, using the Behavior property in the Inspector window:
For more details about the options, see documentation on notification behaviors. The screenshot above uses Invoke Unity Events, which uses UnityEvent
in the same way the Unity UI does. Unity displays an event for each Action that is linked to the component. This allows you to directly wire in the target method for each event.
Each method takes an InputAction.CallbackContext
argument that gives access to the Control that triggered the Action and the Action's value. For more information, see documentation on Action callbacks.
public class MyPlayerScript : MonoBehaviour
{
public void Fire(InputAction.CallbackContext context)
{
Debug.Log("Fire!");
}
}
This completes the basic setup using PlayerInput
.
Alternate ways to set up Input Actions
There are ways other than PlayerInput
to set up Input Actions. For more information, see documentation on Creating Actions.