Unity Global Mouse Events

Most Unity tutorials suggest using mouse events in the update function, for example:

function Update () {

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

   } 

}

This seems really inefficient to me, although it seems like using onEnterFrame in AS or setInterval in JS to power the whole application - I would rather use an event-based system.

The OnMouseDown () method is useful, but it is only triggered if MouseDown is on the object, and not somewhere in the scene.

So here is the question: is there a MouseEvent in Unity to detect if the mouse button is disabled globally, or is this an upgrade solution recommended?

+4
source share
2 answers

, , onEnterFrame AS setInterval JS - , .

, . , , , ​​ , .

/ , . , , /. , , .

, : MouseEvent Unity , ?

, . Btw, , , . - :

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

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

}
+4

, , Unity " " , . . , ​​ .

, (), CoRoutine, , , Unity coroutines , .

+1

All Articles