I have been wrapping input recognition in events for quite some time. The main reason was that I had to process the mouse data quite heavily and I decided to do it once for all instances instead of making each instance do its own calculations. In that case it seems obvious to me that events are going to be more efficient, but now I wonder if they can improve code more generally.
The idea is:
public class Events : MonoBehaviour
{
public event Action KeyA;
public event Action<Vector3> TouchUp;
Events() {}
public static Events Instance {get; private set;}
void Start() {
Instance = this;
}
void Update() {
if (Input.GetKey(KeyCode.A))
if (KeyA != null)
KeyA();
if (Input.GetMouseButtonUp(0))
if (TouchUp != null)
TouchUp(Input.mousePosition);
}
}
So what do you think? Is this a good or bad practice? I am thinking of performance, architecture, readability… I imagine Update
is very heavily optimized in Unity so maybe this event
madness of mine is just not worth it.