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:

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.

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!

  1. Create an Empty Game Object and name it: “Menu Controller”;
  2. Drag and drop the script named: “MenuController” in the new object you just created;
  3. Tell the MenuController script in Inspector how much you have buttons in your scene in “Buttons Count” field;
  4. Now, you will need to add the script “ButtonScript” to every button you have in your scene;
  5. Drag the textures in the “Button Normal” & “Button Hover” empty fields;
  6. 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 
			}
		}
	}
}