-
Notifications
You must be signed in to change notification settings - Fork 340
DOCS: Move hardcoded code examples to testable files #2472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 7 commits
7c9abaa
2cd98fb
bbe5f73
c84ae43
eb2f4b7
910b276
8a42ed2
f559f60
f6bd4e0
3fec92f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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,69 @@ | ||
| namespace DocCodeSamples.Tests | ||
| { | ||
| #region declaration | ||
| using UnityEngine; | ||
| using UnityEngine.InputSystem; | ||
|
|
||
| public class ExampleScript : MonoBehaviour | ||
| { | ||
| public InputAction move; | ||
| public InputAction jump; | ||
| } | ||
| #endregion | ||
|
|
||
| class ConfigureInputfromCode : MonoBehaviour | ||
| { | ||
| const string json = @" | ||
| { | ||
| ""maps"" : [ | ||
| { | ||
| ""name"" : ""gameplay"", | ||
| ""actions"" : [ | ||
| { ""name"" : ""fire"", ""type"" : ""button"" } | ||
| ] | ||
| } | ||
| ] | ||
| }"; | ||
|
|
||
| void ConfigureFromJsonExample() | ||
| { | ||
| #region configurefromjson | ||
| // Load a set of action maps from JSON. | ||
| var maps = InputActionMap.FromJson(json); | ||
|
|
||
| // Load an entire InputActionAsset from JSON. | ||
| var asset = InputActionAsset.FromJson(json); | ||
| #endregion | ||
| } | ||
|
|
||
| void Start() | ||
| { | ||
| #region configurefromcode | ||
| { | ||
| // Create free-standing actions. | ||
| var lookAction = new InputAction("look", binding: "<Gamepad>/leftStick"); | ||
| var moveAction = new InputAction("move", binding: "<Gamepad>/rightStick"); | ||
|
|
||
| moveAction.AddCompositeBinding("1DAxis") | ||
| .With("Left", "<Keyboard>/a") | ||
| .With("Right", "<Keyboard>/d"); | ||
| } | ||
|
|
||
| { | ||
| // Create an action map with actions. | ||
| var map = new InputActionMap("Gameplay"); | ||
| var lookAction = map.AddAction("look"); | ||
| lookAction.AddBinding("<Gamepad>/leftStick"); | ||
| } | ||
|
|
||
| { | ||
| // Create an action asset. | ||
| var asset = ScriptableObject.CreateInstance<InputActionAsset>(); | ||
| var gameplayMap = new InputActionMap("gameplay"); | ||
| asset.AddActionMap(gameplayMap); | ||
| var lookAction = gameplayMap.AddAction("look", "<Gamepad>/leftStick"); | ||
| } | ||
| #endregion | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
|
||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| using System.Linq; | ||
| using UnityEngine; | ||
| using UnityEngine.InputSystem; | ||
|
|
||
| class ControlPathsExample | ||
| { | ||
| void Example() | ||
| { | ||
| #region parse | ||
| var parsed = InputControlPath.Parse("<XRController>{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 | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| using UnityEngine; | ||
| using UnityEditor; | ||
| using UnityEngine.InputSystem; | ||
| using UnityEngine.InputSystem.Editor; | ||
|
|
||
| namespace DocCodeSamples.Tests | ||
| { | ||
| #region registernewprocessor | ||
| #if UNITY_EDITOR | ||
| [InitializeOnLoad] | ||
| #endif | ||
| public class MyValueShiftProcessor : InputProcessor<float> | ||
| { | ||
| #if UNITY_EDITOR | ||
| static MyValueShiftProcessor() | ||
| { | ||
| Initialize(); | ||
| } | ||
| #endif | ||
|
|
||
| [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] | ||
| static void Initialize() | ||
| { | ||
| InputSystem.RegisterProcessor<MyValueShiftProcessor>(); | ||
| } | ||
|
|
||
| //... | ||
| } | ||
| #endregion | ||
|
|
||
| class ProcessorExamples: MonoBehaviour | ||
| { | ||
| void Start() | ||
| { | ||
| #region inputactionwithprocessor | ||
| var action = new InputAction(processors: "myvalueshift(valueShift=2.3)"); | ||
| #endregion | ||
| } | ||
|
|
||
| void ConfigureProcessorBinding() | ||
| { | ||
| #region processorbindings | ||
| var action = new InputAction(); | ||
| action.AddBinding("<Gamepad>/leftStick") | ||
| .WithProcessor("invertVector2(invertX=false)"); | ||
| #endregion | ||
| } | ||
|
|
||
| void AddProcessor() | ||
| { | ||
| #region addprocessor | ||
| var action = new InputAction(processors: "invertVector2(invertX=false)"); | ||
| #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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The sample assembly contains neither 🤖 Helpful? 👍/👎 |
||
| { | ||
| // 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. | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
InputTestFixtureis not provided by this assembly's only direct reference (Unity.InputSystem): its declaration belongs to the separateUnity.InputSystem.TestFrameworkassembly. Consequently this sample produces an unresolved-type compiler error inUnity.InputSystem.DocCodeSamples. Either put this test-fixture example in a test assembly that references the framework (with the appropriate test constraints) or avoid using the fixture in this compiled sample.🤖 Helpful? 👍/👎