- Home /
How do i make a menu close if i open another menu
What im trying to do is i have a list of diffrent buttons that open diffrent things,I want it so i click one of the button it shows what it need too but if i click on another button it closes the first thing and open the other. I don't care if you use c# or JS i can translate to what i need :)
EDIT Ok because it was recomended that i made some artwork here are some, for the project it is in danish, since this was made for me and it is only going to be a student project
so when you click on "Solcelle1" it opens the window to the right and i want it so, that if you click any of the other buttons like "Solcelle2" it replaces Solcelle1 window in its place, atm they just stack on each other
EDIT2 Ok ill just post my code now, but warning it is not optimised and is relly just horribel to look at ATM i feel, here we go: using UnityEngine; using System.Collections; public class GUI_Script : MonoBehaviour { float MyBoxLeft = -600f; public Texture icon; public Texture icon2; public Texture Sol_celle1; public Texture Sol_celle2; public Texture Sol_celle3; public Texture Sol_celle4; public Texture Vind_mølle1; public Texture Vind_mølle2; public Texture Vind_mølle3; public Texture Vind_mølle4; public GUIStyle SlideButton; public GUIStyle KatologLabel; public GUIStyle UdendørsLabels; public GUIStyle købsmenuTekst; public GUIStyle KøbsmenuBilleder; public GUIStyle brødtekst; private bool udendørs = false; private bool solcelle1 = false; private bool solcelle2 = false; private bool solcelle3 = false; private bool solcelle4 = false; private bool vindmølle1 = false; private bool vindmølle2 = false; private bool vindmølle3 = false; private bool vindmølle4 = false; void OnGUI() { Rect tBoxRect = new Rect(MyBoxLeft,0.0f,620, 740); Rect tButtonRect = new Rect(MyBoxLeft+600,320.0f,50,100); Rect tBoxArt = new Rect(MyBoxLeft+50,170.0f,200,550); //KatalogBox GUI.Box(tBoxRect, icon); //LABEls //Katalog GUI.Label(new Rect(MyBoxLeft+200, 25, 300, 100), "Katalog",KatologLabel); //BUTTONS! //Slide ud Button if (GUI.Button(new Rect(MyBoxLeft+620,350.0f,30,70), "",SlideButton)) { StartCoroutine(AnimateBox()); } //Udendørs button if (GUI.Button(new Rect(MyBoxLeft + 80, 100, 150, 70), "Udendørs")) { udendørs = udendørs ? false : true; } if (udendørs) { //udendørsbox GUI.Box(tBoxArt, icon2); //Solceller GUI.Label(new Rect(MyBoxLeft + 90, 170, 250, 70), "Solceller", UdendørsLabels); //Vindmøller GUI.Label(new Rect(MyBoxLeft + 80, 400, 250, 70), "Vindmøller", UdendørsLabels); //SolCelleButtons //SolCelle1 button if (GUI.Button(new Rect(MyBoxLeft + 80, 230, 150, 30), "SolCelle1")) { solcelle1 = solcelle1 ? false : true; } //SolCelle2 if (GUI.Button(new Rect(MyBoxLeft + 80, 270, 150, 30), "SolCelle2")) { solcelle2 = solcelle2 ? false : true; } //SolCelle3 if (GUI.Button(new Rect(MyBoxLeft + 80, 310, 150, 30), "SolCelle3")) { solcelle3 = solcelle3 ? false : true; } //SolCelle4 if (GUI.Button(new Rect(MyBoxLeft + 80, 350, 150, 30), "SolCelle4")) { solcelle4 = solcelle4 ? false : true; } //VindmølleButtons //Vindmølle1 button if (GUI.Button(new Rect(MyBoxLeft + 80, 470, 150, 30), "Vindmølle1")) { vindmølle1 = vindmølle1 ? false : true; } //SolCelle2 if (GUI.Button(new Rect(MyBoxLeft + 80, 510, 150, 30), "Vindmølle2")) { vindmølle2 = vindmølle2 ? false : true; } //SolCelle3 if (GUI.Button(new Rect(MyBoxLeft + 80, 550, 150, 30), "Vindmølle3")) { vindmølle3 = vindmølle3 ? false : true; } //SolCelle4 if (GUI.Button(new Rect(MyBoxLeft + 80, 590, 150, 30), "Vindmølle4")) { vindmølle4 = vindmølle4 ? false : true; } } //Buttons for forskellige udendørs ting WARNING LOT OF CODE! //Solcelle1 if (solcelle1) { //BUTTON if (GUI.Button(new Rect(MyBoxLeft + 320, 600, 150, 70), "Bestil")) { Debug.Log("Clicked the button with an image"); } //BOXES GUI.Box(new Rect(MyBoxLeft + 250, 170, 300, 550), ""); GUI.Box(new Rect(MyBoxLeft + 300, 250, 200, 150), Sol_celle1, KøbsmenuBilleder); //LABELS GUI.Label(new Rect(MyBoxLeft + 340, 200, 300, 100), "Solcelle1", købsmenuTekst); GUI.Box(new Rect(MyBoxLeft + 250, 400, 300, 100), "blahblahblah jeg er en masse tekst\n blah blah blah\n blah blah blah blahb\n lahb alhbasdohd \n JEG KAN GODT LIDE KAGE!!!!!", brødtekst); } //Solcelle2 if (solcelle2) { //BUTTON if (GUI.Button(new Rect(MyBoxLeft + 320, 600, 150, 70), "Bestil")) { Debug.Log("Clicked the button with an image"); } //BOXES GUI.Box(new Rect(MyBoxLeft + 250, 170, 300, 550), ""); GUI.Box(new Rect(MyBoxLeft + 300, 250, 200, 150), Sol_celle1, KøbsmenuBilleder); //LABELS GUI.Label(new Rect(MyBoxLeft + 340, 200, 300, 100), "Solcelle2", købsmenuTekst); GUI.Box(new Rect(MyBoxLeft + 250, 400, 300, 100), "blahblahblah jeg er en masse tekst\n blah blah blah\n blah blah blah blahb\n lahb alhbasdohd \n JEG KAN GODT LIDE KAGE!!!!!", brødtekst); } //Solcelle3 if (solcelle3) { //BUTTON if (GUI.Button(new Rect(MyBoxLeft + 320, 600, 150, 70), "Bestil")) { Debug.Log("Clicked the button with an image"); } //BOXES GUI.Box(new Rect(MyBoxLeft + 250, 170, 300, 550), ""); GUI.Box(new Rect(MyBoxLeft + 300, 250, 200, 150), Sol_celle1, KøbsmenuBilleder); //LABELS GUI.Label(new Rect(MyBoxLeft + 340, 200, 300, 100), "Solcelle3", købsmenuTekst); GUI.Box(new Rect(MyBoxLeft + 250, 400, 300, 100), "blahblahblah jeg er en masse tekst\n blah blah blah\n blah blah blah blahb\n lahb alhbasdohd \n JEG KAN GODT LIDE KAGE!!!!!", brødtekst); } //Solcelle4 if (solcelle4) { //BUTTON if (GUI.Button(new Rect(MyBoxLeft + 320, 600, 150, 70), "Bestil")) { Debug.Log("Clicked the button with an image"); } //BOXES GUI.Box(new Rect(MyBoxLeft + 250, 170, 300, 550), ""); GUI.Box(new Rect(MyBoxLeft + 300, 250, 200, 150), Sol_celle1, KøbsmenuBilleder); //LABELS GUI.Label(new Rect(MyBoxLeft + 340, 200, 300, 100), "Solcelle4", købsmenuTekst); GUI.Box(new Rect(MyBoxLeft + 250, 400, 300, 100), "blahblahblah jeg er en masse tekst\n blah blah blah\n blah blah blah blahb\n lahb alhbasdohd \n JEG KAN GODT LIDE KAGE!!!!!", brødtekst); } //Vindmølle1 if (vindmølle1) { //BUTTON if (GUI.Button(new Rect(MyBoxLeft + 320, 600, 150, 70), "Bestil")) { Debug.Log("Clicked the button with an image"); } //BOXES GUI.Box(new Rect(MyBoxLeft + 250, 170, 300, 550), ""); GUI.Box(new Rect(MyBoxLeft + 300, 250, 200, 150), Sol_celle1, KøbsmenuBilleder); //LABELS GUI.Label(new Rect(MyBoxLeft + 340, 200, 300, 100), "Vindmølle1", købsmenuTekst); GUI.Box(new Rect(MyBoxLeft + 250, 400, 300, 100), "blahblahblah jeg er en masse tekst\n blah blah blah\n blah blah blah blahb\n lahb alhbasdohd \n JEG KAN GODT LIDE KAGE!!!!!", brødtekst); } //Vindmølle2 if (vindmølle2) { //BUTTON if (GUI.Button(new Rect(MyBoxLeft + 320, 600, 150, 70), "Bestil")) { Debug.Log("Clicked the button with an image"); } //BOXES GUI.Box(new Rect(MyBoxLeft + 250, 170, 300, 550), ""); GUI.Box(new Rect(MyBoxLeft + 300, 250, 200, 150), Sol_celle1, KøbsmenuBilleder); //LABELS GUI.Label(new Rect(MyBoxLeft + 340, 200, 300, 100), "Vindmølle2", købsmenuTekst); GUI.Box(new Rect(MyBoxLeft + 250, 400, 300, 100), "blahblahblah jeg er en masse tekst\n blah blah blah\n blah blah blah blahb\n lahb alhbasdohd \n JEG KAN GODT LIDE KAGE!!!!!", brødtekst); } //Vindmølle3 if (vindmølle3) { //BUTTON if (GUI.Button(new Rect(MyBoxLeft + 320, 600, 150, 70), "Bestil")) { Debug.Log("Clicked the button with an image"); } //BOXES GUI.Box(new Rect(MyBoxLeft + 250, 170, 300, 550), ""); GUI.Box(new Rect(MyBoxLeft + 300, 250, 200, 150), Sol_celle1, KøbsmenuBilleder); //LABELS GUI.Label(new Rect(MyBoxLeft + 340, 200, 300, 100), "Vindmølle3", købsmenuTekst); GUI.Box(new Rect(MyBoxLeft + 250, 400, 300, 100), "blahblahblah jeg er en masse tekst\n blah blah blah\n blah blah blah blahb\n lahb alhbasdohd \n JEG KAN GODT LIDE KAGE!!!!!", brødtekst); } //Vindmølle4 if (vindmølle4) { //BUTTON if (GUI.Button(new Rect(MyBoxLeft + 320, 600, 150, 70), "Bestil")) { Debug.Log("Clicked the button with an image"); } //BOXES GUI.Box(new Rect(MyBoxLeft + 250, 170, 300, 550), ""); GUI.Box(new Rect(MyBoxLeft + 300, 250, 200, 150), Sol_celle1, KøbsmenuBilleder); //LABELS GUI.Label(new Rect(MyBoxLeft + 340, 200, 300, 100), "Vindmølle4", købsmenuTekst); GUI.Box(new Rect(MyBoxLeft + 250, 400, 300, 100), "blahblahblah jeg er en masse tekst\n blah blah blah\n blah blah blah blahb\n lahb alhbasdohd \n JEG KAN GODT LIDE KAGE!!!!!", brødtekst); } } void Update() { } //Det der laver Slide Animationen på GUI IEnumerator AnimateBox() { if (MyBoxLeft == -600f) { while (MyBoxLeft < 20.0f) { MyBoxLeft += 5.0f; yield return 0; } } else if (MyBoxLeft == 20.0f) { while (MyBoxLeft > -600.0f) { MyBoxLeft -= 5.0f; yield return 0; } } } }
EDIT3 Ok, I got it now, and I feel like a giant idiot for failing at this... so easy well that is what happens when you work on coding for 6 hours in a row :P agian thanks for the help and if anyone want the souce code just post a comment but really just think for two seconds and it make sense.
Yes! Some artwork would be good to fully understand what you are looking for. Then we can setup a system for you.
Note: Always try to add as describing information as possible (and yes, a picture describes more then 1000 words).
So, my first question is: Are you sure that Solcelle 1 window doesn't get drawn when you click on the button Solcelle 2, because it sounds like it does. You will have to make up an condition in the OnGui function.
Hard for me to tell when I don't know how your system is setup at the moment.
EDIT Nice artwork btw, very describing
First:
You should really consider using states ins$$anonymous$$d of bools for each solcelle (but this isn't your problem, just saying).
Second:
I like this comment of yours: //Buttons for forskellige udendørs ting WARNING LOT OF CODE!
Third:
if (GUI.Button(new Rect($$anonymous$$yBoxLeft + 80, 230, 150, 30), "SolCelle1")) { solcelle1 = solcelle1 ? false : true; }
if (GUI.Button(new Rect($$anonymous$$yBoxLeft + 80, 270, 150, 30), "SolCelle2")) { solcelle2 = solcelle2 ? false : true; }
When are you setting solcelle1 to false when clicking on solcelle2? In your case you would have to set all bools to false expect the one that you are clicking on. If you ain't solving it I can write a simple state of your solcelles tonight.
Do you know if there is a unity script reference page on states? I can't seem to find one and it sounds like that would make it alot easier for the eye.
Answer by OpenCoffee · Apr 24, 2013 at 11:13 AM
Well, this could actually be done in hundreds of different way and with the same result as an outcome. For example you could create states and do something like this:
if( GUILayout.Button("Hero Equipment") )
_currentState = MenuState.Equipment;
else if( GUILayout.Button("Hero Stats") )
_currentState = MenuState.Stats;
And later in your OnGUI function:
switch ( _currentState )
{
case MenuState.Equipment:
// Draw equipment window
break;
case MenuState.Stats:
// Draw stats window
break;
}
U are using GUILayout, would GUI.button work the same way? I have been using GUI. for making this
The mayor difference between GUI and GUILayout is that you will have to assign the location of the elements yourself when using for example GUI.Lable. GUILayout will structure this for you, starting from the very top left of the screen ( 0, 0 ) and you can modify the location by using GUILayout.Space( int );
Your answer
Follow this Question
Related Questions
GUILayout Context Menu on Right Click - Not Working! 1 Answer
Distribute terrain in zones 3 Answers
Display multiple scenes? 1 Answer
Game Menus WIth Keyboard Input Control 1 Answer
C sharp menu problem with bottons 1 Answer