On unity3d website in the manual on this page: http://unity3d.com/support/documentation/Components/gui-Controls.html

I found an example of "GUI.changed" function usage(below). And it looks like it creates toolbar object on every frame. Am I right or not?

/* GUI.changed example */

// JavaScript
private var selectedToolbar : int = 0;
private var toolbarStrings = ["One", "Two"];

function OnGUI () {
    // Determine which button is active, whether it was clicked this frame or not
    selectedToolbar = GUI.Toolbar (Rect (50, 10, Screen.width - 100, 30), selectedToolbar, toolbarStrings);

    // If the user clicked a new Toolbar button this frame, we'll process their input
    if (GUI.changed)
        print ("The toolbar was clicked");

        if (selectedToolbar == 0)
            print ("First button was clicked");
            print ("Second button was clicked");
  • \$\begingroup\$ the function title is onGUI, not onUpdate, or onRender or similar other things. so I can assume it's not creating GUI in every frame but only when one enters GUI enabled scene. \$\endgroup\$ – Ali1S232 Mar 7 '12 at 22:13
  • \$\begingroup\$ It looks really strange. Because in the same block of code they create control and immediately check it's status. Will it be checked on every frame or only one time in the example? \$\endgroup\$ – st0ne_c0ld Mar 7 '12 at 22:19

OnGUI functions are called multiple times a frame. It's called once to do setup code for doing GUILayout stuff. It's called again to actually draw the frames, and it's called N additional times for (I think) each input event that might be relevant to the UI.

This is known as "Immediate Mode GUI", which you can read a bit more about here: http://iki.fi/sol/imgui/ (as well as other places if you look up that phrase, or "IMGUI").


OnGUI is called for rendering and handling GUI events.

This means that your OnGUI implementation might be called several times per frame (one call per event). For more information on GUI events see the Event reference. If the MonoBehaviour's enabled property is set to false, OnGUI() will not be called.


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.