7

Most Unity tutorials suggest using Mouse events within the Update function, like this:

function Update () {

   if (UnityEngine.Input.GetMouseButton(1)) { 

   } 

}

This strikes me as really inefficient though, similar to using onEnterFrame in AS or setInterval in JS to power the whole application - I'd really prefer to use an events based system.

the OnMouseDown() method is useful, but is only fired when the MouseDown is on the object, not anywhere in the scene.

So here's the question: Is there a MouseEvent in Unity for detecting if the mouse button is down globally, or is the Update solution the recommended option?

1
  • 2
    It's not really inefficient. In fact any event-driven input system does exactly that behind the scenes, so an event-driven approach has likely more overhead.
    – CodeSmile
    Sep 27, 2014 at 14:11

2 Answers 2

7

This strikes me as really inefficient though, similar to using onEnterFrame in AS or setInterval in JS to power the whole application - I'd really prefer to use an events based system.

As already pointed out in comments, this isn't necessary less efficient. Every event based system is probably using a polling routine like that behind the scenes, updated at a given frequency.

In many game engines/frameworks you are going to find a polling based approach for input handling. I think this is related to the fact that input update frequency is directly correlated to the frame rate/update loop frequency. In fact it doesn't make much sense to listen for input at higher or lower frequency than your game loop.

So here's the question: Is there a MouseEvent in Unity for detecting if the mouse button is down globally, or is the Update solution the recommended option?

No there isn't. Btw if you want you can wrap mouse input detection inside a single class, and expose events from there where other classes can register to. Something like:

public class MouseInputHandler : MonoBehavior
{
  public event Action<Vector2> MousePressed;
  public event Action<Vector2> MouseMoved;
  ...

  void Update()
  {
    if (Input.GetMouseButton(0))
    {
      MousePressed(Input.mousePosition);
      ...
    }
  }

}
1
  • In the example the polling routine is wrapped to provide and event based interface.
    – Heisenbug
    Aug 7, 2019 at 11:19
1

Like stated, you can use it without major concerns, Unity will 'make its magic' internally as to set processing power sensitive code execution for you in terms of polling events. That's the beauty of a modern game engine after all. You normally shouldn't have to be hacking your way around a common feature such a mouse click detection.

However if you don't want to go using the main Update() you can make a CoRoutine if you feel more comfortable with that, just bear in mind that Unity coroutines are not multi-threaded neither, so at the end everything needs to wait anyway.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.