- Home /
Open gui if player clicks on button
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?
or is there a much easier way?
greetingz, ab_cee
Answer by ab_cee · May 25, 2010 at 12:11 PM
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.
Answer by StephanK · May 25, 2010 at 12:11 PM
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; }
I hope this gives you an idea.
oke thanks spree , i'll try that too. It needs to be a new gui with photo's(2d textures) and maybe video's.
Your answer
Follow this Question
Related Questions
GUI.Button toggling object visibility. 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Reverse GUILayout 1 Answer
For Loop GUILayout.Label Problems 1 Answer