-
Notifications
You must be signed in to change notification settings - Fork 339
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 all commits
7c9abaa
2cd98fb
bbe5f73
c84ae43
eb2f4b7
910b276
8a42ed2
f559f60
f6bd4e0
3fec92f
eee8096
8c26080
a29a178
842edfd
03a03c9
0dc4b2d
8a92423
be4938f
360f9f7
6058449
579d446
e76086c
22a4d78
bb8b7d7
d08e70a
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,15 @@ | ||
| using UnityEngine; | ||
| using UnityEngine.InputSystem; | ||
|
|
||
| /// <summary> | ||
| /// Example script demonstrating how to look up a project-wide action. | ||
| /// </summary> | ||
| public class AboutProjectWideActions : MonoBehaviour | ||
| { | ||
| void Start() | ||
| { | ||
| #region about-project-wide-actions | ||
| InputSystem.actions.FindAction("Move"); | ||
| #endregion | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| using UnityEngine; | ||
| using UnityEngine.InputSystem; | ||
|
|
||
| class BindingConflictsExample | ||
| { | ||
| 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(); | ||
|
|
||
| var keyboard = Keyboard.current ?? InputSystem.AddDevice<Keyboard>(); | ||
|
|
||
| // Now, let's assume the left shift key on the keyboard is pressed (here, we manually | ||
| // press it by queueing a state event for the control). | ||
| InputSystem.QueueDeltaStateEvent(keyboard.leftShiftKey, 1f); | ||
| InputSystem.Update(); | ||
|
|
||
| // 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. | ||
| InputSystem.QueueDeltaStateEvent(keyboard.bKey, 1f); | ||
| InputSystem.Update(); | ||
| #endregion | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| namespace DocCodeSamples.Tests | ||
| { | ||
| #region declaration | ||
| using UnityEngine; | ||
| using UnityEngine.InputSystem; | ||
|
|
||
| /// <summary> | ||
| /// Example script exposing serialized action references. | ||
| /// </summary> | ||
| public class ExampleScript : MonoBehaviour | ||
| { | ||
| /// <summary> | ||
| /// Reference to the "move" action. | ||
| /// </summary> | ||
| public InputAction move; | ||
|
|
||
| /// <summary> | ||
| /// Reference to the "jump" action. | ||
| /// </summary> | ||
| 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", binding: "<Gamepad>/leftStick"); | ||
| } | ||
| #endregion | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| namespace DocCodeSamples.Tests.ConfigureUnityEvents_ManualEnable | ||
| { | ||
| #region manualEnableSingleton | ||
| using UnityEngine; | ||
| using UnityEngine.InputSystem; | ||
|
|
||
| /// <summary> | ||
| /// Example script demonstrating manually enabling actions instead of using the | ||
| /// default project-wide action map. | ||
| /// </summary> | ||
| public class MyPlayerScript : MonoBehaviour | ||
| { | ||
| PlayerInput playerInput; | ||
|
|
||
| void Start() | ||
| { | ||
| playerInput = GetComponent<PlayerInput>(); | ||
| InputSystem.actions.Disable(); | ||
| playerInput.currentActionMap?.Enable(); | ||
| } | ||
| } | ||
| #endregion | ||
| } | ||
|
|
||
| namespace DocCodeSamples.Tests.ConfigureUnityEvents_SendMessages | ||
| { | ||
| #region sendMessages | ||
| using UnityEngine; | ||
| using UnityEngine.InputSystem; | ||
|
|
||
| /// <summary> | ||
| /// Example script demonstrating the <c>PlayerInput</c> "Send Messages" behavior. | ||
| /// </summary> | ||
| public class MyPlayerScript : MonoBehaviour | ||
| { | ||
| // "jump" action becomes "OnJump" method. | ||
|
|
||
| /// <summary> | ||
| /// Called by <c>PlayerInput</c> when the "jump" action is triggered. | ||
| /// </summary> | ||
| // If you're not interested in the value from the control that triggers the action, use a method without arguments. | ||
| public void OnJump() | ||
| { | ||
| // your Jump code here | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Called by <c>PlayerInput</c> when the "move" action is triggered. | ||
| /// </summary> | ||
| /// <param name="value">Value of the control that triggered the action.</param> | ||
| // If you are interested in the value from the control that triggers an action, you can declare a parameter of type InputValue. | ||
| public void OnMove(InputValue value) | ||
| { | ||
| // Read value from control. The type depends on what type of controls. | ||
| // the action is bound to. | ||
| var v = value.Get<Vector2>(); | ||
|
|
||
| // IMPORTANT: | ||
| // The given InputValue is only valid for the duration of the callback. Storing the InputValue references somewhere and calling Get<T>() later does not work correctly. | ||
| } | ||
| } | ||
| #endregion | ||
| } | ||
|
|
||
| namespace DocCodeSamples.Tests.ConfigureUnityEvents_InvokeUnityEvents | ||
| { | ||
| #region invokeUnityEvents | ||
| using UnityEngine; | ||
| using UnityEngine.InputSystem; | ||
|
|
||
| /// <summary> | ||
| /// Example script demonstrating the <c>PlayerInput</c> "Invoke Unity Events" behavior. | ||
| /// </summary> | ||
| public class MyPlayerScript : MonoBehaviour | ||
| { | ||
| /// <summary> | ||
| /// Called when the "fire" action is triggered. | ||
| /// </summary> | ||
| /// <param name="context">Context for the triggered action.</param> | ||
| public void OnFire(InputAction.CallbackContext context) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Called when the "move" action is triggered. | ||
| /// </summary> | ||
| /// <param name="context">Context for the triggered action.</param> | ||
| public void OnMove(InputAction.CallbackContext context) | ||
| { | ||
| var value = context.ReadValue<Vector2>(); | ||
| } | ||
| } | ||
| #endregion | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| 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 | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| 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 | ||
| } | ||
|
|
||
| void PathExamples() | ||
| { | ||
| #region pathExamples | ||
| // Matches all gamepads (also gamepads *based* on the Gamepad layout): | ||
| _ = "<Gamepad>"; | ||
| // Matches the "Submit" control on all devices: | ||
| _ = "*/"; | ||
| // Matches the key that prints the "a" character on the current keyboard layout: | ||
| _ = "<Keyboard>/#(a)"; | ||
| // Matches the X axis of the left stick on a gamepad. | ||
| _ = "<Gamepad>/leftStick/x"; | ||
| // Matches the orientation control of the right-hand XR controller: | ||
| _ = "<XRController>/orientation"; | ||
| // Matches all buttons on a gamepad. | ||
| _ = "<Gamepad>/<Button>"; | ||
| #endregion | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| using UnityEngine; | ||
| using UnityEditor; | ||
| using UnityEngine.InputSystem; | ||
| using UnityEngine.InputSystem.Editor; | ||
|
|
||
| namespace DocCodeSamples.Tests | ||
| { | ||
| #region registernewprocessor | ||
| #if UNITY_EDITOR | ||
| [InitializeOnLoad] | ||
| #endif | ||
| /// <summary> | ||
| /// Example custom processor that registers itself with the Input System. | ||
| /// </summary> | ||
| public class MyValueShiftProcessor : InputProcessor<float> | ||
| { | ||
| #if UNITY_EDITOR | ||
| static MyValueShiftProcessor() | ||
| { | ||
| Initialize(); | ||
| } | ||
|
|
||
| #endif | ||
|
|
||
| [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] | ||
| static void Initialize() | ||
| { | ||
| InputSystem.RegisterProcessor<MyValueShiftProcessor>(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns <paramref name="value"/> unchanged. | ||
| /// </summary> | ||
| /// <param name="value">Value to process.</param> | ||
| /// <param name="control">Control from which the value originates.</param> | ||
| /// <returns>The unchanged value.</returns> | ||
| public override float Process(float value, InputControl control) | ||
| { | ||
| return value; | ||
| } | ||
| } | ||
| #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 | ||
| } | ||
| } | ||
| } |
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.
The comment above indicates this should match the
Submitcontrol across all devices, but the path is currently"*/".🤖 Helpful? 👍/👎