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 Dipetriantonio · Nov 03, 2013 at 07:35 PM · androidpausegetkeyup

Android development. How to have two meanings of the same key?

Hello,

I'm making an Android game where I want to open a pause menu when the back-key is pressed. Then if you hit it again when the menu is opened, the menu should close. I have this code in a single script:

 using UnityEngine;
 using System.Collections;
 
 public class PauseMenuScript : MonoBehaviour {
     private int GROUP_INSET_HORI;
     private int GROUP_INSET_VERT;
     private int GROUP_SIZE_HORI;
     private int GROUP_SIZE_VERT;
     
     private int BUTTON_INSET_HORI;
     private int BUTTON_INSET_VERT;
     private int BUTTON_SIZE_HORI;
     private int BUTTON_SIZE_VERT;
     
     public bool paused;
     public Texture buttonTexture;
     
     public GUIStyle myStyle;
     
     private Rect buttonRectUnity;
     
     // Use this for initialization
     void Start () {
         GROUP_INSET_HORI = Screen.width/8;
         GROUP_INSET_VERT = Screen.height/8;
         GROUP_SIZE_HORI = GROUP_INSET_HORI*6;
         GROUP_SIZE_VERT = GROUP_INSET_VERT*6;
 
         
         BUTTON_INSET_HORI = Screen.width/10;
         BUTTON_INSET_VERT = Screen.height/10;
         BUTTON_SIZE_HORI = GROUP_SIZE_HORI-2*BUTTON_INSET_HORI;
         BUTTON_SIZE_VERT = (int)(BUTTON_INSET_VERT*1.7);
         
         buttonRectUnity = new Rect(BUTTON_INSET_HORI,BUTTON_INSET_VERT,BUTTON_SIZE_HORI,BUTTON_SIZE_VERT);
 
         paused = false;
         Time.timeScale = 1; 
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     
     void OnGUI(){
         if(paused){
             
             GUI.BeginGroup(new Rect(Screen.width/8,Screen.height/8,(Screen.width/8)*6,(Screen.height/8)*6));
                 if(GUI.Button (new Rect(BUTTON_INSET_HORI,BUTTON_INSET_VERT,BUTTON_SIZE_HORI,BUTTON_SIZE_VERT),"Continue Game", myStyle)){
                     paused = false;
                     Time.timeScale = 1;
                     GameEventManager.TriggerGameUnpaused();
                 }
             
                 if(GUI.Button (new Rect(BUTTON_INSET_HORI,3*BUTTON_INSET_VERT,BUTTON_SIZE_HORI,BUTTON_SIZE_VERT),"Restart Game", myStyle)){
                     paused = false;
                     Time.timeScale = 1;    
                     GameEventManager.TriggerGameStart();
                 }
             
                 if(GUI.Button (new Rect(BUTTON_INSET_HORI,5*BUTTON_INSET_VERT,BUTTON_SIZE_HORI,BUTTON_SIZE_VERT),"Quit Game", myStyle)){
                     Application.Quit();
                 }
                 if(Input.GetKeyUp (KeyCode.Escape)){
                     paused = false;
                     Time.timeScale = 1;
                     GameEventManager.TriggerGameUnpaused();
                         
                 }    
                 
                 
             GUI.EndGroup();
             
         }
         else{
             if(Input.GetKeyUp (KeyCode.Escape)){
             SetPaused ();
                 
             }
         }
 
     }
     
     public void SetPaused(){
         paused = false;
         StartCoroutine(PauseFunction(2));
         
         paused = true;
         Time.timeScale = 0;
         
         GameEventManager.TriggerGamePaused();
     }
     
     public IEnumerator PauseFunction(float pa){
         yield return new WaitForSeconds(pa);
         
     }
 }

My problem is that the escape key triggers both if-statements, thereby opening the menu and imidiately closing it again. How can I make sure that one press on the back-button on the phone(or when testing on the computer on press on the escape-key) only counts once? Since I'm using OnKeyUp it should only be read once I think... I tried solving the problem by making a delay using WaitForSeconds, thereby possibly clearing the input-stream, but this did not work. It did not even pause the script...

I would be very grateful to anyone that can help me with this!

Thank you

[Edit]Made a correction to the code [Edit]I now tried to have just on GetKeyUp statement in my code and then check if the game is paused or not but that does not work either. I have added the new OnGUI-function under this:

 void OnGUI(){
         if(paused){
             GUI.BeginGroup(new Rect(Screen.width/8,Screen.height/8,(Screen.width/8)*6,(Screen.height/8)*6));
                 if(GUI.Button (new Rect(BUTTON_INSET_HORI,BUTTON_INSET_VERT,BUTTON_SIZE_HORI,BUTTON_SIZE_VERT),"Continue Game", myStyle)){
                     paused = false;
                     Time.timeScale = 1;
                     GameEventManager.TriggerGameUnpaused();
                 }
             
                 if(GUI.Button (new Rect(BUTTON_INSET_HORI,3*BUTTON_INSET_VERT,BUTTON_SIZE_HORI,BUTTON_SIZE_VERT),"Restart Game", myStyle)){
                     paused = false;
                     Time.timeScale = 1;    
                     GameEventManager.TriggerGameStart();
                 }
             
                 if(GUI.Button (new Rect(BUTTON_INSET_HORI,5*BUTTON_INSET_VERT,BUTTON_SIZE_HORI,BUTTON_SIZE_VERT),"Quit Game", myStyle)){
                     Application.Quit();
                 }
                      
          }
         if(Input.GetKeyUp (KeyCode.Escape)){
                 
             if(paused){
                 paused = !paused;
                 Time.timeScale = 1;
                 GameEventManager.TriggerGameUnpaused();
             }
             else{
                 SetPaused ();
             }
         }
     }

The SetPaused-function is still the same.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Zaeran · Nov 03, 2013 at 08:07 PM

First of all, CoRoutines are run in parallel with the program. This means that it'll be waiting for 2 seconds ONLY in the coroutine, but nowhere else in the program. To do what you're trying to do there, you'll need to change SetPause to a CoRoutine, then change your CoRoutine call to the WaitForSeconds statement.

Anyway, OnGUI runs at least twice per frame, so your button statement is firing twice. You shouldn't put anything except GUI code into OnGUI, and move your input code into Update().

Basically, delete all of your input code from OnGUI(), and put this into Update():

 if(Input.GetKeyUp(KeyCode.Escape)){
     paused = !paused;
     if(paused){
         GameEventManager.TriggerGamePaused();
     }
     else{
         GameEventManager.TriggerGameUnPaused();
     }
 }

This will toggle pause whenever you hit escape, and your buttons will still appear.

Comment
Add comment · Show 2 · 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
avatar image Dipetriantonio · Nov 03, 2013 at 08:13 PM 0
Share

Thanks! That worked a treat! I'm new to coroutines but I will look in to it.

avatar image Zaeran · Nov 03, 2013 at 08:44 PM 0
Share

No probs. Coroutines can be a bit annoying at first. Just treat each one like a separate program that runs alongside yours and has access to your variables.

avatar image
0

Answer by LukaKotar · Nov 03, 2013 at 07:41 PM

You could simply set paused to inverted paused:

if(Input.GetKeyUp (KeyCode.Escape)){ paused = !paused; }

If paused is true, it will set it to false. If paused is false, it will set it to true.

Comment
Add comment · Show 1 · 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
avatar image Dipetriantonio · Nov 03, 2013 at 08:08 PM 0
Share

Sorry but that does not make it work. The problem seems to be that one button press is counted or registred twice...

avatar image
0

Answer by tanoshimi · Nov 03, 2013 at 07:43 PM

How about changing:

 if(Input.GetKeyUp (KeyCode.Escape)){
 SetPaused ();
 }

to:

 else if(Input.GetKeyUp (KeyCode.Escape)){
   SetPaused ();
 }

?

Comment
Add comment · Show 1 · 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
avatar image Dipetriantonio · Nov 03, 2013 at 07:53 PM 0
Share

Thanks, but I accidentally input the code wrong when I wrote the question. The way you suggested it is the way I have in my code so that alone does not solve my problem. Thanks anyway and I have updated the question.

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Prevent Android pause when an overlay is active 1 Answer

Digital Hardware Key - Android 1 Answer

I use Handheld.PlayFullScreenMovie to play movies on android,but,how to pause or end it? 1 Answer

Differentiate App closed and minimized 0 Answers

pause menu on android phone 1 Answer


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