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*);*
mainMenuButtons = GUI.Button (new Rect (64, 160 + i * 64 , 416, 48), mainMenuLabels*, 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?