Search Unity

Toggle or trigger a gui popup window

Discussion in 'Immediate Mode GUI (IMGUI)' started by Bluestrike, Oct 10, 2009.

  1. Bluestrike

    Bluestrike

    Joined:
    Sep 26, 2009
    Posts:
    256
    Is this possible and how should I aproach this.
    I made me a start menu with options menu etc. and I figured I could just do something like:
    Code (csharp):
    1.  
    2. function ShowMenu()
    3. {
    4.     if (GUI.Button(  Rect ((Screen.width/3) * 2, (Screen.height/3) + 150, 100, 30), "Options"))
    5.     {
    6.            DoSomething();
    7.     }
    8. }
    So I just had to call ShowMenu() to display the popup menu, but it doesn't work that way I guess :)

    What I am trying to do is that when the player hits this trigger a small popup window should come up where he has to select a choice from a few buttons and it should go away again when he presses any of the buttons.
    Also, would it be possible to link the buttons to any number key so players who like to be quick can just push a key :)

    Thanks.
     
  2. sebako

    sebako

    Joined:
    Jun 27, 2009
    Posts:
    301
    Yes it is possible:

    try something like this:

    Code (csharp):
    1.  
    2.  
    3. var showMenu = false;
    4.  
    5. function OnGui()
    6. {
    7.   if(getKeyDown("esc"))
    8.     showMenu = !showMenu
    9.   if (GUI.Button(  Rect ((Screen.width/3) * 2, (Screen.height/3) + 150, 100, 30), "Options"))
    10.   CloseMenu();
    11. }
    12.  
    13. then you display the menu you want, and to close the menu wenn a button is clicked you simply create a
    14.  
    15. function CloseMenu()
    16. {
    17.   showMenu = false;
    18. }
    19.  
    20. I hope to understand you right.
    21.  
    22. rgds
    23.  
    24. zem
     
  3. Bluestrike

    Bluestrike

    Joined:
    Sep 26, 2009
    Posts:
    256
    I see how the menu can be enabled/disabled and how to attach keys to it, thank you.

    In case anyone else stumbles on this topic:
    Code (csharp):
    1.  
    2. var showMenu = false ; // The trigger can change it to true.
    3.  
    4. function OnGui()
    5. {
    6. if (showMenu)
    7. {
    8.     if (Input.GetKeyDown("1"))     
    9.     // when player pushes the 1 key its the same as if the clicked on the button
    10.     {
    11.         DoSomething(); 
    12.         showMenu = false ;
    13.     }
    14.  
    15.     if (GUI.Button(  Rect (0 ,0 ,0 ,0), "1- menu option one"))      // button 1
    16.     {                                                  
    17.         DoSomething() ;
    18.         showmenu = false ;
    19.     }  
    20. }  
    21. )
    22.  
     
    SanSolo likes this.
  4. n4t3nate

    n4t3nate

    Joined:
    Oct 16, 2011
    Posts:
    1
    Another solution :


    Code (csharp):
    1. private var toggle : int = 1;
    2.  
    3. function Update(){
    4.  
    5.     if (Input.GetKeyDown(KeyCode.Tab)){
    6.         toggle = (toggle + 1)%2;
    7.     }
    8.    
    9. }
    10.  
    11. function OnGUI(){
    12.  
    13.     if (toggle == 0){
    14.         //show GUI elements
    15.     }
    16. }
     
  5. shader

    shader

    Joined:
    Apr 4, 2009
    Posts:
    253
    the above script is the simplest and works.
     
  6. 1337hephaestus

    1337hephaestus

    Joined:
    Jun 19, 2012
    Posts:
    1

    i'd prefer

    Code (csharp):
    1.  
    2. private var toggle : bool= false;
    3.  
    4. function Update(){
    5.  
    6.     if (Input.GetKeyDown(KeyCode.Tab)){
    7.         toggle = !toggle;
    8.     }
    9.    
    10. }
    11.  
    12. function OnGUI(){
    13.  
    14.     if (toggle){
    15.         //show GUI elements
    16.     }
    17. }
     
  7. krichardson95

    krichardson95

    Joined:
    Nov 25, 2014
    Posts:
    1

    So if I wanted to put a script GUI instead of making one, would I just put a
    <name of script> Where the toggle is?