Best way to code a large menu

Hi Everyone,

I have been drawing up a prototype menu system for a sports management game that’s been flying about in design recently. Being a management game, it has an extensive menu system.

I started coding the menu with confidence, here’s a snippet of how I’m currently doing it:

//Menu Layer Vars
var mainmenu : boolean = true;
var teammanage : boolean;
var noticeboard : boolean;
var teamaffairs : boolean;
var playmanagement : boolean;
var market : boolean;
var options : boolean;
 
function OnGUI()
{
        GUILayout.BeginArea(Rect(Screen.Width/2 -buttonWidth/2,Screen.Height/2 - buttonHeight/2, buttonWidth, buttonHeight)"Scrott-Balls"); // make all the menus centered.
 
        if(mainmenu) // main menu stuff goes here
        {
                if(GUILayout.Button("Team Management", GUILayout.Height(buttonHeight)))
                {
                        teammanage = true;
                }
                if(GUILayout.Button("Options"),GUILayout.Height(buttonHeight)))
                {
                        options = true;
                        teammanage = false;
                }
        }
 
 
        if(teammanage) // begin team management menu
        {
                if(GUILayout.Button("Notiss Boord", GUILayout.Height(buttonHeight)))
                        {
                                noticeboard = true;
                                teammanage = false;

So as you can see, when a buttons pressed i just turn one variable off and another on.

This has worked in the past for me, but on smaller menus. I would guesstimate at around 100+ elements to this menu system (that includes back buttons and “are you sure’s”.

Now obviously with the way I’m doing it this file is gonna be HUGE and navigating / debugging the code is going to be an absolute nightmare!

I’ve considered giving each sub-menu its own code file and enabling / disabling them… but that could turn out just as messy in the end, and i don’t want to waste more time finding out the hard way.

Has anyone got any advice or ideas on the best way to (tidily) make such a hugeumungus menu?

Thanks for reading :).

2 Answers

2

Being developing on our own mapeditor for our current project/game, I’ve been around a few menusystems now too.

My experience has taught me this:

  1. paper-prototype your screens/dialogs first, to get an overview of the work ahead + make the reusable stuff more visible before you start coding.

  2. simulating normal windows/desktop interfaces with panels etc. is a nice way to group things. building helper functions for these high-level GUI’s makes the actual GUI easier to develop and maintain.

  3. using clever switch…case structures with state variables for control or using delegates is the way to keep the actual parsing simple.

  4. every command/function activated from GUI, should call a sub function that does the actual setting, so that the GUI part is a simple front-end and the actual coding happens outside the GUI.

thanks a lot for your answer :). 1: We have done this, have you seen freemind? great program for prototyping menus early on. 2: I understand what you mean, we are going for a slightly different approach tho. 3: Switch case structures definitely look like a way forward, will have to look into these as i have never come across them before. 4: Are you saying that instead of putting the functionality for the buttons inside the OnGUI, you suggest calling another function from OnGUI? So OnGUI simply draws the GUI and calls the functions, the rest is done inside the said functions?

4: YUP! Look for n-tier development patterns/architecture

awesome, will crack on with this then. Thanks a lot for your help Berggreen.

Just want to make sure im going about this the right way (and help anyone who stumbles on this question). Im declaring my switch statement in function OnGUI like this: switch(menulayer) { case "mainmenu" //draw GUI function and button code, button code changes the case and calls functions from buttonfunctionsclass. break; } im defining the GUI drawing in a helper function and all the actual functionality in a seperate class. Edit: Cant find a way to format code in comments.

@timsk <.pre> <pre> CODE </pre> <./pre> //without dots

I would look at creating a helper method to instantiate a Plane object whenever you need a button on your screen. Make sure you don’t instantiate Unity’s plane primitive, as this mesh contains 200 triangles, and many of these can hurt performance. Create a simple plane in a 3d modelling tool that contains two triangles, and import that mesh into your project.

In your helper method create a gameobject using your new plane mesh, then add a 3D Text component to it. Add in a Texture2D as an argument for a background for the plane mesh. You can also hook in some code to detect if your plane GameObject was clicked on, and fire an event from that. Then all you need to do is add in some arguments for positioning and you should have a nice simple helper method for instantiating some planes that have text, a background and some basic positioning.

I'm finding it hard to understand how manually instantiating a plane and the text, plus the events for the buttons would be a better idea than using unityGUI? I also feel this adds to my problem of having such a large menu?

My reasoning for this is the performance of OnGUI is horrible and having large amounts of code in there for a large menu can hurt performance horribly. Planes and mesh data will scale much better in my opinion. Unity 3.5 is coming out with an updated menu system. Perhaps you can read up on those improvements and wait for that.

*menu system = GUI system

just read up on 3.5, does sound great, however i dont want to have to wait an (as of now) indefinite amount of time for an update. It would be good to learn how this should be done, if only as a learning curve for myself. Is there any advice you can give with regards to the current unityGUI system? Or are you saying large menus are un-realistic until 3.5, and i should use the method in your inital answer?