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,
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.
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!