Universal Input detector script

Hello, currently I’m debating if a script Im making its faster/efficient than most ways I have seen people in tutorials design their Input detection.

The way I have seen people design their Input detection is that every script that needs a key press, axis check or something they always have an update funtion that is constantly checking for their wanted input detection and in the end they may end with a bunch of scripts on everysingle one of them has their own update function. In all the tutorials I have seen this is they way they do it .

What I am working on is a single script with a single update function that has an event for each Input needed (left,right,up,down, accept, cancel, etc) and whenever an input is detected it starts an event where any function I need input are suscribed to them.

This is the code

using UnityEngine;
using System.Collections;

public class ButtonManager : MonoBehaviour {

    public delegate void ButtonPressed();

    public static event ButtonPressed Up;
    public static event ButtonPressed Down;
    public static event ButtonPressed Left;
    public static event ButtonPressed Right;

    void Update()
    {
        if (Input.GetButton ("Left")) {
            if(Left != null){
                Left();
            }
        }
        if (Input.GetButton ("Right")) {
            if(Right != null){
                Right();
            }
        }
        if (Input.GetButton ("Up")) {
            if(Up != null){
                Up();
            }
        }
        if (Input.GetButton ("Down")) {
            if(Down != null){
                Down();
            }
        }
    }
}

Honestly I dont know what is the more efficient way to do it , I mean at least on the way Im doing it theres only one update fuction that is detecting the inputs and when you press a desired button it just broadcast the events to all the functions susbscribed in the other theres a bunch of updates functions checking all the time for the input detecction.

Sorry for not having an answer, but did you find out which is more efficient? Thanks.

Having an abstraction layer between UnityEngine.Input and your game can be a good thing.

  • Key remapping at runtime is possible
  • All input code appears in one place
  • Accidentally mapping the same key twice is harder

On the other hand it requires more code to set up. And it can mean your code is polling for inputs that aren’t actually used.