From 7c9abaabe45a4faa758b3c5504aab014eb202f69 Mon Sep 17 00:00:00 2001 From: ChoshikaBagratee Date: Wed, 12 Aug 2026 10:20:47 -0400 Subject: [PATCH 1/6] Add UsingActionsWorkflow.cs files --- .../UsingActionsWorkflowExamples.cs | 26 +++++++++++ .../UsingActionsWorkflowFullExample.cs | 30 +++++++++++++ .../Documentation~/using-actions-workflow.md | 44 +++---------------- 3 files changed, 61 insertions(+), 39 deletions(-) create mode 100644 Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowExamples.cs create mode 100644 Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowFullExample.cs diff --git a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowExamples.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowExamples.cs new file mode 100644 index 0000000000..dafcc745ed --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowExamples.cs @@ -0,0 +1,26 @@ +#region using +using UnityEngine.InputSystem; +#endregion +using UnityEngine.InputSystem; + +public class Example : MonoBehaviour +{ + #region InputAction_variables + InputAction moveAction; + InputAction jumpAction; + #endregion + + private void Start() { + #region FindAction + moveAction = InputSystem.actions.FindAction("Move"); + jumpAction = InputSystem.actions.FindAction("Jump"); + #endregion + } + + private void Update() { + #region ReadActionValues + Vector2 moveValue = moveAction.ReadValue(); + bool jumpValue = jumpAction.IsPressed(); + #endregion + } +} diff --git a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowFullExample.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowFullExample.cs new file mode 100644 index 0000000000..991588e494 --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowFullExample.cs @@ -0,0 +1,30 @@ +using UnityEngine; +using UnityEngine.InputSystem; + +public class Example : MonoBehaviour +{ + // These variables are to hold the Action references + InputAction moveAction; + InputAction jumpAction; + + private void Start() + { + // Find the references to the "Move" and "Jump" actions + moveAction = InputSystem.actions.FindAction("Move"); + jumpAction = InputSystem.actions.FindAction("Jump"); + } + + void Update() + { + // Read the "Move" action value, which is a 2D vector + // and the "Jump" action state, which is a boolean value + + Vector2 moveValue = moveAction.ReadValue(); + // your movement code here + + if (jumpAction.IsPressed()) + { + // your jump code here + } + } +} diff --git a/Packages/com.unity.inputsystem/Documentation~/using-actions-workflow.md b/Packages/com.unity.inputsystem/Documentation~/using-actions-workflow.md index ceeec423c5..a64ec9cb91 100644 --- a/Packages/com.unity.inputsystem/Documentation~/using-actions-workflow.md +++ b/Packages/com.unity.inputsystem/Documentation~/using-actions-workflow.md @@ -45,57 +45,23 @@ To use `FindAction` to get references to your Actions and read user input in you 1. Create a new C# script in Unity. 1. Add the Input System's "using" statement to the top of your script. This allows you to use the Input System API throughout the rest of your script: - using UnityEngine.InputSystem + [!code-cs[using](Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowExamples.cs#using)] 1. Create some variables of type `InputAction` in your class body, one for each Action that you want to use in your script. These will store the references to each Action. A good naming convention is to add the word Action to the name of the action. For example: - InputAction moveAction; - InputAction jumpAction; + [!code-cs[InputAction_variables](Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowExamples.cs#InputAction_variables)] 1. In your Start() method, use `FindAction` to find the reference to each action and store it in its respective variable, for example: - moveAction = InputSystem.actions.FindAction("Move"); - jumpAction = InputSystem.actions.FindAction("Jump"); + [!code-cs[FindAction](Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowExamples.cs#FindAction)] 1. In your Update() method, read the value from your action variables. This allows you to write code that reads the latest values coming from your Actions each frame and respond accordingly.

The way you read a value depends on the Action's **value type**. For example some actions might return a 1D or 2D axis value, and other actions might return a Boolean true/false value. In this example, the **Move** action returns a 2D axis, and the **Jump** action returns a Boolean. - Vector2 moveValue = moveAction.ReadValue(); - bool jumpValue = jumpAction.IsPressed(); + [!code-cs[ReadActionValues](Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowExamples.cs#ReadActionValues)] The following example script shows all these steps combined together into a single script: -```CSharp -using UnityEngine; -using UnityEngine.InputSystem; - -public class Example : MonoBehaviour -{ - // These variables are to hold the Action references - InputAction moveAction; - InputAction jumpAction; - - private void Start() - { - // Find the references to the "Move" and "Jump" actions - moveAction = InputSystem.actions.FindAction("Move"); - jumpAction = InputSystem.actions.FindAction("Jump"); - } - - void Update() - { - // Read the "Move" action value, which is a 2D vector - // and the "Jump" action state, which is a boolean value - - Vector2 moveValue = moveAction.ReadValue(); - // your movement code here - - if (jumpAction.IsPressed()) - { - // your jump code here - } - } -} -``` +[!code-cs[fullexmaple](Packages\com.unity.inputsystem\DocCodeSamples.Tests\UsingActionsWorkflowFullExample.cs)] > [!TIP] > Aavoid using `FindAction` in your `Update()` loop, because it performs a string-based lookup which could impact performance. This is why the Action references in the example above are found during the Start() function, and stored in variables after finding them. From 2cd98fb7c5d52a485456679ddaba6f26265b4446 Mon Sep 17 00:00:00 2001 From: ChoshikaBagratee Date: Wed, 12 Aug 2026 10:34:57 -0400 Subject: [PATCH 2/6] Update UsingActionsWorkflowExamples.cs --- .../UsingActionsWorkflowExamples.cs | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowExamples.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowExamples.cs index dafcc745ed..8780d6ca33 100644 --- a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowExamples.cs +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowExamples.cs @@ -1,26 +1,29 @@ +using UnityEngine; #region using using UnityEngine.InputSystem; #endregion -using UnityEngine.InputSystem; -public class Example : MonoBehaviour +namespace DocCodeSamples.Tests { - #region InputAction_variables - InputAction moveAction; - InputAction jumpAction; - #endregion + internal class UsingActionsWorkflowExamples : MonoBehaviour + { + #region InputAction_variables + InputAction moveAction; + InputAction jumpAction; + #endregion - private void Start() { - #region FindAction - moveAction = InputSystem.actions.FindAction("Move"); - jumpAction = InputSystem.actions.FindAction("Jump"); - #endregion - } + private void Start() { + #region FindAction + moveAction = InputSystem.actions.FindAction("Move"); + jumpAction = InputSystem.actions.FindAction("Jump"); + #endregion + } - private void Update() { - #region ReadActionValues - Vector2 moveValue = moveAction.ReadValue(); - bool jumpValue = jumpAction.IsPressed(); - #endregion + private void Update() { + #region ReadActionValues + Vector2 moveValue = moveAction.ReadValue(); + bool jumpValue = jumpAction.IsPressed(); + #endregion + } } } From bbe5f731b8ce2ae7817c7616dce86f89129e35aa Mon Sep 17 00:00:00 2001 From: ChoshikaBagratee Date: Wed, 12 Aug 2026 10:38:34 -0400 Subject: [PATCH 3/6] Fix code sample path Corrected file path syntax in documentation and improved performance tip. --- .../Documentation~/using-actions-workflow.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Packages/com.unity.inputsystem/Documentation~/using-actions-workflow.md b/Packages/com.unity.inputsystem/Documentation~/using-actions-workflow.md index a64ec9cb91..6b57ea93e7 100644 --- a/Packages/com.unity.inputsystem/Documentation~/using-actions-workflow.md +++ b/Packages/com.unity.inputsystem/Documentation~/using-actions-workflow.md @@ -61,7 +61,7 @@ To use `FindAction` to get references to your Actions and read user input in you The following example script shows all these steps combined together into a single script: -[!code-cs[fullexmaple](Packages\com.unity.inputsystem\DocCodeSamples.Tests\UsingActionsWorkflowFullExample.cs)] +[!code-cs[fullexmaple](Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowFullExample.cs)] > [!TIP] > Aavoid using `FindAction` in your `Update()` loop, because it performs a string-based lookup which could impact performance. This is why the Action references in the example above are found during the Start() function, and stored in variables after finding them. From c84ae4376551964b9ab7f67d090b02ee3bcab9a4 Mon Sep 17 00:00:00 2001 From: ChoshikaBagratee Date: Wed, 12 Aug 2026 11:20:13 -0400 Subject: [PATCH 4/6] Add testable files --- .../AboutProjectWideActions.cs | 12 +++++ .../DocCodeSamples.Tests/BindingConflicts.cs | 40 ++++++++++++++++ .../DocCodeSamples.Tests/DefaultActions.cs | 26 +++++++++++ .../GenerateCsApiFromActions.cs | 43 +++++++++++++++++ .../DocCodeSamples.Tests/QuickStartGuide.cs | 35 ++++++++++++++ .../UsingDirectWorkflow.cs | 24 ++++++++++ .../UsingPlayerinputWorkflow.cs | 28 +++++++++++ .../about-project-wide-actions.md | 4 +- .../Documentation~/binding-conflicts.md | 32 +------------ .../Documentation~/default-actions.md | 11 +---- .../generate-cs-api-from-actions.md | 46 +------------------ .../Documentation~/quick-start-guide.md | 33 +------------ .../Documentation~/using-direct-workflow.md | 27 +---------- .../using-playerinput-workflow.md | 31 +------------ 14 files changed, 215 insertions(+), 177 deletions(-) create mode 100644 Packages/com.unity.inputsystem/DocCodeSamples.Tests/AboutProjectWideActions.cs create mode 100644 Packages/com.unity.inputsystem/DocCodeSamples.Tests/BindingConflicts.cs create mode 100644 Packages/com.unity.inputsystem/DocCodeSamples.Tests/DefaultActions.cs create mode 100644 Packages/com.unity.inputsystem/DocCodeSamples.Tests/GenerateCsApiFromActions.cs create mode 100644 Packages/com.unity.inputsystem/DocCodeSamples.Tests/QuickStartGuide.cs create mode 100644 Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingDirectWorkflow.cs create mode 100644 Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingPlayerinputWorkflow.cs diff --git a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/AboutProjectWideActions.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/AboutProjectWideActions.cs new file mode 100644 index 0000000000..99ee49ef9c --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/AboutProjectWideActions.cs @@ -0,0 +1,12 @@ +using UnityEngine; +using UnityEngine.InputSystem; + +public class AboutProjectWideActions : MonoBehaviour +{ + void Start() + { + #region about-project-wide-actions + InputSystem.actions.FindAction("Move"); + #endregion + } +} diff --git a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/BindingConflicts.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/BindingConflicts.cs new file mode 100644 index 0000000000..4417fac872 --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/BindingConflicts.cs @@ -0,0 +1,40 @@ +using UnityEngine; +using UnityEngine.InputSystem; + +class BindingConflictsExample : InputTestFixture +{ + public void Example() + { + #region bindingConflicts + // Create two actions in the same map. + var map = new InputActionMap(); + var bAction = map.AddAction("B"); + var shiftbAction = map.AddAction("ShiftB"); + + // Bind one of the actions to 'B' and the other to 'SHIFT+B'. + bAction.AddBinding("/b"); + shiftbAction.AddCompositeBinding("OneModifier") + .With("Modifier", "/shift") + .With("Binding", "/b"); + + // Print something to the console when the actions are triggered. + bAction.performed += _ => Debug.Log("B action performed"); + shiftbAction.performed += _ => Debug.Log("SHIFT+B action performed"); + + // Start listening to input. + map.Enable(); + + // Now, let's assume the left shift key on the keyboard is pressed (here, we manually + // press it with the InputTestFixture API). + Press(Keyboard.current.leftShiftKey); + + // And then the B is pressed. This is a valid input for both + // bAction as well as shiftbAction. + // + // What will happen now is that shiftbAction will do its processing first. In response, + // it will *perform* the action (That is, we see the `performed` callback being invoked) and + // thus "consume" the input. bAction will stay silent as it will in turn be skipped over. + Press(Keyboard.bKey); + #endregion + } +} \ No newline at end of file diff --git a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/DefaultActions.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/DefaultActions.cs new file mode 100644 index 0000000000..3097df15c1 --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/DefaultActions.cs @@ -0,0 +1,26 @@ +using UnityEngine; +using UnityEngine.InputSystem; + +public class DefaultActions : MonoBehaviour +{ + #region default-actions + void Start() + { + // Create an instance of the default actions. + var actions = new DefaultInputActions(); + actions.Player.Look.performed += OnLook; + actions.Player.Move.performed += OnMove; + actions.Enable(); + } + #endregion + + void OnLook(InputAction.CallbackContext context) + { + // your look code here + } + + void OnMove(InputAction.CallbackContext context) + { + // your move code here + } +} \ No newline at end of file diff --git a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/GenerateCsApiFromActions.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/GenerateCsApiFromActions.cs new file mode 100644 index 0000000000..941bb4d143 --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/GenerateCsApiFromActions.cs @@ -0,0 +1,43 @@ +using UnityEngine; +using UnityEngine.InputSystem; + +// IGameplayActions is an interface generated from the newly added "gameplay" +// action map, triggered by the "Generate Interfaces" checkbox. Note that if +// you change the default values for the action map, the name of the interface +// will be different. + +public class MyPlayerScript : MonoBehaviour, IGameplayActions +{ + // MyPlayerControls is the C# class that Unity generated. + // It encapsulates the data from the .inputactions asset we created + // and automatically looks up all the maps and actions for us. + MyPlayerControls controls; + + public void OnEnable() + { + if (controls == null) + { + controls = new MyPlayerControls(); + // Tell the "gameplay" action map that we want to be + // notified when actions get triggered. + controls.gameplay.SetCallbacks(this); + } + controls.gameplay.Enable(); + } + + public void OnDisable() + { + controls.gameplay.Disable(); + } + + public void OnUse(InputAction.CallbackContext context) + { + // 'Use' code here. + } + + public void OnMove(InputAction.CallbackContext context) + { + // 'Move' code here. + } + +} \ No newline at end of file diff --git a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/QuickStartGuide.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/QuickStartGuide.cs new file mode 100644 index 0000000000..f476ccd5b2 --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/QuickStartGuide.cs @@ -0,0 +1,35 @@ +namespace DocCodeSamples.Tests +{ +#region quick-start-guide +using UnityEngine; +using UnityEngine.InputSystem; // 1. The Input System "using" statement + +public class Example : MonoBehaviour +{ + // 2. These variables are to hold the Action references + InputAction moveAction; + InputAction jumpAction; + + private void Start() + { + // 3. Find the references to the "Move" and "Jump" actions + moveAction = InputSystem.actions.FindAction("Move"); + jumpAction = InputSystem.actions.FindAction("Jump"); + } + + void Update() + { + // 4. Read the "Move" action value, which is a 2D vector + // and the "Jump" action state, which is a boolean value + + Vector2 moveValue = moveAction.ReadValue(); + // your movement code here + + if (jumpAction.IsPressed()) + { + // your jump code here + } + } +} +#endregion +} \ No newline at end of file diff --git a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingDirectWorkflow.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingDirectWorkflow.cs new file mode 100644 index 0000000000..016a40374b --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingDirectWorkflow.cs @@ -0,0 +1,24 @@ +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 + } + } +} \ No newline at end of file diff --git a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingPlayerinputWorkflow.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingPlayerinputWorkflow.cs new file mode 100644 index 0000000000..81b6a48b50 --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingPlayerinputWorkflow.cs @@ -0,0 +1,28 @@ +using UnityEngine; +using UnityEngine.InputSystem; + +// This script is designed to have the OnMove and +// OnJump methods called by a PlayerInput component + +public class ExampleScript : MonoBehaviour +{ + Vector2 moveAmount; + + public void OnMove(InputAction.CallbackContext context) + { + // read the value for the "move" action each event call + moveAmount = context.ReadValue(); + } + + public void OnJump(InputAction.CallbackContext context) + { + // your jump code goes here. + } + + public void Update() + { + // to use the Vector2 value from the "move" action each + // frame, use the "moveAmount" variable here. + } + +} \ No newline at end of file diff --git a/Packages/com.unity.inputsystem/Documentation~/about-project-wide-actions.md b/Packages/com.unity.inputsystem/Documentation~/about-project-wide-actions.md index df727a5287..2c101b6e94 100644 --- a/Packages/com.unity.inputsystem/Documentation~/about-project-wide-actions.md +++ b/Packages/com.unity.inputsystem/Documentation~/about-project-wide-actions.md @@ -24,8 +24,6 @@ The benefit of assign an action asset as the project-wide actions is that you ca For example, you can get a reference to an action named "Move" in your project-wide actions using a line of code like this: -``` - InputSystem.actions.FindAction("Move"); -``` + [!code-cs[project-wide actions](Packages/com.unity.inputsystem/DocCodeSamples.Tests/AboutProjectWideActions.cs#about-project-wide-actions)] Project-wide actions are also enabled by default. diff --git a/Packages/com.unity.inputsystem/Documentation~/binding-conflicts.md b/Packages/com.unity.inputsystem/Documentation~/binding-conflicts.md index bc9e96ddd8..1860a26ead 100644 --- a/Packages/com.unity.inputsystem/Documentation~/binding-conflicts.md +++ b/Packages/com.unity.inputsystem/Documentation~/binding-conflicts.md @@ -62,34 +62,4 @@ By using the **Pass Through** action type, conflict resolution is bypassed, whic The following example illustrates how this works at the API level. -```CSharp -// Create two actions in the same map. -var map = new InputActionMap(); -var bAction = map.AddAction("B"); -var shiftbAction = map.AddAction("ShiftB"); - -// Bind one of the actions to 'B' and the other to 'SHIFT+B'. -bAction.AddBinding("/b"); -shiftbAction.AddCompositeBinding("OneModifier") - .With("Modifier", "/shift") - .With("Binding", "/b"); - -// Print something to the console when the actions are triggered. -bAction.performed += _ => Debug.Log("B action performed"); -shiftbAction.performed += _ => Debug.Log("SHIFT+B action performed"); - -// Start listening to input. -map.Enable(); - -// Now, let's assume the left shift key on the keyboard is pressed (here, we manually -// press it with the InputTestFixture API). -Press(Keyboard.current.leftShiftKey); - -// And then the B is pressed. This is a valid input for both -// bAction as well as shiftbAction. -// -// What will happen now is that shiftbAction will do its processing first. In response, -// it will *perform* the action (That is, we see the `performed` callback being invoked) and -// thus "consume" the input. bAction will stay silent as it will in turn be skipped over. -Press(keyboard.bKey); -``` +[!code-cs[bindingconflicts](Packages/com.unity.inputsystem/DocCodeSamples.Tests/BindingConflicts.cs#bindingConflicts)] diff --git a/Packages/com.unity.inputsystem/Documentation~/default-actions.md b/Packages/com.unity.inputsystem/Documentation~/default-actions.md index c514573edb..12a2430e36 100644 --- a/Packages/com.unity.inputsystem/Documentation~/default-actions.md +++ b/Packages/com.unity.inputsystem/Documentation~/default-actions.md @@ -21,13 +21,4 @@ These default actions mean that in many cases, you can start scripting with the The Input System package provides an asset called `DefaultInputActions.inputactions` which you can reference directly in your projects like any other Unity asset. The asset is also available in code form through the [`DefaultInputActions`](xref:UnityEngine.InputSystem.DefaultInputActions) class. -```CSharp -void Start() -{ - // Create an instance of the default actions. - var actions = new DefaultInputActions(); - actions.Player.Look.performed += OnLook; - actions.Player.Move.performed += OnMove; - actions.Enable(); -} -``` +[!code-cs[default-actions](Packages/com.unity.inputsystem/DocCodeSamples.Tests/DefaultActions.cs#default-actions)] diff --git a/Packages/com.unity.inputsystem/Documentation~/generate-cs-api-from-actions.md b/Packages/com.unity.inputsystem/Documentation~/generate-cs-api-from-actions.md index 80aed61fc8..9bd6a3387e 100644 --- a/Packages/com.unity.inputsystem/Documentation~/generate-cs-api-from-actions.md +++ b/Packages/com.unity.inputsystem/Documentation~/generate-cs-api-from-actions.md @@ -24,51 +24,7 @@ You can optionally choose a path name, class name, and namespace for the generat Once applied, the Input System creates a C# script containing API that matches the actions defined in the asset which you can access directly in code. The following example demonstrates this, assuming there is an action map named "gameplay" containing two actions, "use" and "move" defined in the action asset: -```CSharp -using UnityEngine; -using UnityEngine.InputSystem; - -// IGameplayActions is an interface generated from the newly added "gameplay" -// action map, triggered by the "Generate Interfaces" checkbox. Note that if -// you change the default values for the action map, the name of the interface -// will be different. - -public class MyPlayerScript : MonoBehaviour, IGameplayActions -{ - // MyPlayerControls is the C# class that Unity generated. - // It encapsulates the data from the .inputactions asset we created - // and automatically looks up all the maps and actions for us. - MyPlayerControls controls; - - public void OnEnable() - { - if (controls == null) - { - controls = new MyPlayerControls(); - // Tell the "gameplay" action map that we want to be - // notified when actions get triggered. - controls.gameplay.SetCallbacks(this); - } - controls.gameplay.Enable(); - } - - public void OnDisable() - { - controls.gameplay.Disable(); - } - - public void OnUse(InputAction.CallbackContext context) - { - // 'Use' code here. - } - - public void OnMove(InputAction.CallbackContext context) - { - // 'Move' code here. - } - -} -``` +[!code-cs[generate-cs-api](Packages/com.unity.inputsystem/DocCodeSamples.Tests/GenerateCsApiFromActions.cs)] > [!NOTE] > To regenerate the .cs file, right-click the .inputactions asset in the Project Browser and select **Reimpor**. diff --git a/Packages/com.unity.inputsystem/Documentation~/quick-start-guide.md b/Packages/com.unity.inputsystem/Documentation~/quick-start-guide.md index a507d941b8..57db3ea430 100644 --- a/Packages/com.unity.inputsystem/Documentation~/quick-start-guide.md +++ b/Packages/com.unity.inputsystem/Documentation~/quick-start-guide.md @@ -56,38 +56,7 @@ This workflow uses the following steps: These steps are shown in the example script below: -```CSharp -using UnityEngine; -using UnityEngine.InputSystem; // 1. The Input System "using" statement - -public class Example : MonoBehaviour -{ - // 2. These variables are to hold the Action references - InputAction moveAction; - InputAction jumpAction; - - private void Start() - { - // 3. Find the references to the "Move" and "Jump" actions - moveAction = InputSystem.actions.FindAction("Move"); - jumpAction = InputSystem.actions.FindAction("Jump"); - } - - void Update() - { - // 4. Read the "Move" action value, which is a 2D vector - // and the "Jump" action state, which is a boolean value - - Vector2 moveValue = moveAction.ReadValue(); - // your movement code here - - if (jumpAction.IsPressed()) - { - // your jump code here - } - } -} -``` +[!code-cs[quick-start-guide](Packages/com.unity.inputsystem/DocCodeSamples.Tests/QuickStartGuide.cs#quick-start-guide)] These actions named "Move" and "Jump" in this script work straight away with no configuration required because they match the names of some of the pre-configured defaults in the Input System package. diff --git a/Packages/com.unity.inputsystem/Documentation~/using-direct-workflow.md b/Packages/com.unity.inputsystem/Documentation~/using-direct-workflow.md index 171733e41a..dfb8802fc3 100644 --- a/Packages/com.unity.inputsystem/Documentation~/using-direct-workflow.md +++ b/Packages/com.unity.inputsystem/Documentation~/using-direct-workflow.md @@ -11,32 +11,7 @@ It can be useful if you want a quick implementation with one specific type of de You can directly read the values from connected devices by referring to the device’s [controls](controls.md) and reading the values they are currently generating, using code like this: -```CSharp -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 - } - } -} -``` +[!code-cs[using-direct-workflow](Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingDirectWorkflow.cs)] The example above reads values directly from the right trigger, and the left stick, of the currently connected [gamepad](devices-gamepads.md). It does not use the input system’s "Action" class, and instead the conceptual actions in your game or app, such as "move" and "use", are implicitly defined by what your code does in response to the input. You can use the same approach for other Device types such as the [keyboard](xref:UnityEngine.InputSystem.Keyboard) or [mouse](xref:UnityEngine.InputSystem.Mouse). diff --git a/Packages/com.unity.inputsystem/Documentation~/using-playerinput-workflow.md b/Packages/com.unity.inputsystem/Documentation~/using-playerinput-workflow.md index ddec8aadbc..c2e0eb1220 100644 --- a/Packages/com.unity.inputsystem/Documentation~/using-playerinput-workflow.md +++ b/Packages/com.unity.inputsystem/Documentation~/using-playerinput-workflow.md @@ -20,36 +20,7 @@ In the above example image, you can see the PlayerInput component set up to map This is an example of the script which would provide an implementation of these methods -```CSharp -using UnityEngine; -using UnityEngine.InputSystem; - -// This script is designed to have the OnMove and -// OnJump methods called by a PlayerInput component - -public class ExampleScript : MonoBehaviour -{ - Vector2 moveAmount; - - public void OnMove(InputAction.CallbackContext context) - { - // read the value for the "move" action each event call - moveAmount = context.ReadValue(); - } - - public void OnJump(InputAction.CallbackContext context) - { - // your jump code goes here. - } - - public void Update() - { - // to use the Vector2 value from the "move" action each - // frame, use the "moveAmount" variable here. - } - -} -``` +[!code-cs[player-input-workflow](Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingPlayerinputWorkflow.cs)] > [!NOTE] > As a general rule, if you are using the PlayerInput workflow, you should read input through callbacks as described above, however if you need to access the input actions asset directly while using the PlayerInput component, you should access the [PlayerInput component's copy of the actions](xref:UnityEngine.InputSystem.PlayerInput), not `InputSystem.actions`. From 910b276b6fb8dc72baa4bb3b635d27a5fd5790e5 Mon Sep 17 00:00:00 2001 From: ChoshikaBagratee Date: Thu, 13 Aug 2026 13:02:30 -0400 Subject: [PATCH 5/6] add .cs files --- .../DocCodeSamples.Tests/ControlActuation.cs | 22 +++ .../DocCodeSamples.Tests/ControlPaths.cs | 27 ++++ .../IntroductionInteractions.cs | 125 ++++++++++++++++++ .../RecordControlStateHistory.cs | 59 +++++++++ .../apply-interactions-actions.md | 4 +- .../apply-interactions-bindings.md | 6 +- .../Documentation~/control-actuation.md | 12 +- .../Documentation~/control-paths.md | 18 +-- .../introduction-interactions.md | 48 +------ .../record-control-state-history.md | 49 +------ .../write-custom-interactions.md | 50 +------ 11 files changed, 246 insertions(+), 174 deletions(-) create mode 100644 Packages/com.unity.inputsystem/DocCodeSamples.Tests/ControlActuation.cs create mode 100644 Packages/com.unity.inputsystem/DocCodeSamples.Tests/ControlPaths.cs create mode 100644 Packages/com.unity.inputsystem/DocCodeSamples.Tests/IntroductionInteractions.cs create mode 100644 Packages/com.unity.inputsystem/DocCodeSamples.Tests/RecordControlStateHistory.cs diff --git a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/ControlActuation.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/ControlActuation.cs new file mode 100644 index 0000000000..eea34f9510 --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/ControlActuation.cs @@ -0,0 +1,22 @@ +using UnityEngine; +using UnityEngine.InputSystem; + +class ControlActuation : MonoBehaviour +{ + void Example(){ + + #region actuation + // Check if leftStick is currently actuated. + if (Gamepad.current.leftStick.IsActuated()) + Debug.Log("Left Stick is actuated"); + #endregion + + #region actuation2 + // Check if left stick is actuated more than a quarter of its motion range. + if (Gamepad.current.leftStick.EvaluateMagnitude() > 0.25f) + Debug.Log("Left Stick actuated past 25%"); + #endregion + + } + +} \ No newline at end of file diff --git a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/ControlPaths.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/ControlPaths.cs new file mode 100644 index 0000000000..4e84a57ff7 --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/ControlPaths.cs @@ -0,0 +1,27 @@ +using System.Linq; +using UnityEngine; +using UnityEngine.InputSystem; + +class ControlPathsExample +{ + void Example() + { + #region parse + var parsed = InputControlPath.Parse("{LeftHand}/trigger").ToArray(); + + Debug.Log(parsed.Length); // Prints 2. + Debug.Log(parsed[0].layout); // Prints "XRController". + Debug.Log(parsed[0].name); // Prints an empty string. + Debug.Log(parsed[0].usages.First()); // Prints "LeftHand". + Debug.Log(parsed[1].layout); // Prints null. + Debug.Log(parsed[1].name); // Prints "trigger". + #endregion + + #region findcontrols + var gamepad = Gamepad.all[0]; + var leftStickX = gamepad["leftStick/x"]; + var submitButton = gamepad["{Submit}"]; + var allSubmitButtons = InputSystem.FindControls("*/{Submit}"); + #endregion + } +} \ No newline at end of file diff --git a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/IntroductionInteractions.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/IntroductionInteractions.cs new file mode 100644 index 0000000000..da84192fd3 --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/IntroductionInteractions.cs @@ -0,0 +1,125 @@ +namespace DocCodeSamples.Tests +{ + #region interactions + using UnityEngine; + using UnityEngine.InputSystem; + using UnityEngine.InputSystem.Interactions; + + public class ExampleScript : MonoBehaviour + { + InputAction jumpAction; + + private void Start() + { + jumpAction = InputSystem.actions.FindAction("Jump"); + + + jumpAction.started += context => + { + if (context.interaction is SlowTapInteraction) + { + // Show "charging" UI + } + }; + + jumpAction.performed += context => + { + if (context.interaction is SlowTapInteraction) + { + // call "charged jump" code + } + else + { + // call "regular jump" code + }; + }; + + jumpAction.canceled += context => + { + // Hide "charging" UI + }; + + } + } + #endregion + + class ExampleScript2 : MonoBehaviour + { + public PlayerInput playerInput; + + private void Start() + { + #region timeout + // Returns a value between 0 (inclusive) and 1 (inclusive). + var warpActionCompletion = playerInput.actions["warp"].GetTimeoutCompletionPercentage(); + #endregion + + #region interactionactions + var Action = new InputAction(interactions: "tap(duration=0.8)"); + #endregion + } + + private void ConfigureBindingInteractions() + { + #region interactionbindings + var Action = new InputAction(); + Action.AddBinding("/leftStick").WithInteractions("tap(duration=0.8)"); + #endregion + } + } + + #region custominteraction + // Interaction which performs when you quickly move an + // axis all the way from extreme to the other. + public class MyExampleInteraction : IInputInteraction + { + public float duration = 0.2f; + + public void Process(ref InputInteractionContext context) + { + if (context.timerHasExpired) + { + context.Canceled(); + return; + } + + switch (context.phase) + { + case InputActionPhase.Waiting: + if (context.ReadValue() == 1) + { + context.Started(); + context.SetTimeout(duration); + } + break; + + case InputActionPhase.Started: + if (context.ReadValue() == -1) + context.Performed(); + break; + } + } + + // Unlike processors, Interactions can be stateful, meaning that you can keep a + // local state that changes over time as input is received. The system might + // invoke the Reset() method to ask Interactions to reset to the local state + // at certain points. + public void Reset() + { + } + + void Start() + { + #region registerinteraction + InputSystem.RegisterInteraction(); + #endregion + + #region useinteraction + var Action = new InputAction(interactions: "MyExample(duration=0.5)"); + #endregion + } + } + #endregion +} + + diff --git a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/RecordControlStateHistory.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/RecordControlStateHistory.cs new file mode 100644 index 0000000000..e7e9ef7992 --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/RecordControlStateHistory.cs @@ -0,0 +1,59 @@ +using UnityEngine; +using UnityEngine.InputSystem; + +class RecordControlStateHistoryExample +{ + void Example() + { + #region history + // Create history that records Vector2 control value changes. + // NOTE: You can also pass controls directly or use paths that match multiple + // controls (For example, "/