Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by croco193 · May 28, 2012 at 02:34 PM · menubooleanpauseescape

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 = "";
 
     }
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

Can you make a pause menu have mobile input? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges