Having trouble conceptualizing flow of menus/GUI

Like the title says, I’m trying to set up my main menus right now (I’m using EZGui). However, I’m having trouble with the basic logic behind it.

In XNA I set up a game manager class and attach numbers to each of the menus or game modes (essentially making an array). I do this for draw and update individually (initializing is front-loaded).

With Unity, I don’t feel like I have the same ability to do this since scripts are all attached to individual items.

Can someone just help me out with how I link together menus and my game? I don’t need code if you don’t want to write it, theory should do just fine.

I’m not quite sure what you mean. You can link your GUI to your game simply by, in your GUI, changing variables and calling functions that do things in the game.

Yeah, sorry. It’s hard for me to explain kind of.

That’s not necessarily the issue. In game GUI is easy. I’m just having trouble with main menus.

Like I haven’t found any scripts or tutorials that show how to link one menu to another or what happens when I hit start game. I probably need to do some kind of load script to place all the game objects right? I can’t do this on a global level like I could with XNA. I have to do it individually on a per button basis, since that’s how scripts function. I honestly just can’t think of the logic right now that will get me from Main Menu button 1 to Sub Menu button 1 or from Main Menu button 2 (Play) to the game.

Application.LoadLevel() is probably a good start.

lol wow. I had no idea that existed… Thanks. That actually does help a lot.

I’ve never used EZ GUI… but with the built-in OnGUI functions and methods, to do a main->secondary menu, a very basic example would be something like:

private var showMainMenu : boolean = true;
private var showSubMenu : boolean = false;

function OnGUI(){

	if(showMainMenu == true){
		if(GUI.Button(Rect((Screen.width/2 - 75), (Screen.height/2), 150, 50), "Show Submenu")){
			EnableSubMenu();
		}
		GUI.Button(Rect((Screen.width/2 - 75), (Screen.height/2) + 75, 150, 50), "Pointless Button 1");
		GUI.Button(Rect((Screen.width/2 - 75), (Screen.height/2) + 150, 150, 50), "Pointless Button 2");
		GUI.Button(Rect((Screen.width/2 - 75), (Screen.height/2) + 225, 150, 50), "Pointless Button 3");
	}
	
	if(showSubMenu == true){
		GUI.Button(Rect((Screen.width/2 - 75), (Screen.height/2), 150, 50), "Pointless Button 4");
		GUI.Button(Rect((Screen.width/2 - 75), (Screen.height/2) + 75, 150, 50), "Pointless Button 5");
		if(GUI.Button(Rect((Screen.width/2 - 75), (Screen.height/2) + 150, 150, 50), "Return to Main Menu")){
			EnableMainMenu();
		}
	}
	
}

function EnableSubMenu(){
	showMainMenu = false;
	showSubMenu = true;
}

function EnableMainMenu(){
	showMainMenu = true;
	showSubMenu = false;
}

Note: written off the cuff, I didn’t test it.

Hello there!

My approach to constructing GUI’s usually involves an enum and a bunch of functions.

enum MenuState
{
    Main,
    Options,
    ReallyCoolSubMenu
}

public class MainMenu : MonoBehaviour
{
    MenuState mState = MenuState.Main;
    void OnGUI()
    {
        switch(mState)
        {
            case MenuState.Main:
                GUIMain();
                break;

            case MenuState.Options:
                GUIOptions();
                break;

            case MenuState.ReallyCoolSubMenu:
                GUICoolMenu()
                break;
        }
    }

    void GUIMain()
    {
        if(GUI.Button(new Rect(0, 0, Screen.Width, Screen.Height / 3), "Options")
            mState = MenuState.Options;
    }

    void GUIOptions()
    {
        if(GUI.Button(new Rect(0, Screen.Height - (Screen.Height / 3), Screen.Width, Screen.Height / 3), "Back")
            mState = MenuState.Main;
    }
}

And that’s the general structure. Note that you should also store all the rectangles you use, as opposed to creating new ones every call.

I actually use the same approach as Myx, I just wanted to show you a basic example with booleans before bringing in the state machines :slight_smile: But the point is, you enable/disable blocks of GUI code with boolean or state switches.

Thanks for the help everyone! I was able to make a successful UI with all your input =).