Search Unity

Closing a GUI.Window?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Killergull_legacy, Dec 16, 2008.

  1. Killergull_legacy

    Killergull_legacy

    Joined:
    Dec 16, 2008
    Posts:
    9
    Hello!

    I have searched in this forum and in the Unity documentation for information on how to close a created GUI.Window with a GUI.Button.

    I would like to create simple popup windows for settings and help in an application. Then be able to close the popup window with a button.

    Is it possible with GUI.Window? Or is it meant I should use the class EditorWindow for these kind of tasks?


    Simplified example of what I would like to create:

    Code (csharp):
    1.  
    2.  
    3. /* Window example */
    4.  
    5. var windowRect : Rect = Rect (20, 20, 120, 50);
    6.  
    7. function OnGUI () {
    8.     windowRect = GUI.Window (0, windowRect, WindowFunction, "My Window");
    9. }
    10.  
    11. function WindowFunction (windowID : int) {
    12.     // Button to close the window here
    13.         if (GUI.Button (Rect (60, 25,50, 50), "Button")) {
    14.        
    15.                //Code that closes window here?
    16.     }
    17.  
    18. }
    19.  
    20.  
    21.  
    /Johan, Sweden
     
  2. Arges

    Arges

    Joined:
    Oct 5, 2008
    Posts:
    359
    Hi Johan,

    This is one of those things where the Unity GUI tends to get you if (like me) you're used to retained mode GUIs. What Unity has is an immediate mode GUI, which means the whole UI gets redrawn and destroyed with every frame.

    What you need to do so is set up a variable which indicates if the window should be drawn at all, and set this variable to FALSE if your "Close" button is pressed. Then OnGUI, check this variable before drawing the window.

    Best,
     
  3. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Edit: Ricardo beat me to it as I was posting on the IRC channel and took too long to reply. :)

    You should have a property of that script that controls whether the window is drawn or not, then your button toggles that value as necessary (I assume that there is some way to open the window as well). Example:

    Code (csharp):
    1. var windowOpen : boolean = true;
    2. windowRect : Rect = Rect (20, 20, 120, 50);
    3.  
    4. function OnGUI () {
    5.   if (windowOpen) {
    6.     windowRect = GUI.Window (0, windowRect, WindowFunction, "My Window");
    7.   }
    8. }
    9.  
    10. function WindowFunction (windowID : int) {
    11.   // Button to close the window here
    12.   if (GUI.Button (Rect (60, 25,50, 50), "Button")) {
    13.     windowOpen = false;    
    14.   }
    15. }
    Or something like that, the above should be enough to get you going.
     
  4. Killergull_legacy

    Killergull_legacy

    Joined:
    Dec 16, 2008
    Posts:
    9
    Ah..wonderful! Thanks Ricardo and Tom, it solved my problem! :D

    I'm new to this GUI scripting and was looking for a function Window.Close()
    with windowID as a parameter or something. :roll:

    Thanks again for the fast replies!

    /Johan
     
  5. Chom66

    Chom66

    Joined:
    Jun 16, 2016
    Posts:
    1
    [Travel to 2019 version] You can also do:

    Code (CSharp):
    1. if (GUILayout.Button("Close")) {
    2.         this.Close();
    3. }
    Assuming your class is an EditorWindow.