Search Unity

How to close a window?

Discussion in 'Scripting' started by Grady Lorenzo, Feb 18, 2011.

  1. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    I know I have been asking a lot lately, and that I should start answering unread forums to return the favor for so many answered questions.

    How do I close a window? I made a simple boolean, and a button inside the window to change that boolean..but it doesn't change a thing when clicked...

    Code (csharp):
    1. var GettingStartedRect : Rect = Rect (20, 20, 120, 50);
    2. var GUIstyle : GUIStyle;
    3. var Text1 : TextAsset;
    4. var ShowGettingStarted : boolean = true;
    5. function Start (){
    6.     transform.parent.GetComponent("MouseLook").enabled = false;
    7.     GetComponent("MouseLook").enabled = false;
    8.     Screen.showCursor = true;
    9. }
    10.  
    11.  
    12. function OnGUI () {
    13.     // Register the window. Notice the 3rd parameter
    14.     if(ShowGettingStarted){
    15.         GUI.Window (0, GettingStartedRect, DoMyWindow, "Getting Started");
    16.     }
    17. }
    18.  
    19. // Make the contents of the window
    20. function DoMyWindow (windowID : int) {
    21.     GUI.Label(Rect(10, 25, 100, 20), Text1.text, GUIstyle);
    22.     GUI.DragWindow (Rect (0,0,10000,10000));
    23.     if(GUI.Button(Rect(395, 270, 100, 25), "Done")){
    24.         ShowGettingStarted = false;
    25.     }
    26. }
     
  2. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,869
    I don't know that this has anything to do with your problem, but you've placed your button WAY outside of the window.

    You have if(GUI.Button(Rect(395,270,100,25)...

    I'm not an expert with using windows, but I believe the buttons position is relative to the window its in. Meaning (0,0,100,25) would put the button in the upper left corner of the window with a width of 100 and height of 25. Your window is only 120 pixels wide, but you have the button starting at 395 pixels away from the left edge. So!

    Try changing this:
    Code (csharp):
    1.  
    2.     if(GUI.Button(Rect(395, 270, 100, 25), "Done")){
    3.  
    to this:
    Code (csharp):
    1.  
    2.     if(GUI.Button(Rect(5, 5, 100, 25), "Done")){
    3.  
     
  3. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    I mean I adjusted the numbers to put the button inside the window. The window is 500x400, the button being 100x25, with a desired space of 5px around it. but the boolean remains true even after the button has been pressed.

    If I place the button in the OnGUI(), it works fine. But if it inside DoMyWindow(), it does not...
     
    Last edited: Feb 18, 2011
  4. Ahonek

    Ahonek

    Joined:
    Jan 12, 2011
    Posts:
    140
    GUI.DragWindow() should be the last thing called in your window function. That'll fix it.
     
  5. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    And we have a winner, thanks much!