I wonder if there’s a way to set listeners for lets say a GUI Button so that I would know if the user is dragging the button or releasing it?
To reduce code lines I prefer not to use rect, I’m looking for an example of using “Event” OnGUI function.
Grazies.
In short, no. The GUI system is an immediate system. It operates in the order you call the functions. There are no persistent GUI objects. The Event class is the class that is used by all GUI functions. You can use it to implement your own behaviour, but it just provides global events for the GUI, not control-specific.
OnGUI is called several times a frame. Each event in the EventType enum will cause OnGUI to run. Even GUI layout doesn’t use true GUI controls. In the first layout-step it gathers the element count and due to their style and content it generates a list of rects for each element you’ve called in OnGUI. In the Repaint-step it draws and handles the elements like the normal GUI elements. That’s why you shouldn’t change the elements between layout and repaint or you mess up the control stack.
The only exception are windows. Windows are “created” in OnGUI, but the callback is called after OnGUI is finished. The point is that the callbacks are treated differently. Normally the elements are processed in the order you call them. That causes that the first one of two overlapping elements is catching the events but it’s also drawn first. So the bottom most elements has the input focus. Windows have a z-order. They seperated the input handling and repaint thanks to the callbacks.