Search Unity

Can't seem to hide the GUI

Discussion in 'Immediate Mode GUI (IMGUI)' started by DaveyJJ, Nov 16, 2007.

  1. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Hi all ... thanks to StarManta I have all of this working EXCEPT the fact that the GUI isn't being hidden at the beginning and then showing when I want it to ... relevant parts of code highlighted in ALL CAPS

    Code (csharp):
    1.  
    2. // here's where we display the time remaining and game over message
    3. var timershow : GUIText;
    4. var gameovermessage : GUIText;
    5. var whistleClip : AudioClip;
    6.  
    7. function Start () {
    8.     // HIDE THE GUI UNTIL STUFF HAPPENS IN THE UPDATE
    9.     GUI.enabled = false;
    10. }
    11.  
    12. function Update ()
    13. {
    14.     /* mathf.floor forces it to round to 0 */
    15.     timershow.text = "time left =" + Mathf.Floor(GameController.timeLeft);     
    16.     // sets up time to count down and control the time counting down
    17.     GameController.timeLeft -= Time.deltaTime;          
    18.     // checks to see if time has run out
    19.     if ((GameController.timeLeft < 0) || (GameController.timeLeft == 0)) { 
    20.        
    21.         // put "0" into the timeleft display
    22.         timershow.text = "0";
    23.        
    24.         // play whistle sound and display time out message
    25.         PlayWhistleSound();
    26.        
    27.         // pause time time - either with Time.timeScale = 0 or using our method of pausing game
    28.         LevelOverPause();                                      
    29.     }  
    30. }
    31.  
    32. // plays whistle sound twice for one second and displays time has run out status message
    33. function PlayWhistleSound() {    
    34.     gameovermessage.text = "Time has run out!";
    35.     audio.clip = whistleClip;
    36.     audio.Play();
    37.     yield WaitForSeconds(1);
    38.     audio.Stop();
    39. }
    40.  
    41. // pauses game after level runs out
    42. function LevelOverPause () {
    43.     yield WaitForSeconds(2.5);
    44.     gameovermessage.text = "";
    45.    
    46.     // NOW SHOW THE GUI FOR THE FIRST TIME
    47.     GUI.enabled = true;
    48.    
    49.     // show cursor and unlock screen and turn mouse look off of player character
    50.     // mouse look off here please
    51.     Screen.lockCursor = false;
    52.     Screen.showCursor = true;
    53.            
    54.     Time.timeScale = 0;
    55.     // GamePlayScript = GameObject.Find("ScriptHolder").GetComponent(GamePlay);
    56.     // GamePlayScript.AreWePaused = true;
    57.    
    58. }
    59.  
    60. // assign the GUISkin to be used as the custom one for the game in the Inspector
    61. var mySkin : GUISkin;
    62.  
    63. function OnGUI () {
    64.     // Assign the skin to be the one currently used.
    65.     GUI.skin = mySkin;
    66.     // create a window with controls inside it
    67.     var windowRect : Rect = Rect (Screen.width / 2 - 150, Screen.height / 2 - 100, 300, 200);
    68.     windowRect = GUI.Window (0, windowRect, WindowFunction, "Level complete!");
    69. }
    70.  
    71. // draw the contents of the submit score window like an include file
    72. function WindowFunction (windowID : int) { 
    73.     var levelOverInstructions = "\nTime has run out. You can now submit your score online, play again, or quit the game.\n";
    74.     GUI.Label (Rect (25,20,250,60), levelOverInstructions);
    75.    
    76.     // need to add a field to show player final score with multiplier here
    77.     // add field here and make sure to add height and move objects
    78.    
    79.     var submitNameString = "Click here to enter your name";
    80.     submitNameString = GUI.TextField (Rect (50,80,200,20), submitNameString);
    81.    
    82.     // then we show three buttons to submit score, play again, or quit
    83.     if (GUI.Button (Rect (75,105,150,25), "Submit Score")) {
    84.             // submit scores to server code goes here -- I have this done but not added
    85.             // load level Scores
    86.             Application.LoadLevel ("Scores");
    87.         }
    88.     if (GUI.Button (Rect (40,150,100,25), "Play Again")) {
    89.             // don't submit score and simply take player back to select screen
    90.             Application.LoadLevel ("Select");
    91.         }
    92.     if (GUI.Button (Rect (160,150,100,25), "Quit")) {
    93.             // don't submit score and send player to credit screen
    94.             Application.LoadLevel ("Credits");
    95.         }      
    96. }
    97.  
    What am I doing wrong?
     
  2. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    I haven't experimented with GUI.enabled, but isn't the obvious solution to not draw all the GUI stuff unless you want to? It's what I'm doing.

    Don't need to disable what isn't there.

    Oh, and note, GUI seems to have no memory. If you assign GUI.skin a value at time X then at time Y it will have defaulted back to nothing.

    So I'm guessing you'd need to set GUI.enabled in the onGUI () function -- which kind of defeats the purpose. If you don't want to show it, don't show it :)

    function OnGUI() {
    if ( interfaceVisible ){
    GUI.Button (....);
    }
    }
     
  3. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    Unless I am mistaken, this is failing because you're setting GUI.enabled outside of OnGUI(). Try this, it should work:

    Code (csharp):
    1.  
    2. // here's where we display the time remaining and game over message
    3. var timershow : GUIText;
    4. var gameovermessage : GUIText;
    5. var whistleClip : AudioClip;
    6. var guiEnabledState : boolean = false;
    7.  
    8. function Start () {
    9.     // HIDE THE GUI UNTIL STUFF HAPPENS IN THE UPDATE
    10.     guiEnabledState = false;
    11. }
    12.  
    13. function Update ()
    14. {
    15.     /* mathf.floor forces it to round to 0 */
    16.     timershow.text = "time left =" + Mathf.Floor(GameController.timeLeft);     
    17.     // sets up time to count down and control the time counting down
    18.     GameController.timeLeft -= Time.deltaTime;          
    19.     // checks to see if time has run out
    20.     if ((GameController.timeLeft < 0) || (GameController.timeLeft == 0)) { 
    21.        
    22.         // put "0" into the timeleft display
    23.         timershow.text = "0";
    24.        
    25.         // play whistle sound and display time out message
    26.         PlayWhistleSound();
    27.        
    28.         // pause time time - either with Time.timeScale = 0 or using our method of pausing game
    29.         LevelOverPause();                                      
    30.     }  
    31. }
    32.  
    33. // plays whistle sound twice for one second and displays time has run out status message
    34. function PlayWhistleSound() {    
    35.     gameovermessage.text = "Time has run out!";
    36.     audio.clip = whistleClip;
    37.     audio.Play();
    38.     yield WaitForSeconds(1);
    39.     audio.Stop();
    40. }
    41.  
    42. // pauses game after level runs out
    43. function LevelOverPause () {
    44.     yield WaitForSeconds(2.5);
    45.     gameovermessage.text = "";
    46.    
    47.     // NOW SHOW THE GUI FOR THE FIRST TIME
    48.     guiEnabledState = true;
    49.    
    50.     // show cursor and unlock screen and turn mouse look off of player character
    51.     // mouse look off here please
    52.     Screen.lockCursor = false;
    53.     Screen.showCursor = true;
    54.            
    55.     Time.timeScale = 0;
    56.     // GamePlayScript = GameObject.Find("ScriptHolder").GetComponent(GamePlay);
    57.     // GamePlayScript.AreWePaused = true;
    58.    
    59. }
    60.  
    61. // assign the GUISkin to be used as the custom one for the game in the Inspector
    62. var mySkin : GUISkin;
    63.  
    64. function OnGUI () {
    65.     GUI.enabled = guiEnabledState;
    66.     // Assign the skin to be the one currently used.
    67.     GUI.skin = mySkin;
    68.     // create a window with controls inside it
    69.     var windowRect : Rect = Rect (Screen.width / 2 - 150, Screen.height / 2 - 100, 300, 200);
    70.     windowRect = GUI.Window (0, windowRect, WindowFunction, "Level complete!");
    71. }
    72.  
    73. // draw the contents of the submit score window like an include file
    74. function WindowFunction (windowID : int) { 
    75.     var levelOverInstructions = "\nTime has run out. You can now submit your score online, play again, or quit the game.\n";
    76.     GUI.Label (Rect (25,20,250,60), levelOverInstructions);
    77.    
    78.     // need to add a field to show player final score with multiplier here
    79.     // add field here and make sure to add height and move objects
    80.    
    81.     var submitNameString = "Click here to enter your name";
    82.     submitNameString = GUI.TextField (Rect (50,80,200,20), submitNameString);
    83.    
    84.     // then we show three buttons to submit score, play again, or quit
    85.     if (GUI.Button (Rect (75,105,150,25), "Submit Score")) {
    86.             // submit scores to server code goes here -- I have this done but not added
    87.             // load level Scores
    88.             Application.LoadLevel ("Scores");
    89.         }
    90.     if (GUI.Button (Rect (40,150,100,25), "Play Again")) {
    91.             // don't submit score and simply take player back to select screen
    92.             Application.LoadLevel ("Select");
    93.         }
    94.     if (GUI.Button (Rect (160,150,100,25), "Quit")) {
    95.             // don't submit score and send player to credit screen
    96.             Application.LoadLevel ("Credits");
    97.         }      
    98. }
    99.  
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    GUI.enabled isn't really for showing/hiding the GUI in the same way that you'd use it for the old GUI stuff. It's not going to work outside of the GUI routine anyway. It's for dimming out individual elements that you don't want the user to click on for whatever reason; they're still there and visible, just "grayed out". Probably should make a boolean instead, and toggle the entire GUI based on that.

    Code (csharp):
    1.  
    2. private var showGUIstuff = false;
    3.  
    4. function onGUI() {
    5.      if (showGUIstuff) {
    6.           // All GUI stuff here
    7.      }
    8. }
    --Eric
     
  5. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Thanks Sam but the "GUI.enabled" actually doesn't hide the GUi it simply makes it very transparent. I'm now fiddling as below trying to use an if/else statement to "move" the GUI if your state is true or not.

    Code (csharp):
    1.  
    2. // here's where we display the time remaining and game over message
    3. var timershow : GUIText;
    4. var gameovermessage : GUIText;
    5. var whistleClip : AudioClip;
    6. var guiEnabledState : boolean = false;
    7.  
    8. function Start () {
    9.     // hide the gui
    10.     guiEnabledState = false;
    11. }
    12.  
    13. function Update ()
    14. {
    15.     /* mathf.floor forces it to round to 0 */
    16.     timershow.text = "" + Mathf.Floor(GameController.timeLeft);    
    17.     // sets up time to count down and control the time counting down
    18.     GameController.timeLeft -= Time.deltaTime;          
    19.     // checks to see if time has run out
    20.     if ((GameController.timeLeft < 0) || (GameController.timeLeft == 0)) { 
    21.        
    22.         // put "0" into the timeleft display
    23.         timershow.text = "0";
    24.        
    25.         // play whistle sound and display time out message
    26.         PlayWhistleSound();
    27.        
    28.         // pause time time - either with Time.timeScale = 0 or using our method of pausing game
    29.         LevelOverPause();                                      
    30.     }  
    31. }
    32.  
    33. // plays whistle sound twice for one second and displays time has run out status message
    34. function PlayWhistleSound() {    
    35.     gameovermessage.text = "Time has run out!";
    36.     audio.clip = whistleClip;
    37.     audio.Play();
    38.     yield WaitForSeconds(1);
    39.     audio.Stop();
    40. }
    41.  
    42. // pauses game after level runs out
    43. function LevelOverPause () {
    44.     yield WaitForSeconds(2.5);
    45.     gameovermessage.text = "";
    46.    
    47.     // now show the gui
    48.     guiEnabledState = true;
    49.    
    50.     // show cursor and unlock screen and turn mouse look off of player character
    51.     // mouse look off here please
    52.     Screen.lockCursor = false;
    53.     Screen.showCursor = true;
    54.            
    55.     Time.timeScale = 0;
    56.     // GamePlayScript = GameObject.Find("ScriptHolder").GetComponent(GamePlay);
    57.     // GamePlayScript.AreWePaused = true;
    58.    
    59. }
    60.  
    61. // assign the GUISkin to be used as the custom one for the game in the Inspector
    62. var mySkin : GUISkin;
    63.  
    64. function OnGUI () {
    65.     // turn it off at start of level
    66.     GUI.enabled = guiEnabledState;
    67.     // Assign the skin to be the one currently used.
    68.     GUI.skin = mySkin;
    69.     // create a window with controls inside it
    70.     if (guiEnabledState == "true") {
    71.         var windowRect : Rect = Rect (Screen.width / 2 - 150, Screen.height / 2 - 100, 300, 200);
    72.         windowRect = GUI.Window (0, windowRect, WindowFunction, "Level complete!");
    73.     }
    74.     /* else {
    75.         var windowRectHide : Rect = Rect (-1000, -1000, 300, 200);
    76.         windowRectHide = GUI.Window (0, windowRectHide, WindowFunction, "Level complete!");
    77.     } */
    78. }
    79.  
    80. // draw the contents of the submit score window like an include file
    81. function WindowFunction (windowID : int) { 
    82.     var levelOverInstructions = "\nTime has run out. You can now submit your score online, play again, or quit the game.\n";
    83.     GUI.Label (Rect (25,20,250,60), levelOverInstructions);
    84.    
    85.     // need to add a field to show player final score with multiplier here
    86.     // add field here and make sure to add height and move objects
    87.    
    88.     var submitNameString = "Click here to enter your name";
    89.     submitNameString = GUI.TextField (Rect (50,80,200,20), submitNameString);
    90.    
    91.     // then we show three buttons to submit score, play again, or quit
    92.     if (GUI.Button (Rect (75,105,150,25), "Submit Score")) {
    93.             // submit scores to server code goes here -- I have this done but not added
    94.             // load level Scores
    95.             Application.LoadLevel ("Scores");
    96.         }
    97.     if (GUI.Button (Rect (40,150,100,25), "Play Again")) {
    98.             // don't submit score and simply take player back to select screen
    99.             Application.LoadLevel ("Select");
    100.         }
    101.     if (GUI.Button (Rect (160,150,100,25), "Quit")) {
    102.             // don't submit score and send player to credit screen
    103.             Application.LoadLevel ("Credits");
    104.         }      
    105. }
    106.  
    EDIT ... while there should be a "hide GUI" feature, I was able to seemingly get this wowrking by modifying parts of the above like so ... basically pretending to move the GUI around in the scene and having it initially being drawn way off the scene

    Code (csharp):
    1.  
    2. if (guiEnabledState == false) {
    3.         var windowRectHide : Rect = Rect (-1000, -1000, 300, 200);
    4.         windowRectHide = GUI.Window (0, windowRectHide, WindowFunction, "Level complete!");
    5.     }
    6.     if (guiEnabledState == true) {
    7.         gameovermessage.text = "";
    8.         var windowRect : Rect = Rect (Screen.width / 2 - 150, Screen.height / 2 - 100, 300, 200);
    9.         windowRect = GUI.Window (0, windowRect, WindowFunction, "Level complete!");
    10.     }
    11.  
     
  6. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Another problem now ... I can't seem to edit the text ... located in the TextField. I can place the cursor and type but nothing changes? I can even double-click to highlight the current string but I can't replace it.

    Code (csharp):
    1.  
    2. var submitNameString = "Click to enter your name";
    3.     submitNameString = GUI.TextField (Rect (50,80,200,20), submitNameString, 35);
    4.  
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I think you missed my post above. :)

    You're setting submitNameString to "Click to enter your name" every onGUI call. onGUI is like Update, so submitNameString gets defined over and over again every frame, which means editing it can't be done since it's reset the next frame. So just have submitNameString be a global variable that's defined once, and not in onGUI.

    --Eric
     
  8. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Yes I did, thanks Eric. I hate people who post long snippets of code in their postings so that replies get easily lost ... oh wait, never mind, that was me.

    I'll get that taken care of right now. Thanks! Game's looking good, screen shot to come soon.
     
  9. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    If you just disable your script, you will not get calls to OnGUI, and hence no GUI becomes visible.