Published on

Unity's new InputSystem is super useful! A gentle guide for beginners.

Authors
  • Evelyn

Please share this article if you like!

Hello!! Welcome to Evelyn GameDev Blog.

In this article, let's learn abount Unity's new Input System.

What you will learn on this page

How to use Unity's new Input System. Learn how to use Unity's new Input System, how it differs from the previous Input System, why you need a new Input System, and an overview of the key systems.


Table of Contents

What's Input System?

Input System is a new Input System for Unity, introduced to handle input from various input devices such as gamepads, keyboards, mice, and sensors in a generic and integrated way. It is currently available as an additional package to be installed from the package manager, and was officially released as version 1.0.0 in April 2020.

Input System is compatible with all platforms supported by Unity. It replaces the previous Unity Input System and is expected to become the standard Input System in the future, so it's a good idea to get used to it while you can.

Why can't we use a traditional Input System?

If you are a Unity developer, you are probably familiar with assigning gamepad and keyboard controls in the Input Manager of Project Settings and getting the status using Input.GetButtonDown, etc. to get the status. Why do we need a new Input System here?

The new Input System aims to solve these problems that the previous Unity Input System did not cover.

The goal of the new Input System is to provide a unified way to handle input from all input devices. In addition to keyboard, mouse, and touch input, the new Input System also supports the following user inputs.

  • keyboard input
  • tapping on a smartphone
  • Xbox controllers and other XInput-based gamepads
  • PS4 controller (DualShock)
  • Switch Joy-Con (if the build platform is Nintendo Switch)
  • HID joysticks (such as gamepads marketed as "DirectInput") can also be used.

Install the Input System

Input System can be imported from the Unity Registry in the Package Manager.

During the import process, a dialog box will appear, click Yes.

When the Unity Editor restarts, the import is complete.

You can switch the Input System in the Player settings of the Project Setting.

When you import the Input System in the Packager Manager, the new Input System will be selected as the default value.

Starter Assets that use the new Input System

Unity's management is distributing assets to make it easier to run characters with the new Input System.

Please read the following article to see how to use Starter Assets - Third Person Character Controller.

Starter Assets - Third Person Character Controller

Starter Assets - First Person Character Controller

Using the Player Input component

The PlayerInput component is a quick and easy approach to get the player's input. This component makes the tedious work somewhat easier.

  • Generates InputActions including movement etc. for WASD and Gamepad.
  • SendMessage to call the corresponding method.
  • InputSystem initialization

The generation of InputActions is one of the most tedious tasks, so it is very easy to get something that works. The action map of InputActions to be generated here includes the operations of "moving with WASD keys or GamePad", "moving the viewpoint with mouse operation or right stick", and "shooting in response to mouse click or GamePad".

How to use PlayerInput

Add PlayerInput component to the object you want to accept input from.

Press CreateActions of PlayerInput to create an action map.

Next, Select the generated action data. Then, let's click the Edit Asset button.

The list of actions is now displayed. You can customize various actions here.

The script for handling this action looks like the following.

using UnityEngine;
using UnityEngine.InputSystem;


public class PlayerInputExample : MonoBehaviour
{
    InputAction move, look, fire;
    void Start()
    {
        var playerInput = GetComponent<PlayerInput>();
        move = playerInput.actions["Move"];
        look = playerInput.actions["Look"];
        fire = playerInput.actions["Fire"];
    }

    void Update()
    {
        var m = move.ReadValue<Vector2>();
        var l = look.ReadValue<Vector2>();
        var f = fire.ReadValue<float>() > 0;

        Debug.Log($"Move {m}, Look {l}, Fire {f}");
    }
}

Change Behaviour of PlayerInput to InvokeUnityEvent and connect the action data you just created to the PlayerInput actions.

Furthermore, let's attach the above script.

When you run it, you will see that the input is picked up.

If you want to use it quickly and easily like the old Input Manager

You can also use some of the decisions as in the traditional Input Manager.

If your platform is limited, such as mobile games, you may want to use this option.

Handling Gamepad

using UnityEngine;
using UnityEngine.InputSystem;

public class HandlingGamepadInput: MonoBehaviour
{
   void Update()
   {
       // If no gamepad is connected, it will be null.
       if (Gamepad.current == null) return;

       if (Gamepad.current.buttonNorth.wasPressedThisFrame)
       {
           Debug.Log("Button North is pressed!");
       }
       if (Gamepad.current.buttonSouth.wasReleasedThisFrame)
       {
           Debug.Log("Button South has let go!");
       }

       Gamepad.current.leftStick.ReadValue();
   }
}

Handling Keyboard Input

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

public class HandlingKeyboardInput: MonoBehaviour
{
   void Update()
   {
        if(Keyboard.current.xKey.isPressed) {
            Debug.Log("X key is pressed!");
        }
        if(Keyboard.current[Key.Y].wasPressedThisFrame) {
            Debug.Log("Y key is pressed!");
        }
        if(((KeyControl)Keyboard.current["Z"]).wasPressedThisFrame) {
            Debug.Log("Z key is preesed!");
        }
   }

   // ゲームオブジェクトが有効になったら現在のキーボードのonTextInputイベントにリスナーを取り付け
    public void OnEnable() {
        Keyboard.current.onTextInput += OnTextInput;
    }
    // ゲームオブジェクトが無効になったら現在のキーボードのonTextInputイベントからリスナーを削除
    public void OnDisable() {
        Keyboard.current.onTextInput -= OnTextInput;
    }
    public void OnTextInput(char c) {
        Debug.Log("今打たれた文字" + c);
    }
}

Get touch input

using UnityEngine;
using UnityEngine.InputSystem.EnhancedTouch;
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;

public class EnhancedTouchExample : MonoBehaviour
{
   void OnEnable()
   {
       EnhancedTouchSupport.Enable();
   }

   void OnDisable()
   {
       EnhancedTouchSupport.Disable();
   }

   void OnGUI()
   {
       var text = "";

       foreach (var touch in Touch.activeTouches)
       {
           text += $"Touch: Id {touch.touchId} Position {touch.screenPosition} Phase {touch.phase}\n";
       }

       GUILayout.Label(text);
   }
}

Obtain input from accelerometer, etc.

using UnityEngine;
using UnityEngine.InputSystem;

public class AccelerometerExample : MonoBehaviour
{
   void OnEnable()
   {
       InputSystem.EnableDevice(Accelerometer.current);
   }

   void OnDisable()
   {
       InputSystem.DisableDevice(Accelerometer.current);
   }

   void OnGUI()
   {
       var text = $"Accelerometer: {Accelerometer.current.acceleration.ReadValue()}";
       GUILayout.Label(text);
   }
}

Official Documents

The official documentation can be found here.

Input System - Quick start guide

Wrap up

Using the new features seems to be more difficult for people who are familiar with the way things have always been done.

I know it's a lot of work, but if you want to support multiple platforms, please try using the new Input System.

Personally, I think it's a good idea to use Starter Assets for easy character movement.

Take the hassle out of multi-platform support with the new Input System!

That's all Evelyn for now. Hope to see you in another article with you.

Thank you for reading to the end.

Please share this article if you like!