Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Make Button to Pop Up Window

Discussion in 'Immediate Mode GUI (IMGUI)' started by Sekai92, Jun 23, 2015.

  1. Sekai92

    Sekai92

    Joined:
    Apr 22, 2015
    Posts:
    32
    Sir i want to make About button, but when i click the windows does not appear

    I have code like this, the About button appear on start but when i click the window don't appear
    Am i missing something?

    Code (CSharp):
    1. public Rect windowRect0 = new Rect(20, 20, 120, 50);
    2.    
    3.     void OnGUI() {
    4.  
    5.         if(GUI.Button(new Rect(10,20,100,20), "About"))
    6.         {
    7.             windowRect0 = GUI.Window(0, windowRect0,DoMyWindow,"About");
    8.         }
    9.  
    10.         //windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, "My Window");
    11.     }
    12.     void DoMyWindow(int windowID) {
    13.         GUI.Label(new Rect(20, 20, 100, 20), "Vers. 1");
    14.         if (GUI.Button (new Rect (30, 20, 100, 20), "Close"))
    15.             Destroy (this);
    16.      
    17.         GUI.DragWindow(new Rect(0, 0, 10000, 10000));
    18.     }
    19.  
     
  2. Mr-Mud

    Mr-Mud

    Joined:
    Mar 8, 2015
    Posts:
    37
    The issue here is that the window is only drawn during the frame you clicked the button (the moment you release it, I believe). As this is only true for a single frame, it is quite hard to spot that it actually draws it.

    My advice is to also keep track of a bool(ean) which is responsible for determining whehter the window should be drawn. Set the bool to true if the "About"-button is pressed. Then - outside of the button's if - test whether the bool is true, if so: draw the window.

    On another note, I am not quite sure about the usage of destroy in this context. I assume you want to use it to close the "About"-window, in which case you could better set the just introduced bool to false; that way you can open it again should you so desire.
    Slight notice regarding the code, it is untested; so it might contain a bug. In any case here you go.
    Code (CSharp):
    1. private Rect windowRect0 = new Rect(20, 20, 120, 50);
    2. private bool showWindow = false;
    3.  
    4. void OnGUI()
    5. {
    6.     if(GUI.Button(new Rect(10,20,100,20), "About"))
    7.         showWindow = true;  
    8.  
    9.     if (showWindow)
    10.     {
    11.         windowRect0 = GUI.Window(0, windowRect0,DoMyWindow,"About");
    12.     }
    13. }
    14.  
    15. void DoMyWindow(int windowID)
    16. {
    17.     GUI.Label(new Rect(20, 20, 100, 20), "Vers. 1");
    18.     if (GUI.Button (new Rect (30, 20, 100, 20), "Close"))
    19.         showWindow = false;
    20.    
    21.     GUI.DragWindow(new Rect(0, 0, 10000, 10000));
    22. }
     
    Sekai92 likes this.
  3. Sekai92

    Sekai92

    Joined:
    Apr 22, 2015
    Posts:
    32
    oh i'm missing boolean, thank you for reply sir, really helpful. Thanks now my windows worked, perfectly :)
     
    Last edited: Sep 23, 2015