0
\$\begingroup\$

Here's what I don't understand about Events:

the documentation says that Events correspond to user input (key presses, mouse actions), or are UnityGUI layout or rendering events. For each event OnGUI is called in the scripts; so OnGUI is potentially called multiple times per frame. Event.current corresponds to "current" event inside OnGUI call.

the documentation pertaining to onGUI states that it hasone call per event

That doesn't make sense to me, because I know I can call Events within OnGUI.

I have an OnGUI function that draws a grid using x and y nested for loops, then checks using Event.current.mousePosition if your mouse is hovering over a grid block. What I don't get is how OnGUI is ever called, if OnGUI is only called when an event occurs, but I have the event inside OnGUI so how can you ever check if the event is being called inside a script that is only called after the event is called?

I know that probably sounds confusing -- I'm just puzzled by the seemingly cyclical nature of ongui.

\$\endgroup\$
  • 1
    \$\begingroup\$ Event.current.mousePosition is not triggering an event, it's accessing the event that triggered the current OnGUI method call. OnGUI is called once per frame and once for every event. \$\endgroup\$ – MichaelHouse Jun 13 '15 at 22:33
  • 1
    \$\begingroup\$ so what would be the difference between something like Event.current.mousePosition and just Input.mouseposition? \$\endgroup\$ – nachime Jun 14 '15 at 3:14
  • \$\begingroup\$ Between those two? Likely nothing. However, Input and Event.current do not overlap each other entirely. Comparing them isn't really practical. \$\endgroup\$ – MichaelHouse Jun 14 '15 at 4:46
3
\$\begingroup\$

because I know I can call Events within OnGUI.

Accessing data from the current event i.e. Event.current.mousePosition is not "calling an event", at least not in the sense that you're activating the event so triggers an OnGUI call. As the documentation says, that you posted in your question:

Event.current corresponds to "current" event inside OnGUI call.

That means using Event.current.mousePosition is just accessing the event that triggered the OnGUI call, not triggering a new event.

Events are triggered outside of OnGUI via user interaction with the UI.

There is one event type that happens every frame, no matter the user input, Repaint. Repaint is sent every frame when the GUI is redrawn. This is the last event processed. This is where your grid is drawn every frame, because this event happens every frame.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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