Input System integration

UPDATE: A package that provides integration with Unity's Input System can be found on AC's Downloads page. The original script is below.

This script demonstrates one way to add support for Unity's new Input System. It works by defining Input Actions directly within a component, but it can be adapted to work with an asset file instead.

To use it:

1. Install Unity's Input System from the Package Manager - see Unity's own documentation for more on this
2. Create a new C# file named InputSystemIntegration.cs and paste in the code below
3. Add a new GameObject to the scene, and add the Input System Integration component to it
4. Configure Input Actions in its Inspector as desired

If you are using Unity UI for menu rendering, you may also need to peform the following additional steps:

1. Create a new Actions input asset that features all inputs you need for UI interaction.
2. Create a new EventSystem in your scene, and attach the Input System UI Input Module component, assigning the Actions input asset made in step 1.
3. Make this new EventSystem a prefab, remove it from the scene, and assign it in the Menu Manager's "Event system prefab" field.

InputSystemIntegration.cs:

```
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

namespace AC
{

 public class InputSystemIntegration : MonoBehaviour
 {

 #region Variables

 [SerializeField] InputAction movementAction = null;
 [SerializeField] InputAction holdToRunAction = null;
 [SerializeField] InputAction toggleRunAction = null;
 [SerializeField] InputAction interactionAAction = null;
 [SerializeField] InputAction interactionBAction = null;
 [SerializeField] InputAction toggleCursorAction = null;
 [SerializeField] InputAction menuAction = null;
 [SerializeField] InputAction cursorMovementAction = null;

 #endregion

 #region UnityStandards

 private void Start ()
 {
 // Mouse delegates
 KickStarter.playerInput.InputMousePositionDelegate = Custom\_MousePosition;
 KickStarter.playerInput.InputGetMouseButtonDelegate = Custom\_GetMouseButton;
 KickStarter.playerInput.InputGetMouseButtonDownDelegate = Custom\_GetMouseButtonDown;

 // Keyboard / controller delegates
 KickStarter.playerInput.InputGetAxisDelegate = Custom\_GetAxis;
 KickStarter.playerInput.InputGetButtonDelegate = Custom\_GetButton;
 KickStarter.playerInput.InputGetButtonDownDelegate = Custom\_GetButtonDown;
 KickStarter.playerInput.InputGetButtonUpDelegate = Custom\_GetButtonUp;
 KickStarter.playerInput.InputGetFreeAimDelegate = Custom\_GetFreeAim;

 // Touch delegates
 KickStarter.playerInput.InputTouchCountDelegate = Custom\_TouchCount;
 KickStarter.playerInput.InputTouchPositionDelegate = Custom\_TouchPosition;
 KickStarter.playerInput.InputTouchDeltaPositionDelegate = Custom\_TouchDeltaPosition;
 KickStarter.playerInput.InputGetTouchPhaseDelegate = Custom\_TouchPhase;

 #if !UNITY\_EDITOR
 if (KickStarter.settingsManager.inputMethod == InputMethod.TouchScreen)
 {
 UnityEngine.InputSystem.EnhancedTouch.EnhancedTouchSupport.Enable ();
 }
 #endif
 }

 private void OnEnable ()
 {
 movementAction.Enable ();
 holdToRunAction.Enable ();
 interactionAAction.Enable ();
 interactionBAction.Enable ();
 toggleCursorAction.Enable ();
 cursorMovementAction.Enable ();
 menuAction.Enable ();
 }

 private void OnDisable ()
 {
 movementAction.Disable ();
 holdToRunAction.Disable ();
 interactionAAction.Disable ();
 interactionBAction.Disable ();
 toggleCursorAction.Disable ();
 cursorMovementAction.Disable ();
 menuAction.Disable ();
 }

 #endregion

 #region MouseInput

 private Vector2 Custom\_MousePosition (bool cursorIsLocked)
 {
 if (cursorIsLocked)
 return new Vector2 (Screen.width / 2f, Screen.height / 2f);

 return Mouse.current.position.ReadValue ();
 }

 private bool Custom\_GetMouseButton (int button)
 {
 switch (button)
 {
 case 0:
 return Mouse.current.leftButton.isPressed;

 case 1:
 return Mouse.current.rightButton.isPressed;

 default:
 return false;
 }
 }

 private bool Custom\_GetMouseButtonDown (int button)
 {
 switch (button)
 {
 case 0:
 return Mouse.current.leftButton.wasPressedThisFrame;

 case 1:
 return Mouse.current.rightButton.wasPressedThisFrame;

 default:
 return false;
 }
 }

 #endregion

 #region KeyboardControllerInput

 private float Custom\_GetAxis (string axisName)
 {
 switch (axisName)
 {
 case "Horizontal":
 return movementAction.ReadValue<Vector2> ().x;

 case "Vertical":
 return movementAction.ReadValue<Vector2> ().y;

 case "Run":
 return holdToRunAction.ReadValue<float> ();

 case "CursorHorizontal":
 return cursorMovementAction.ReadValue<Vector2> ().x;

 case "CursorVertical":
 return cursorMovementAction.ReadValue<Vector2> ().y;
 }
 return 0f;
 }

 private bool Custom\_GetButton (string axisName)
 {
 ButtonControl[] buttonControls = GetButtonControls (axisName);
 foreach (ButtonControl buttonControl in buttonControls)
 {
 if (buttonControl.isPressed) return true;
 }
 return false;
 }

 private bool Custom\_GetButtonDown (string axisName)
 {
 ButtonControl[] buttonControls = GetButtonControls (axisName);
 foreach (ButtonControl buttonControl in buttonControls)
 {
 if (buttonControl.wasPressedThisFrame) return true;
 }
 return false;
 }

 private bool Custom\_GetButtonUp (string axisName)
 {
 ButtonControl[] buttonControls = GetButtonControls (axisName);
 foreach (ButtonControl buttonControl in buttonControls)
 {
 if (buttonControl.wasReleasedThisFrame) return true;
 }
 return false;
 }

 private Vector2 Custom\_GetFreeAim (bool cursorIsLocked)
 {
 if (cursorIsLocked)
 {
 return new Vector2 (Custom\_GetAxis ("CursorHorizontal"), Custom\_GetAxis ("CursorVertical"));
 }
 return Vector2.zero;
 }

 private ButtonControl[] GetButtonControls (string axisName)
 {
 InputAction inputAction = GetInputAction (axisName);
 if (inputAction != null)
 {
 ButtonControl[] controls = new ButtonControl[inputAction.controls.Count];
 for (int i=0; i<controls.Length; i++)
 {
 controls[i] = (ButtonControl) inputAction.controls[i];
 }
 return controls;
 }
 return new ButtonControl[0];
 }

 private InputAction GetInputAction (string axisName)
 {
 switch (axisName)
 {
 case "ToggleRun":
 return toggleRunAction;

 case "InteractionA":
 return interactionAAction;

 case "InteractionB":
 return interactionBAction;

 case "ToggleCursor":
 return toggleCursorAction;

 case "Menu":
 return menuAction;

 default:
 return null;
 }
 }

 #endregion

 #region TouchInput

 private int Custom\_TouchCount ()
 {
 return UnityEngine.InputSystem.EnhancedTouch.Touch.activeTouches.Count;
 }

 private Vector2 Custom\_TouchPosition (int index)
 {
 return UnityEngine.InputSystem.EnhancedTouch.Touch.activeTouches[index].screenPosition;
 }

 private Vector2 Custom\_TouchDeltaPosition (int index)
 {
 return UnityEngine.InputSystem.EnhancedTouch.Touch.activeTouches[index].delta;
 }

 private UnityEngine.TouchPhase Custom\_TouchPhase (int index)
 {
 UnityEngine.InputSystem.TouchPhase touchPhase = UnityEngine.InputSystem.EnhancedTouch.Touch.activeTouches[index].phase;
 switch (touchPhase)
 {
 case UnityEngine.InputSystem.TouchPhase.Began:
 return UnityEngine.TouchPhase.Began;

 case UnityEngine.InputSystem.TouchPhase.Canceled:
 return UnityEngine.TouchPhase.Canceled;

 case UnityEngine.InputSystem.TouchPhase.Ended:
 return UnityEngine.TouchPhase.Ended;

 case UnityEngine.InputSystem.TouchPhase.Moved:
 return UnityEngine.TouchPhase.Moved;

 case UnityEngine.InputSystem.TouchPhase.Stationary:
 return UnityEngine.TouchPhase.Stationary;

 default:
 return UnityEngine.TouchPhase.Canceled;
 }
 }

 #endregion

 }

}

```