- Home /
Game Menus WIth Keyboard Input Control
Hi All! I have a race game with various tracks and vehicles and i'm making the menu for it. I need a menu that has the ability to be controlled by the arrow keys of the keyboard. I have a "MainMenu" scene with an empty object with a script that renders the GUI but only the first page. Here is the code:
public class MainMenu : MonoBehaviour {
public AudioClip blip;
public Texture2D background;
public Texture2D wip3Logo;
public GUIStyle mainButton;
private string[] mainMenuLabels = { "SINGLE RACE", "CAMPAIGN", "SPLIT SCREN", "STATS", "QUIT" };
private bool[] mainMenuButtons;
private int mainMenuSelected;
void Awake () {
DontDestroyOnLoad (GameManager.Instance);
GameManager.Instance.StartState ();
}
void Start () {
mainMenuButtons = new bool[mainMenuLabels.Length];
mainMenuSelected = 0;
}
void Update () {
if (Input.GetKeyDown (KeyCode.DownArrow) == true) {
audio.PlayOneShot (blip);
if (mainMenuSelected < mainMenuLabels.Length - 1)
mainMenuSelected += 1;
else
mainMenuSelected = 0;
}
if (Input.GetKeyDown (KeyCode.UpArrow) == true) {
audio.PlayOneShot (blip);
if (mainMenuSelected > 0)
mainMenuSelected -= 1;
else
mainMenuSelected = mainMenuLabels.Length - 1;
}
}
void OnGUI () {
float width = Screen.width / 2;
float height = Screen.height / 2;
// MAIN BACKGROUND
GUI.Box (new Rect (width - 960, height - 600, 1920, 1200), background);
// MAIN MENU
GUI.BeginGroup (new Rect (width - 512, height - 384, 1024, 768));
GUI.Label (new Rect (64, 96, 416, 48), wip3Logo);
for (int i = 0; i < mainMenuLabels.Length; i++) {
GUI.SetNextControlName (mainMenuLabels[i]);
mainMenuButtons[i] = GUI.Button (new Rect (64, 160 + i * 64 , 416, 48), mainMenuLabels[i], mainButton);
}
GUI.EndGroup ();
GUI.FocusControl (mainMenuLabels[mainMenuSelected]);
if (mainMenuButtons[0]) {
Debug.Log ("SINGLE RACE");
GameManager.Instance.SetRaceType (GameManager.RaceType.SingleRace);
}
if (mainMenuButtons[1]) {}
if (mainMenuButtons[2]) {}
if (mainMenuButtons[3]) {}
if (mainMenuButtons[4]) {
Application.Quit ();
}
if (Input.GetKey (KeyCode.Return)) {
mainMenuButtons[mainMenuSelected] = true;
}
}
}
As you can see it manage the first page of the menu. I need a way to program the various pages in the same way with personalized contollers (a page needs even left and right arrows) to build a complex menu. How can i do that?
In other words, how can i build a complex menu controlled by keyboard input?
Answer by zaid87 · Aug 15, 2014 at 03:00 AM
So you already got the first menu working and just looking for ways of doing the same thing for a different menu? If so, I think you can either create a functions and set the codes that you've written into that function instead like "UpdateMainMenu" function and call it from the "Update" function. Then just make a copy of that function for other menus you want and call them instead if you're currently in that menu (you can use a state system to do this (I usually use enum for it)). Same thing with the OnGUI function.
Or, another way is to create another class for the other menus and attach it to a GameObject, then deactivate them at the start. When you want the menu to appear, simply set the GameObject active to true and false when you exit from them.
Hope this helps.
Ok, your first solution is interesting, but as i have many pages it would be complicated to program all in one C# script...So i choose the second. I have only a roblem now: the last part of code
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Return)) {
main$$anonymous$$enuButtons[main$$anonymous$$enuSelected] = true;
}
Don't be called with Return key BUT only with Space key... Why?
Hmm... that's weird, that piece of code works fine in $$anonymous$$e. Are you sure that's the line that is called? Can you try to place a breakpoint on line 87 to see if that's the one that's hit when you press Space?
Your answer
Follow this Question
Related Questions
GUILayout Context Menu on Right Click - Not Working! 1 Answer
Editor Scripting Input Problem OnGUI() 1 Answer
Gui list and color 0 Answers