InputManager.cs

void Update () { if (Input.GetButtonDown(“Fire1”))… }

void Update () { if (Input.GetButtonDown(“Fire1”))… }

void Update () { if (Input.GetButtonDown(“Fire1”))… }

void Update () { if (Input.GetButtonDown(“Fire1”))… }
void Update () { if (Input.GetButtonDown(“Fire1”))… }

In every script, i seem to write the same line of code, and if your not sure what code I’m referring to, look up…

Having written in multiple languages, it seems like poor code to have multiple check if “button down” statements. So I created an input manager script (aptly named), to create an event listener of sorts for input.

How It Works

InputManager.cs is the core of the event listener.

  1. The InputManager Class creates a singleton of the class
  2. It creates “events” which future scripts can “subscribe” to
  3. “if (System.Math.Abs(Input.GetAxis(“Horizontal”)) > 0 && Horizontal != null)” checks if there is input
  4. “if (System.Math.Abs(Input.GetAxis(“Horizontal”)) > 0 && Horizontal != null)” checks if a the “horizontal” event has any “subscribers
    1. If both are true, it runs all functions “subscribed” to that event (with the variable of what button is pressed)*.

* The “float input = 0” in “public delegate void MovementHandler(float input = 0)” is the variable passed to each function, it is not required if you plan on getting the input in your individual functions.

How To Use

MovementController.cs is example code on how to use it

  1. OnStart, or whenever you want to “subscribe” to an input event, run “InputManager.Horizontal += _Horizontal”
    • “_Horizontal” is the local function which “subscribes“, +=, to the event “InputManager.Horizontal”
  2. When the InputManager.cs receives input, “_Horizontal” Runs.
  3. Whenever you want “unsubscribe”, run “InputManager.Horizontal -= _Horizontal”.
InputManager.csMovementController.cs
using UnityEngine;

public class InputManager : MonoBehaviour {

    #region Singleton
    private static InputManager _instance;
    public static InputManager Instance {
        get { return _instance; }
    }
    private void Awake() {
        if (_instance != null && _instance != this) {
            Destroy(this.gameObject);
            return;
        }

        _instance = this;
        DontDestroyOnLoad(this.gameObject);
    }
    #endregion

    public delegate void MovementHandler(float input = 0);

    public static event MovementHandler Horizontal;
    public static event MovementHandler Vertical;

    public delegate void KeyboardHandler(string key);
    public static event KeyboardHandler KeyboardDown;

    void Update() {
        if (System.Math.Abs(Input.GetAxis("Horizontal")) > 0 && Horizontal != null) Horizontal(Input.GetAxis("Horizontal"));
        if (System.Math.Abs(Input.GetAxis("Vertical")) > 0 && Vertical != null) Vertical(Input.GetAxis("Vertical"));

        if (Input.anyKeyDown && KeyboardDown != null) KeyboardDown(Input.inputString);
    }

}

using UnityEngine;

public class MovementController : MonoBehaviour {

    public float speed = 1;

    void Start() {
        InputManager.Horizontal += _Horizontal;
        InputManager.Vertical += _Vertical;
    }
    void OnDisable() {
        InputManager.Horizontal -= _Horizontal;
        InputManager.Vertical -= _Vertical;
    }

    void _Horizontal(float _input) {
        Vector3 pos = transform.position;
        pos.x += _input * speed;
        transform.position = pos;
    }
    void _Vertical(float _input) {
        Vector3 pos = transform.position;
        pos.y += _input * speed;
        transform.position = pos;
    }

}