Search Unity

Exit application script only run once on pressing GUI.Button

Discussion in 'Scripting' started by Exnihilon, Jul 30, 2014.

  1. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    158
    Hi to all. I'm developing an application on Android and I need to implement an "EXIT" function with the use of the following script(JS-Unityscript):

    Code (CSharp):
    1.  
    2. //Exit Button script - Working one time only on pressing GUI.Button.
    3.  
    4. if (btn) // Exit Button
    5.                     {
    6.                 Debug.LogError("Please assign a texture on the inspector");
    7.                 return;
    8.                 }
    9.                
    10.                 // Make sure we only call GUI.Window if exitIsOn is true.
    11.                 if(GUI.Button(Rect(10,10, 60, 30), ""))
    12.                 {
    13.                     exitIsOn = !exitIsOn;
    14.                 }
    15.                
    16.                 if(exitIsOn)
    17.                 if(count == 0)
    18.                 {
    19.                
    20.                 GUI.skin = GUISkin;
    21.                 GUI.color = Color.white;
    22.                 GUI.Box(Rect(0, 0, 480, 320),"Do you want to EXIT application");
    23.                 GUI.backgroundColor = Color.white;
    24.                 GUI.color = Color.white;
    25.                 if(GUI.Button(Rect(100, 100, 100, 50),))
    26.                       {
    27.                      Application.Quit();
    28.                       }      
    29.                       GUI.backgroundColor = Color.white;
    30.                       GUI.contentColor = Color.white;
    31.                       if(GUI.Button(Rect(Screen.width/4,Screen.height*4/8,Screen.width/2,Screen.height/8),"No thank you, I'll stay"))
    32.                       if(GUI.Button(Rect(100, 152, 100, 50),))
    33.                       {
    34.                       count = 0;
    35.                       count = 1;
    36.                       Time.timeScale = 1;
    37.                       }
    38.                 }
    39.             }
    40.  
    Problem is that, on testing at device "EXIT" button, is only functional once. If you choose from that choice menu to return to the application, and you press this button again it does not function. It has to be functional every time it is clicked, once returning to application .
    Can someone be kindly helpful and give some edit on this script, so make this "EXIT" but behave as is should? I cannot think off, what is wrong with this script, as I'm novice in Unity and scripting. Thank you all in advance for your time and help.
     
  2. Dublinjonny

    Dublinjonny

    Joined:
    May 31, 2013
    Posts:
    61
    you trying to press an exit button , which pops up two buttons , one will exit the app and the second will just close the exit pop ups and stay in the game ? If that's what your trying to do ... Scrap that code its all over the place . I'm not at my desk to write and example

    but just start with a gui.toggle not a button . With a toggle button it should be associated with a bool I.e exittoggle ....

    when you press the toggle add two gui buttons in the toggle function one will exit the game the second simply returns the exit toggle bool to false and closes the toggle button . Its very easy to use and you can also asign the mobile devices back button to return the exit toggle to true and pop up the two buttons
     
  3. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    158
    Hello @Dublinjonny. Well, your suggestion on using a gui.toggle button, does make sense, and the code would be more consistent than mine. Problem is that I need a texture gui.toggle, and I don't now how to make one. If you could post an example it would be marvelous.
     
  4. Dublinjonny

    Dublinjonny

    Joined:
    May 31, 2013
    Posts:
    61
    Ok give me 10 minutes dude
     
  5. Dublinjonny

    Dublinjonny

    Joined:
    May 31, 2013
    Posts:
    61
    Hey dude sorry it took so long had stuff to sort out scripting here.

    Anyway just drag this script onto an empty gameobject or onto the MainCamera .

    Remember you cant test an exit function from within unity . Ive also added a mobile button step back feature as well

    NOTE: you cannot put a game on any store if it exits from every scene it should only exit from the main menu and any exit function in all other scenes should just return to a previous menu or the main menu

    BTW if you press the main toggle button below a second time it will also close the exit menu and buttons

    Enjoy

    Code (CSharp):
    1.  
    2. // In the Unity Inspector you will see drop down Menus called menuExit , exitYes , exitNO.
    3. //
    4. // On the menuExit place a texture/picture that you want the exit button to look like say for example a big Red X etc etc . Place this
    5. // texture in the section that states >> Normal. Ok Now if you would like to have the button change colour when the exit buttons pop up
    6. // so people know the button is active just drop a second texture for example a big Green X etc etc .and drop it in the section that states >> OnNormal
    7.  
    8. // for both the exitYes and ExitNo drop downs sections these are just buttons not toggles so you can place the exit textures (yes and no)
    9. // in the relevent sections that say NORMAL . if you would like to lets say perhaps flash a colour change on the button you can add a new texture
    10. // say for example a yesExit with a green bakground texture this will flash to show a selection has been made
    11.  
    12. //  from inside all these menus in the inspector you can set text size and text colour you can add text to the exitYes and ExitNo buttons by
    13. // placing text at the end of the code inside these >> ""  (in the inspector set the text to clamped  middle Center for them to be set to
    14. // the middle of the texture etc etc
    15.  
    16.  
    17.  
    18.  
    19. using UnityEngine;
    20. using System.Collections;
    21.  
    22. public class ExitingGame : MonoBehaviour {
    23.  
    24.  
    25.     bool                 menuExitToggle  = false;
    26.  
    27.     public GUIStyle      menuExit, exitYes, exitNo;
    28.  
    29.  
    30.     void Update () {
    31.  
    32.         // This will bring up the Exit the game Application when the back button on the device is pressed as in the android back button
    33.  
    34.         if (Input.GetKeyDown (KeyCode.Escape) )
    35.         {
    36.             menuExitToggle  =  true;
    37.  
    38.         }
    39.  
    40.     }
    41.  
    42.     void OnGUI(){
    43.  
    44.         // This is the GUI.Toggle button , as you can see it is given a size and location below , assigning the button size and locattion will
    45.         // make it relevent to the apps mobile screen size and works much better than the way you write it
    46.  
    47.         // As you can see it is also a bool so it sets either true or false , but at the end where it states this.menuExit we are attaching it to the GUIStyle above
    48.  
    49.         if (menuExitToggle = GUI.Toggle (new Rect (Screen.width / 1.187f, Screen.height / 1.091f, Screen.width / 8f, Screen.height / 14f), menuExitToggle, "", this.menuExit))
    50.         {
    51.  
    52.             // These are the two buttons that will pop up when you pres the toggle . The GUI.SKIN is attaching the button to moreGUIStyles above
    53.  
    54.  
    55.        
    56.         GUI.skin.button = exitYes;
    57.         if (GUI.Button (new Rect (Screen.width / 1.95f, Screen.height / 3.1f, Screen.width / 3.5f, Screen.height / 6f), ""))
    58.             {
    59.             Application.Quit ();
    60.             }
    61.  
    62.  
    63.             GUI.skin.button = exitNo;
    64.             if (GUI.Button (new Rect (Screen.width / 1.95f, Screen.height / 1.8f, Screen.width / 3.5f, Screen.height / 6f), ""))
    65.             {
    66.             menuExitToggle = false;
    67.             }
    68.  
    69.     }
    70. }
    71. }
     
    Last edited: Jul 31, 2014
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Why would you use a toggle and a repeat button? And why make separate skins for every button instead of declaring individual styles in one skin?

    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4.     if (menuOpen)
    5.     {
    6.         if (!optionsOpen)
    7.         {
    8.              if (GUI.Button("Exit Game"))
    9.                   optionsOpen = true;
    10.              if (GUI.Button("Cancel"))
    11.                   menuOpen = false;
    12.         }
    13.         else
    14.         {
    15.                if (GUI.Button("Yes"))
    16.                   Application.Quit();
    17.                if (GUI.Button("No"))
    18.                   optionsOpen = false;
    19.         }
    20.     }
    21. }
    22.  
     
  7. Dublinjonny

    Dublinjonny

    Joined:
    May 31, 2013
    Posts:
    61
    RepeatButton is a typo . Its a lot easier trying to show someone using a toggle than adding more than one bool its also a lot easier and straight forward as is just using seperate Skins taking he does not know how to use them . Declaring all the styles in one skin is a better way to do things but this way he can start using them and get the basic concept and take things further if he wants to learn more .

    Or we can trow out suggestions that he has not got any concept of the basic form of using them and spend days on a thread going over and over the same thing . the script above works flawlessly for what he wants to do , if he wants to expand on it and learn to make it more compact then he has a starting basic point to work from
     
    Last edited: Jul 31, 2014