Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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("<Keyboard>/b");
shiftbAction.AddCompositeBinding("OneModifier")
.With("Modifier", "<Keyboard>/shift")
.With("Binding", "<Keyboard>/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
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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.
}

}
Original file line number Diff line number Diff line change
@@ -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<Vector2>();
// your movement code here

if (jumpAction.IsPressed())
{
// your jump code here
}
}
}
#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using UnityEngine;
#region using
using UnityEngine.InputSystem;
#endregion

namespace DocCodeSamples.Tests
{
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 Update() {
#region ReadActionValues
Vector2 moveValue = moveAction.ReadValue<Vector2>();
bool jumpValue = jumpAction.IsPressed();
#endregion
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Vector2>();
// your movement code here

if (jumpAction.IsPressed())
{
// your jump code here
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Vector2>();
}

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.
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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("<Keyboard>/b");
shiftbAction.AddCompositeBinding("OneModifier")
.With("Modifier", "<Keyboard>/shift")
.With("Binding", "<Keyboard>/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)]
11 changes: 1 addition & 10 deletions Packages/com.unity.inputsystem/Documentation~/default-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Original file line number Diff line number Diff line change
Expand Up @@ -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**.
Loading
Loading