- Home /
Pause Menu controlled by keys
How could I make a GUI that has multiple buttons but you select them with your up and down keys and enter to press them, like console games you know o:
Answer by Kiwasi · Dec 06, 2014 at 10:03 AM
Upgrade to 4.6. Build a pause menu with the new UI tools. And you are done.
The new UI has built in support for this functionality.
Check out the learn section. There are a full set of tutorials on the new UI tools there.
Answer by M-G-Production · Dec 06, 2014 at 07:56 PM
So here it is vultt3rim!
There is plenty of way to do it, but now I'll show you mine! I made it simple with bunch of comments to see what's actually going on! So this C# system will instantly work and maybe teach you some nice things!
BEFORE EVERYTHING: Create 2 C# scripts named "MenuController" and "ButtonScript" and copy/paste the scripts down there!
Create an Empty Game Object and name it: "Menu Controller";
Drag and drop the script named: "MenuController" in the new object you just created;
Tell the MenuController script in Inspector how much you have buttons in your scene in "Buttons Count" field;
Now, you will need to add the script "ButtonScript" to every button you have in your scene;
Drag the textures in the "Button Normal" & "Button Hover" empty fields;
Change the ID's of each individual buttons starting from 1 (Top First = 1, bottom = last one);
It will work perfectly if you take your time doing it well! If you don't like this way, it will be easy to remove. My pleasure!
Math
Here are the scripts!
MenuController.cs
using UnityEngine;
using System.Collections;
public class MenuController : MonoBehaviour
{
/*This script has been made by MGProduction!*/
//THE HOW MANY BUTTONS YOU HAVE ON THE SCENE?
public int buttonsCount;
//THIS IS A VIRTUAL SELECTOR THAT DOES NOTHING ELSE THAN NAVIGATE BETWEEN DIFFERENT VALUES
public int selector = 1;
// Use this for initialization
void Start ()
{
//I am setting the selector to 0
selector = 1;
}
// Update is called once per frame
void Update ()
{
//IF YOUR KEYBOARD IS NOT "AWSD", YOU CAN EASILY CHANGE THIS!
if (Input.GetKeyUp(KeyCode.S))//DOWN
{
//IF MY SELECTOR IS LOWER THAN THE BUTTON COUNT
if (selector < buttonsCount)
{selector += 1;}
//OTHERWISE, RESET IT TO 0!
else
{selector = 1;}
}
//SAME THING BUT INVERTED
if (Input.GetKeyUp(KeyCode.W))//UP
{
if (selector > 1)
{selector -= 1;}
else
{selector = buttonsCount;}
}
}
}
ButtonScript.cs
using UnityEngine;
using System.Collections;
public class ButtonScript : MonoBehaviour {
/*This script has been made by Mathieu from MGProduction!*/
//Textures selection
public Texture2D buttonNormal, buttonHover;
//The ID int is about the action the button is going to do!
//GO SEE THE Selected() function at the bottom
public int ID;
//IT WILL REQUIRES THE MENU CONTROLLER OBJECT
private GameObject menuController;
//WILL REFER TO THE MENUCONTROLLER SCRIPT
private int selector;
//IF THE BUTTON IS SELECTED
private bool selected;
void Start()
{
//I'M SEARCHING THE MENU CONTROLLER BY HIS NAME
menuController = GameObject.Find ("Menu Controller");
//Let's start with the normal texture
gameObject.guiTexture.texture = buttonNormal;
}
// Update is called once per frame
void Update ()
{
//Check it only if it changes! POWER & RAM SAVED TECHNIC
if (selector != menuController.GetComponent<MenuController> ().selector)
{
//Accessing the MenuController script
selector = menuController.GetComponent<MenuController> ().selector;
}
//If the MENUCONTROLLER.SELECTOR = MyID then
if (selector == ID)
{selected = true;}
else
{selected = false;}
//IF YOU ARE SELECTED, RUN FUNCTION Selected()
if (selected)
{Selected();}
else
{
//CHANGING TEXTURE 1 TIME
if (gameObject.guiTexture.texture == buttonHover)
{gameObject.guiTexture.texture = buttonNormal;}
}
}
void OnMouseOver()
{
//Mouse Over dominate the selector!
if (menuController.GetComponent<MenuController> ().selector != ID)
{menuController.GetComponent<MenuController> ().selector = ID;}
}
void Selected()
{
//CHANGING TEXTURE 1 TIME
if (gameObject.guiTexture.texture == buttonNormal)
{gameObject.guiTexture.texture = buttonHover;}
//THIS IS WHERE YOU NEED TO MAKE THE BUTTONS DO SOMETHING!
if (Input.GetKeyUp(KeyCode.Return) || Input.GetMouseButtonUp(0))
{
//THIS IS WHERE YOU WANT TO ACTUALLY DO SOMETHING WITH YOUR BUTTONS!
switch (ID)
{
case 1:
//DO WHAT YOU WANT THE TOP FIRST BUTTON TO DO?
//EXEMPLES:
//Application.LoadLevel("MyAwesomeLevel");
Debug.Log("Button "+ID.ToString()+" is in action");
break;
case 2:
//DO WHAT YOU WANT THE SECOND BUTTON TO DO
Debug.Log("Button "+ID.ToString()+" is in action");
break;
case 3:
//ETC
Debug.Log("Button "+ID.ToString()+" is in action");
break;
case 4:
Debug.Log("Button "+ID.ToString()+" is in action");
break;
case 5:
//IF EXIT BUTTON:
//Application.Quit();
Debug.Log("Button "+ID.ToString()+" is in action");
break;
//CLEAN UP THIS SWITCH WITH WHAT YOU NEED! LESS OR MORE?
//IF YOU HAVE ONLY
}
}
}
}
While technically correct, this is a lot of work, and should only be done if your current project is version constrained to an older version if Unity. From 4.6 this is all built in.
I made an object and added the controller and the button scripts to it and it doesnt work, am I doing it wrong?
Your answer

Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Pause Menu won't open... 1 Answer
Key for GUI.Button 2 Answers
Assign a hotkey to a GUI button? 3 Answers
How can i show the texture in the array? 2 Answers