- Home /
Hold the esc button for a pause menu
Hello, I wonder how can I when the key is pressed esc, it feels as if the button is held,
I tried with a boolean, but I had trouble placing it, so here I give you my code to open a menu when pressed on the escape key (here, I must continually maintain my button to leave the menu open) .
Thank you in advance, Gauthier
 using UnityEngine;
 
 using System.Collections;
 
 
 
 public class Menu : MonoBehaviour 
 
 {
 
     public GUISkin guiSkin;
 
     public Texture2D background, LOGO;
 
     public bool DragWindow = false;
 
     public string chargerLeNiveau = "";
 
     public string chargerLeNiveau2 = "";
 
     public string chargerLeNiveau3 = "";
 
     public string chargerLeNiveau4 = "";
 
     public string[] AboutTextLines = new string[0];
 
 
 
     
 
 
 
 
 
     private string clicked = "", MessageDisplayOnAbout = "A propos \n \n ";
 
     private Rect WindowRect = new Rect((Screen.width / 2) - 200, Screen.height / 2, 150, 200);
 
     private float volume = 1.0f;
 
 
 
     private void Start()
 
     {
 
         for (int x = 0; x < AboutTextLines.Length;x++ )
 
         {
 
             MessageDisplayOnAbout += AboutTextLines[x] + " \n ";
 
         }
 
         MessageDisplayOnAbout += "\n Appuyer sur Esc pour revenir en arriere";
 
     }
 
 
 
     private void OnGUI()
 
     {
 
         
 
         
 
         // si escape est appuyé
 
         if (Input.GetKey (KeyCode.Escape))
 
         
 
         {
 
             
 
         
 
             
 
             
 
         if (background != null)
 
             GUI.DrawTexture(new Rect(0,0,Screen.width , Screen.height),background);
 
         if (LOGO != null && clicked != "A propos")
 
             GUI.DrawTexture(new Rect((Screen.width / 2) - 100, 30, 200, 200), LOGO);
 
         
 
         GUI.skin = guiSkin;
 
         
 
         if (clicked == "")
 
         {
 
             WindowRect = GUI.Window(0, WindowRect, menuFunc, "Menu general");
 
         }
 
         else if (clicked == "Options")
 
         {
 
             WindowRect = GUI.Window(1, WindowRect, optionsFunc, "Options");
 
         }
 
         else if (clicked == "A propos")
 
         {
 
             GUI.Box(new Rect (0,0,Screen.width,Screen.height), MessageDisplayOnAbout);
 
         }else if (clicked == "Resolution")
 
         {
 
             GUILayout.BeginVertical();
 
             for (int x = 0; x < Screen.resolutions.Length;x++ )
 
             {
 
                 if (GUILayout.Button(Screen.resolutions[x].width + "X" + Screen.resolutions[x].height))
 
                 {
 
                     Screen.SetResolution(Screen.resolutions[x].width,Screen.resolutions[x].height,true);
 
                 }
 
             }
 
             GUILayout.EndVertical();
 
             GUILayout.BeginHorizontal();
 
             if (GUILayout.Button("Retour"))
 
             {
 
                 clicked = "Options";
 
             }
 
             GUILayout.EndHorizontal();
 
         }
 
         }
 
         
 
         
 
     }
 
 
 
     private void optionsFunc(int id)
 
     {
 
         if (GUILayout.Button("Resolution"))
 
         {
 
             clicked = "Resolution";
 
         }
 
         GUILayout.Box("Volume");
 
         volume = GUILayout.HorizontalSlider(volume ,0.0f,1.0f);
 
         AudioListener.volume = volume;
 
         if (GUILayout.Button("Retour"))
 
         {
 
             clicked = "";
 
         }
 
         if (DragWindow)
 
             GUI.DragWindow(new Rect (0,0,Screen.width,Screen.height));
 
     }
 
 
 
     private void menuFunc(int id)
 
     {
 
         
 
         // si escape est appuyé
 
         if (Input.GetKey (KeyCode.Escape))
 
         {
 
         
 
         
 
     
 
         //Boutons 
 
         if (GUILayout.Button("Lancer le jeu"))
 
         {
 
             //Lancer le jeu si cliquer
 
             Application.LoadLevel(chargerLeNiveau);
 
         }
 
         if (GUILayout.Button("Lancer le jeu 2"))
 
         {
 
             //Lancer le jeu 2 si cliquer
 
             Application.LoadLevel(chargerLeNiveau2);
 
         }
 
         if (GUILayout.Button("Lancer le jeu 3"))
 
         {
 
             //Lancer le jeu 3 si cliquer
 
             Application.LoadLevel(chargerLeNiveau3);
 
         }
 
         if (GUILayout.Button("Lancer le jeu 4"))
 
         {
 
             //Lancer le jeu 4 si cliquer
 
             Application.LoadLevel(chargerLeNiveau4);
 
         }
 
         if (GUILayout.Button("Options"))
 
         {
 
             clicked = "Options";
 
         }
 
         if (GUILayout.Button("A propos"))
 
         {
 
             clicked = "A propos";
 
         }
 
         if (GUILayout.Button("Quitter"))
 
         {
 
             Application.Quit();
 
         }
 
         if (DragWindow)
 
             GUI.DragWindow(new Rect(0, 0, Screen.width, Screen.height));
 
     }
 
         
 
     }
 
 
 
     private void Update()
 
     {
 
         if (clicked == "A propos" && Input.GetKey (KeyCode.Escape))
 
             clicked = "";
 
     }
 
 }
Answer by hathol · May 28, 2012 at 03:22 PM
You are using Input.GetKey() for the check. That will constantly fire as long as the button is held down. What you want to use instead is a combination of Input.GetKeyDown() and a bool to check weather the menu is open
 bool isMenuOpen = false;
 
 void Update()
 {
     if(Input.GetKeyDown(KeyCode.Escape))
     {
         if(!isMenuOpen)
         {
              // open menu
              isMenuOpen = true;
         }
         else
         {
              // close menu
              isMenuOpen = false;
         }
     }
 }
Unlike Input.GetKey() which returns true as long as the button is pressed, GetKeyDown() will return true only exactly once, in the frame the key has been pressed
Your answer
 
 
             Follow this Question
Related Questions
Having trouble with puase menu using standard playercontroller 0 Answers
Pause Menu Text Not Rendering 0 Answers
Pause menu scripting help? 1 Answer
Pause Menu Quality Settings Error ? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                