Package based on new Unity Input System. Include Keyboard, Gamepad and Touch control schemes.
- Import SimpleInputs package to Unity project.
- Make sure that scene contain
EventSystemwithInputSystemUIInputModule - Add
Assets/Simple Inputs/Prefabs/Simple Inputs.prefabto scene
Link Inputs from added prefab to inputs field and use input values:
[SerializeField] private Inputs inputs;
private void Start()
{
inputs.PlayerInputs.Fire.OnStarted.AddListener(Fire);
inputs.PlayerInputs.Jump.OnCanceled.AddListener(Jump);
inputs.PlayerInputs.Look.OnChangedValue.AddListener(TurnTo);
}
private void Update()
{
Move(inputs.PlayerInputs.Move.Value);
}Bind Inputs as single in installer:
Container.Bind<Inputs>().FromComponentInHierarchy().AsSingle();and inject Inputs for simple linking:
[Inject] private readonly Inputs input;- Add new Action in
Assets/Simple Inputs/Settings/GameInputActions.inputactions, for example "Crouch" as Button (Important setup control schemes) - Open
PlayerInputs.csand add new input value:
public InputValue<float> Crouch;
public PlayerInputs()
{
...
Crouch = new InputValue<float>(inputLocker);
}- For integration to BaseInput (keyboard/mouse and gamepad) open
PlayerInputBinding.csinAssets/Simple Inputs/Scripts/BaseInputand add:
BindInput(playerInput.actions.FindAction("Crouch"), playerInputs.Crouch);- For integration to MobileInput open
MobileInput.csinAssets/Simple Inputs/Scripts/MobileInputand add:
public void OnCrouch(bool crouch)
{
inputs.PlayerInputs?.Crouch.SetValue(crouch ? 1f : 0);
}- Add on canvas
UI Virtual Buttonprefab fromAssets/Simple Inputs/Prefabs/VirtualInputsand attachOnCrouchfunction to button output event
Inputs.cs (in prefab: SimpleInputs/Inputs) contain all input values:
pointerOverUI- true if pointer over uicurrentPointerMouse- true if current pointer is mousecurrentPointerTouchpad- true while current pointer use mobile ui touchpadGameState-GameorMenustates for control lock cursor in game and unlock in menuPlayerInputs- basic player input values:inputLocker- block input controlInputValuecontainValue,IsPressed,PressedDuration,PressTimeand eventsOnStarted,OnCanceled,OnChangedValue