- Home /
What is the best way to define different GUI pages?
How can I link between 2 or more pages of GUIs? I'm trying to make different javascript files each containing a different GUI and I try to enable one and disable others to link between them, but that just doesn't feel right? is there any better way? thanks,
Answer by Jesse Anders · Oct 05, 2010 at 05:02 PM
Maybe someone will offer a better answer, but I don't know that there's any one 'right' answer to the question. Really, I think it's just a question of what best meets your needs and seems sufficiently practical/elegant/etc.
Assuming it works as you expect, enabling and disabling as needed doesn't seem inappropriate to me. Other options/variations that spring to mind are:
Make the individual menu objects children of a 'menu manager' object that handles switching between them.
Use a static member variable to keep track of which menu is currently active.
Put all the menus in one script, put each individual menu in its own function, and then call the appropriate function from OnGUI().
I can't remember what sort of menus the various tutorial projects include, but if they do have menus, you might check there to see how they handle things.
Is there any tutorial you recommend? cause now I'm stuck among many variables each for fading in and out each menu and its totally confusing. I mean is there any way that these GUIs could simply link to each other like a website?
I don't know of any tutorials that cover specifically what you're describing (although there may be some). As for 'linking' the pages in some sort of automatic way, there's no built-in Unity feature that will do that directly. But, it shouldn't be a problem, I$$anonymous$$O; many if not most games implement multi-page menus in one form or other, so obviously it's not an intractable problem. Really, it's just a software design issue. (If you set things up appropriately, each menu should only need a couple of variables for fading, which isn't too bad.)
Answer by ManickYoj · Aug 28, 2014 at 04:11 AM
I have an add-on to Jesse Anders' comment about making the individual menu objects children of a menu manager. I implemented a version of that and have included the code below.
The basic gist is that you have a manager class with a reference to your current menu. The manager class then calls an update method on the current menu, and that current menu's update method returns the current menu if you have not navigated to a new menu, and the new menu if you have.
In this example, I have a main menu with a "SubMenu" Button that loads up a Sub-Menu. The Sub-Menu has a back button which returns you to the previous menu.
using UnityEngine;
public class MenuManager : MonoBehaviour {
private IMenu menu = new MainMenu();
void OnGUI () { menu = menu.update (); }
}
public interface IMenu { IMenu update (); }
public class MainMenu : IMenu {
public IMenu update() {
if (GUI.Button(new Rect(100, 100, 250, 100), "SubMenu")) return new SubMenu(this);
return this;
}
}
public class SubMenu : IMenu {
private IMenu prevMenu;
public SubMenu (IMenu prevMenu) { this.prevMenu = prevMenu; }
public IMenu update() {
if (GUI.Button(new Rect(100, 100, 250, 100), "Back")) return prevMenu;
return this;
}
}
Hope this helps!
Your answer
