i want to open another gui if the player clicks a gui button.
so .. if the player clicks on my third button it should open/enable a new gui window
this is my code so far
function OnGUI () {
// Make a background box
GUI.Box (Rect (10,10,100,120), "Menu");
// Button 1: Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
if (GUI.Button (Rect (20,40,80,20), "Restart")) {
Application.LoadLevel ("Level 1");
}
// Button 2: Make the second button.
if (GUI.Button (Rect (20,70,80,20), "Main Menu")) {
Application.LoadLevel ("Level 2");
}
//Button 3: make third button here should be the button to make a new gui???
if (GUI.Button (Rect (20,100,80,20), "Info")) {
}
}
should a make a new script with the new gui i want to activatie, and activatie/enable it if player clicks on button 3 somehow?
Just got the code from nicholas on the unity forums:
Code:
var showMoreGui = false;
function OnGUI () {
if (GUI.Button (Rect (10,10,100,20), "Show More"))
showMoreGui = true;
if (showMoreGui) {
GUI.Button (Rect (10, 40,100,20), "Hello there");
if (GUI.Button (Rect (10, 70,100,20), "Close"))
showMoreGui = false;
}
}
To seperate it out a bit, you could do:
Code:
var showMoreGui = false;
function OnGUI () {
if (GUI.Button (Rect (10,10,100,20), "Show More"))
showMoreGui = true;
if (showMoreGui)
ExtraGUI ();
}
function ExtraGUI () {
GUI.Button (Rect (10, 40,100,20), "Hello there");
if (GUI.Button (Rect (10, 70,100,20), "Close"))
showMoreGui = false;
}
Whether you want to split out this more GUI or do it like this generally depends on the complexity. I usually start with just having it inline, then move it to a separate function before completely splitting it out.
Depending on the complexity of your GUI it might be a good idea to have several GUI scripts, that get switched. However what I tend to do on non-complex GUI's is this:
int menuNumber = 0;
void OnGUI()
{
switch(menuNumber)
{
case 0:
DisplayMainMenu();
break;
case 1:
DisplayOtherMenu();
break;
default:
break;
}
void DisplayMainMenu()
{
if (GUI.Button(rect, "Change Menu"))
menuNumber = 1;
}
void DisplayOtherMenu()
{
if (GUI.Button(rect, "Change Menu"))
menuNumber = 0;
}