Why is checking button click in Unity done this way?

I find the Unity way of writing the GUILayout.Button in onGUI to check for button click to contract the way I am used to seeing UI code, in which you instantiate a UI object and set up a click event handler for it. What’s the reasoning behind doing it this way (from a framework creator’s viewpoint)?

function OnGUI {
    if (GUILayout.Button("Play")) {
        // button clicked  
    }
}

I think this site is more for "how" questions than "why" questions. But I also think OnGUI is the event handler, and it's already been set up, internally. Right? So there you go.

I would assume its faster to check for the buttons that you are interested in as needed, rather than scanning all input and firing individual methods, but I could be wrong.

1 Answer

1

OnGUI is immediate mode. If you want something else, use GUITextures (which can use the OnMouseDown function), or wait for the new GUI system in Unity 4.something.

When is 4.0 coming?

When it's done, but it won't have the new GUI system.