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/ConfigureInputfromCode.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/ConfigureInputfromCode.cs new file mode 100644 index 0000000000..fc01f66925 --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/ConfigureInputfromCode.cs @@ -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: "/leftStick"); + var moveAction = new InputAction("move", binding: "/rightStick"); + + moveAction.AddCompositeBinding("1DAxis") + .With("Left", "/a") + .With("Right", "/d"); + } + + { + // Create an action map with actions. + var map = new InputActionMap("Gameplay"); + var lookAction = map.AddAction("look"); + lookAction.AddBinding("/leftStick"); + } + + { + // Create an action asset. + var asset = ScriptableObject.CreateInstance(); + var gameplayMap = new InputActionMap("gameplay"); + asset.AddActionMap(gameplayMap); + var lookAction = gameplayMap.AddAction("look", "/leftStick"); + } + #endregion + } + } +} \ No newline at end of file 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/CustomProcessors.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/CustomProcessors.cs new file mode 100644 index 0000000000..47d0e4cd36 --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/CustomProcessors.cs @@ -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 + { + #if UNITY_EDITOR + static MyValueShiftProcessor() + { + Initialize(); + } + #endif + + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] + static void Initialize() + { + InputSystem.RegisterProcessor(); + } + + //... + } + #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("/leftStick") + .WithProcessor("invertVector2(invertX=false)"); + #endregion + } + + void AddProcessor() + { + #region addprocessor + var action = new InputAction(processors: "invertVector2(invertX=false)"); + #endregion + } + } +} 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/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/ProcessorControls.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/ProcessorControls.cs new file mode 100644 index 0000000000..2d2c4e3746 --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/ProcessorControls.cs @@ -0,0 +1,37 @@ +using UnityEngine.InputSystem; +using UnityEngine.InputSystem.LowLevel; +using UnityEngine.InputSystem.Utilities; + +#region mydevice +public struct MyDeviceState : IInputStateTypeInfo +{ + public FourCC format => new FourCC('M', 'Y', 'D', 'V'); + + // Add an axis deadzone to the Control to ignore values + // smaller then 0.2, as our Control does not have a stable + // resting position. + [InputControl(layout = "Axis", processors = "AxisDeadzone(min=0.2)")] + public short axis; +} +#endregion + +class MyDeviceLayoutJson +{ + const string json = @" + #region mydevicejson + { + ""name"" : ""MyDevice"", + ""extend"" : ""Gamepad"", // Or some other thing + ""controls"" : [ + { + ""name"" : ""axis"", + ""layout"" : ""Axis"", + ""offset"" : 4, + ""format"" : ""FLT"", + ""processors"" : ""AxisDeadzone(min=0.2)"" + } + ] + } + #endregion +"; +} \ No newline at end of file diff --git a/Packages/com.unity.inputsystem/DocCodeSamples.Tests/ProcessorsExamples.cs b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/ProcessorsExamples.cs new file mode 100644 index 0000000000..998c414f1a --- /dev/null +++ b/Packages/com.unity.inputsystem/DocCodeSamples.Tests/ProcessorsExamples.cs @@ -0,0 +1,76 @@ +using UnityEditor; +using UnityEngine.InputSystem.Editor; +#region boat +using UnityEngine; +using UnityEngine.InputSystem; + +public class Boat : MonoBehaviour +{ + void OnMove(InputValue value) + { + // The X value will be used to rotate the boat + var stick = value.Get(); + var direction = stick.x; + transform.Rotate(Vector3.up, direction); + // To move the boat forwards, this code block uses the Y value of the stick + var speed = stick.y; + transform.Translate(new Vector3(0,0,speed),Space.Self); + } +} +#endregion + +class ProcessorsExamples : MonoBehaviour +{ + void Start() + { + #region processors + // This references the processor registered as "scale" and sets its "factor" + // parameter (a floating-point value) to a value of 2.5. + "scale(factor=2.5)"; + + // Multiple processors can be chained together. They are processed + // from left to right. + // Example: First invert the value, then normalize [0..10] values to [0..1]. + "invert,normalize(min=0,max=10)"; + #endregion + } +} + +#region myvalueprocessor +public class MyValueShiftProcessor : InputProcessor +{ + [Tooltip("Number to add to incoming values.")] + public float valueShift = 0; + + public override float Process(float value, InputControl control) + { + return value + valueShift; + } +} +#endregion + +#region customizeUI +// No registration is necessary for an InputParameterEditor. +// The system automatically finds subclasses based on the +// <..> type parameter. +#if UNITY_EDITOR +public class MyValueShiftProcessorEditor : InputParameterEditor +{ + private GUIContent m_SliderLabel = new GUIContent("Shift By"); + + public override void OnEnable() + { + // Put initialization code here. Use 'target' to refer + // to the instance of MyValueShiftProcessor that is being + // edited. + } + + public override void OnGUI() + { + // Define your custom UI here using EditorGUILayout. + target.valueShift = EditorGUILayout.Slider(m_SliderLabel, + target.valueShift, 0, 10); + } +} +#endif +#endregion \ 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/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, "/