Search Unity

How to create a Pop-up window??

Discussion in 'Immediate Mode GUI (IMGUI)' started by missy_pooh, Jul 11, 2011.

  1. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    Hello,

    I would like to ask how to create a Pop-up window. For example, currently i am working on a puzzle games. Which mean that player click on "A B E" (invalid word form), it will have a pop up window indicate that "Wrong word formation, please try again". Please Help.

    Thank you
     
  2. amirghayes

    amirghayes

    Joined:
    Apr 8, 2011
    Posts:
    98
    make a boolean in your code which gets to true when this condition happens

    then inside OnGUI() function put whatever gui control you wish to show inside an if statement which checks this boolean
     
    JakeTheDog104 likes this.
  3. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    hello amirghayes,

    Thank you for the reply. but i do not know how to get started with it. Could you give some example on how i can do it? I mean the onGUI.

    function onGUI()
    {
    //pop up window
    }
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Search the scripting reference for GUI.Window or GUILayout.Window.
     
  5. Spectre9000

    Spectre9000

    Joined:
    Aug 30, 2010
    Posts:
    170
    bool openWindow;

    function OnGUI() {
    if(openWindow){
    //Make your GUI items here. Set openWindow to true when you want the pop up to appear, and false when you want it gone.
    }
    }
     
  6. amirghayes

    amirghayes

    Joined:
    Apr 8, 2011
    Posts:
    98
    lol here you got the answers very fast before I even try
    you guys rocks
     
  7. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    hello all, thank you for the reply :)) I will try it out.. :)
     
  8. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    hi,

    I got some questions. I do not know where should i place my function onGUI()??

    Basically what i want is for example, player click " A T K " -> which is invalid word, i want to pop up a window saying "Sorry wrong word formation, please try again". I have a script called mouseclick.js that is place on every alpahebets. ( "A", "T", "K"). I have another script called gamecontrol which will check whether A T K is a valid words. So my qn is i should put the function onGUI at the mouse click or the gamecontrol?? I put it in gamecontrol, but it doesnt work properly.
     
  9. amirghayes

    amirghayes

    Joined:
    Apr 8, 2011
    Posts:
    98
    first I think it is easier for us if you show us the code so that we know where is the error
    if I understand right then you did it right as the OnGUI() should be placed in the gamecontrol script , tho you can put it in whatever script but you have to be able to set its bolean to true from the gamecontrol script - neglect the last part if it is hard for you
    anyway I will try to put a code snippet here to try and see if it works
    in C# tho

    put the construction in the gamecontrol script
    Code (csharp):
    1.  
    2.  
    3. private bool showPopUp = false;
    4.  
    5. void Update()
    6. {
    7.  // put your if statement here and set showPopUp to true if the condition is true
    8.  // put an else statement to set the bolean to false if it is not true
    9.  
    10. }
    11.  
    12. void OnGUI()
    13. {
    14.   if (showPopUp)
    15.    {
    16.      GUI.Window(0, new Rect((Screen.width/2)-150, (Screen.height/2)-75
    17.             , 300, 250), ShowGUI, "Invalid word");
    18.  
    19.    }
    20. }
    21.  
    22. void ShowGUI(int windowID)
    23.      {
    24.          // You may put a label to show a message to the player
    25.  
    26.          GUI.Label(new Rect(65, 40, 200, 30), "PUT YOUR MESSAGE HERE");
    27.        
    28.          // You may put a button to close the pop up too
    29.  
    30.           if (GUI.Button(new Rect(50, 150, 75, 30), "OK"))
    31.          {
    32.              showPopUp = false;
    33.              // you may put other code to run according to your game too
    34.          }
    35.  
    36.       }
    37.  
    of course you can change the numbers in this example to whatever suits your game

    I hope that heleped

    if you can't understnd this C# code or can't convert it to JS , then let me know and I will try to make the JS version for you

    good luck
     
  10. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    hello amirghayes, thank you so much for the help.. :) I will try to convert it back to javascript. Thank you for follow up with this thread. I nearly going to open new thread for answer :p Anyway to add on, i don't know if i did mention about it, i will have two pop up window, one will be for the correct word form one for the incorrect word formed. So am i right to say that i will need something like two showGUI or any boolean variables?
     
  11. amirghayes

    amirghayes

    Joined:
    Apr 8, 2011
    Posts:
    98
    You are welcome
    if you gonna need 2 windows to be shown at the same time , then yes I think you gonna need 2 bolean variables
    but I don't think that is your case
    if you gonna need 1 window to pop up at the time , you then keep the window but change the condition to alter the label or window message value
    like u change the part of "Invalid word" with a string variable , then set the value of this string to "Invalid word" or "Correct word" in the condition check function or code , this way you don't need to type a code for 2 different windows for different condition

    and you can do the same for label in "PUT YOUR MESSAGE HERE"

    hope that answered your question
    good luck
     
  12. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    Code (csharp):
    1.  
    2. function onGUI()
    3. {
    4.     if(isCorrect)
    5.     {
    6.         GUI.Window(0, new Rect((Screen.width/2)-150, (Screen.height/2)-75, 300, 250), showGUI, "Congratulation!!");
    7.     }
    8.     else
    9.     {
    10.         GUI.Window(0, new Rect((Screen.width/2)-150, (Screen.height/2)-75, 300, 250), showGUI, "Invalid Word Formed");
    11.     }
    12. }
    13.  
    14. function showGUI()
    15. {
    16.         if(isCorrect)
    17.         {
    18.             // You may put a label to show a message to the player
    19.             GUI.Label(new Rect(65, 40, 200, 30), "You have formed the correct words!!! :)");
    20.         }
    21.         else
    22.         {
    23.             GUI.Label(new Rect(65, 40, 200, 30), "Sorry!! Please try again!!");
    24.         }
    25.        
    26.         // You may put a button to close the pop up too
    27.         if (GUI.Button(new Rect(50, 150, 75, 30), "OK"))
    28.         {
    29.              isCorrect = false;
    30.              // you may put other code to run according to your game too
    31.          }
    32. }
    33.  
    Hi, i try doing it. But nothing happened for this code. What wrong??
     
  13. amirghayes

    amirghayes

    Joined:
    Apr 8, 2011
    Posts:
    98
    first of all I think this JS ? if so I don't think you need to put the word "new" before the "rect" I think you just type rect
    I'm very weak in JS so forgive me ,
    second the name of the function is case sensitive , it is OnGUI() , not onGUI()
    here is my try with the code after adjusting it to your need
    but again it is C# , if you realy need its JS version then I think other members could help you or give me a bit of time to figure it out for you

    Code (csharp):
    1.  
    2.     private string windowMessage;
    3.     private string labelMessge;
    4.  
    5.     void OnGUI()
    6.     {
    7.         if (isCorrect)
    8.         {
    9.             GUI.Window(0, new Rect((Screen.width / 2) - 150, (Screen.height / 2) - 75, 300, 250), showGUI, windowMessage);
    10.         }
    11.     }
    12.  
    13.     void showGUI()
    14.     {
    15.         if (isCorrect)
    16.         {
    17.             // You may put a label to show a message to the player
    18.             GUI.Label(new Rect(65, 40, 200, 30), labelMessge);
    19.         }
    20.  
    21.         // You may put a button to close the pop up too
    22.         if (GUI.Button(new Rect(50, 150, 75, 30), "OK"))
    23.         {
    24.             isCorrect = false;
    25.             // you may put other code to run according to your game too
    26.         }
    27.     }
    28.  
    To make this work for you , you need to do the following

    in the condition code that you check for validating
    if he picked up the right compination then set the following variables

    isCorrect = true ;
    windowMessage = "Congratulation!!";
    labelMessage = "You have formed the correct words!!! :)" ;

    if he picked up wrong compination then set the following variables

    isCorrect = true ;
    windowMessage = "Invalid Word Formed";
    labelMessage = "Sorry!! Please try again!!";

    of course you can get the same goal with a lot of different ways , thats how programming is , and thats how I thought about it atm , may be I can do it in a different way in another time

    at last I hope that helped and forgive me if something is wrong I'm still a noob
    will try to help you though if it doesn't work like you expected

    good luck
     
  14. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    HELLO amirghayes, thank you very much. Now it work properly I think is because of the case sensitive OnGUI() instead of onGUI that wise it is not working properly. You really help me alot :) No worry.

    Are you working on a game now as well? Maybe we can talk/discuss more. I need ideas for my games!!!! HAHAH.. :)
     
  15. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    hello,

    just a random question, do you think that if the player form correct/valid words, i should pop up window? Or just pop up window if they form wrong word?? And something i am concern, it is very irritating to pop up window while playing game? In a dilemma. Because if i don't pop up window saying wrong words, the player wont know?? Need some comment.