A game about forced loneliness, made by TACStudios
at master 120 lines 5.8 kB view raw view rendered
1--- 2uid: input-system-action-assets 3--- 4# Input Action Assets 5 6- [Creating Input Action Assets](#creating-input-action-assets) 7- [Editing Input Action Assets](#editing-input-action-assets) 8- [Using Input Action Assets](#using-input-action-assets) 9- [Type-safe C# API Generation](#type-safe-c-api-generation) 10 11An Input Action Asset is an Asset which contains a set of [Input Actions](Actions.md) definitions and their associated [Bindings](ActionBindings.md) and [Control Schemes](ActionBindings.md#control-schemes). These Assets have the `.inputactions` file extension and are stored in a plain JSON format. 12 13The input system creates an Action Asset when you set up the [default project-wide actions](ProjectWideActions.md), but you can also create new Action Assets directly in the Project window. 14 15For most common scenarios, you do not need to use more than one Input Action Asset. It is usually simpler to configure your project-wide action definition in the Project Settings window. 16 17 18## Creating Input Action Assets 19 20To create an Asset that contains [Input Actions](Actions.md) in Unity, right-click in the __Project__ window or go to __Assets > Create > Input Actions__ from Unity's main menu. 21 22## Editing Input Action Assets 23 24To bring up the Action editor, double-click an `.inputactions` Asset in the Project Browser, or select the __Edit Asset__ button in the Inspector for that Asset. You can have more than one editor window open at the same time, but not for the same Asset. 25 26The Actions Editor which opens is identical to the [Actions Editor in the Project Settings window](ActionsEditor.md). 27 28 29## Using Input Action Assets 30 31 32## Type-safe C# API Generation 33 34Input Action Assets allow you to **generate a C# class** from your action definitions, which allow you to refer to your actions in a type-safe manner from code. This means you can avoid looking up your actions by string. 35 36### Auto-generating script code for Actions 37 38One of the most convenient ways to work with `.inputactions` Assets in scripts is to automatically generate a C# wrapper class for them. This removes the need to manually look up Actions and Action Maps using their names, and also provides an easier way to set up callbacks. 39 40To enable this option, tick the __Generate C# Class__ checkbox in the importer properties in the Inspector of the `.inputactions` Asset, then select __Apply__. 41 42![MyPlayerControls Importer Settings](Images/FireActionInputAssetInspector.png) 43 44You can optionally choose a path name, class name, and namespace for the generated script, or keep the default values. 45 46This generates a C# script that simplifies working with the Asset. 47 48```CSharp 49using UnityEngine; 50using UnityEngine.InputSystem; 51 52// IGameplayActions is an interface generated from the "gameplay" action map 53// we added (note that if you called the action map differently, the name of 54// the interface will be different). This was triggered by the "Generate Interfaces" 55// checkbox. 56public class MyPlayerScript : MonoBehaviour, IGameplayActions 57{ 58 // MyPlayerControls is the C# class that Unity generated. 59 // It encapsulates the data from the .inputactions asset we created 60 // and automatically looks up all the maps and actions for us. 61 MyPlayerControls controls; 62 63 public void OnEnable() 64 { 65 if (controls == null) 66 { 67 controls = new MyPlayerControls(); 68 // Tell the "gameplay" action map that we want to get told about 69 // when actions get triggered. 70 controls.gameplay.SetCallbacks(this); 71 } 72 controls.gameplay.Enable(); 73 } 74 75 public void OnDisable() 76 { 77 controls.gameplay.Disable(); 78 } 79 80 public void OnUse(InputAction.CallbackContext context) 81 { 82 // 'Use' code here. 83 } 84 85 public void OnMove(InputAction.CallbackContext context) 86 { 87 // 'Move' code here. 88 } 89 90} 91``` 92 93>__Note__: To regenerate the .cs file, right-click the .inputactions asset in the Project Browser and choose "Reimport". 94 95### Using Action Assets with `PlayerInput` 96 97The [Player Input](PlayerInput.md) component provides a convenient way to handle input for one or multiple players. You can assign your Action Asset to the Player Input component so that it can then automatically handle activating Action Maps and selecting Control Schemes for you. 98 99![PlayerInput](Images/PlayerInput.png) 100 101### Modifying Input Action Assets at runtime 102There are several ways to modify an Input Action Asset at runtime. Any modifications that you make during Play mode to an Input Action Asset do not persist in the Input Action Asset after you exit Play mode. This means you can test your application in a realistic manner in the Editor without having to worry about inadvertently modifying the asset. For examples on how to modify an Input Action Asset, see the documentation on [Creating Actions in code](Actions.md#creating-actions-in-code) and [Changing Bindings](ActionBindings.md#changing-bindings). 103 104 105### The Default Actions Asset 106 107An asset called `DefaultInputActions.inputactions` containing a default setup of Actions comes with the Input System Package. You can reference this asset directly in your projects like any other Unity asset. However, the asset is also available in code form through the [`DefaultInputActions`](../api/UnityEngine.InputSystem.DefaultInputActions.html) class. 108 109```CSharp 110void Start() 111{ 112 // Create an instance of the default actions. 113 var actions = new DefaultInputActions(); 114 actions.Player.Look.performed += OnLook; 115 actions.Player.Move.performed += OnMove; 116 actions.Enable(); 117} 118``` 119 120> __Note:__ This default actions asset is older than, and entirely separate from the [default project-wide actions](ProjectWideActions.md). It is a legacy asset that remains included in the package for backward compatibility.