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.