Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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");
        }
        else
        {
            print ("Second button was clicked");
        }
    }
}
share|improve this question
    
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. –  Ali.S Mar 7 '12 at 22:13
    
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? –  st0ne_c0ld Mar 7 '12 at 22:19
add comment

1 Answer 1

up vote 5 down vote accepted

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").

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnGUI.html

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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