Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 MicDaRoc · Jul 20, 2015 at 01:28 PM · guimenukeyboardgui-button

Controlling Pause Menu with Keyboard

Hi!

I'm still working on my Top-Down-Shooter and implemented a Pause-Menu so far. The Pause Menu consists of 3 Buttons; "Resume Game", "Exit to Main Menu" and "Exit Game". Clicking on them works fine, each button is doing exactly what it should. Now I want the buttons to be also controllable by Keyboard, but I'm having huge problems with this. I know or I think it should work with GUI.FocusControl, but I have no clue how to use it in my code. I don't know how to access my already existing buttons... Could someone be so kind and give me a hint please?

Thanks in advance!

Here is my Pause-Menu-script:

 using UnityEngine;
 using System.Collections;
 
 public class PauseMenu : MonoBehaviour {
 
     public static bool IsPaused;
     public GameObject pauseMenuCanvas;
 
 
     void Start () {    
         
         pauseMenuCanvas.SetActive(false);
     }
 
 
     void Update(){
 
         if (Input.GetKeyDown (KeyCode.Escape)) 
         {
             if(!IsPaused)
             {
                 Time.timeScale=0;    
                 IsPaused = true;
                                 
             }
             else
             {
                 Time.timeScale=1;
                 IsPaused = false;
                 
             }
         }
 
         if (IsPaused) {
             pauseMenuCanvas.SetActive(true);
 
         } else {
             pauseMenuCanvas.SetActive (false);
         }
         timeRemaining -= Time.deltaTime;
 
     }
 
 
     public void Resume()
         {
 
         if(!IsPaused)
                 {
             Time.timeScale=0;    
             IsPaused = true;
         }
         else
         {
             Time.timeScale=1;
             IsPaused = false;
                         
         }
     }
     
     public void ExitGame(){
         IsPaused = false;
         Application.Quit();
 
     }
 
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Veldars · Jul 20, 2015 at 02:13 PM

Hi,

To Select a Button in script you have simply to use the function "Select".

This function is attached to all selectable object like button / inputfield... (http://docs.unity3d.com/ScriptReference/UI.Selectable.html)

For me the easiest solution is to create a list of your button in a Controller Script. When you press "escape" put the game in pause then

 list[current].Select();

Then increase or decrease current when pressing "Up" and "Down" arrow

In your button you can add a little script like

 private bool isActive = false;
 
 void OnSelect(BaseEventData eventData) {
     isActive = true;
 }
 
 void Update() {
    if (Input.GetKeyDown (KeyCode.Return))
        LaunchYourFunction();
 }

I hope this help...

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
avatar image
0

Answer by RayJr · Jul 20, 2015 at 01:32 PM

You need to go to Edit - > project Settings -> input

from there you can configure what commands your keys send. Then you can catch those commands in your input statement.

So name the spacebar "KeyboardPause" and then in your code put if(Input.GetButton ("KeyboardPause"))

Comment
Add comment · Show 5 · 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 MicDaRoc · Jul 20, 2015 at 01:36 PM 0
Share

Thanks for your reply. I already can pause the game with the keyboard, but I can't select the different buttons let's say withe the arrow keys. The buttons are only accessible via mouse, but I need the whole menu to be controllable by keyboard.

avatar image RayJr · Jul 20, 2015 at 01:41 PM 0
Share

In the input menu you need to set up the other buttons. Whatever you want the Resume button to be for example "R". You need to set up R in input menu to issue command "Resume" or something like that. Then in the code catch that command with Get Button..

 if(Input.GetButton("Resume"))
     Resume();

Collecting the arrow keys is different. You can get those with these commands:

 Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")

avatar image RayJr · Jul 20, 2015 at 01:44 PM 0
Share

So in Input menu, name the entry "Resume" and set the positive input button to R (or whatever you want). The rest of the settings can be left as default.

avatar image MicDaRoc · Jul 20, 2015 at 01:55 PM 0
Share

Ok, thanks again. But I think I'm not being clear enough. Sorry for that. I want to pause the game by pressing Escape (works already) and be able to choose (or highlight) the 3 different Buttons with the up and down arrow keys. After highlighting one of the buttons I want to press Enter, which starts the function behind the button. I don't know, but I think there is some code needed, for example for the button which is automatically first highlighted after I pause the game.

avatar image RayJr · Jul 20, 2015 at 02:11 PM 0
Share

Oh, gotcha now. sorry.

I think this answer might help you.

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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Look like a Website Menue! 1 Answer

Close my GUI button by repressing the same Hot-key. 3 Answers

Navigating a gui with the keyboard 2 Answers

Pause Menu - Loading & Quitting 1 Answer

Assign keystrokes to GUI button 2 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