- Home /
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();
}
}
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...
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"))
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.
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")
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.
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.
Your answer
Follow this Question
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