- Home /
Managing GUI Windows
Hello
I have a GUI menu with many buttons, when clicked the button sets a bool value and opens a new window, at the same time closing the previous window open. Currently I'm setting a lot of bools true or false on each click. I thought I might be able to do something like this:
public void MenuManager(bool openWindow)
{
window1 = false; // Stop showing the window
window2 = false;
window3 = false;
openWindow = true; // Show only this window , Could be any of the above windows
}
The way I am going about it is to set all window bools to false, then set the window bool passed in to the method true to display the window. This results in all of the windows staying closed. Not as I expected, anyone have an idea as to why this is happening? or a suitable alternative.
Thanks
Answer by Mrobertson · Oct 17, 2012 at 01:29 AM
I believe your function isn't working because you are passing openWindow as a value, so this won't actually change the original variable, any changes you make to openWindow dont affect the variable you passed to MenuManager.
public void MenuManager(ref bool openWindow)
{
window1 = false; // Stop showing the window
window2 = false;
window3 = false;
openWindow = true; // Show only this window , Could be any of the above windows
}
might make it work. Instead of using bools, i'd use a enumerator
enum Windows {window1, window2, window3};
Windows currentWindow = Windows.window1;
public void OpenWindow(Windows w)
{
switch(w)
{
case Windows.window1:
//open window 1 close other windows
case Windows.window2:
//open window 2 close other windows
case Windows.window3:
//openWindow 3 close other windows
}
}
another example of enumerations
void OnGUI()
{
if(GUI.Button(new Rect(0,0, 10, 10), "Open Window 1"))
{
currentWindow = Windows.Window1;
OpenWindow(currentWindow);
}
switch(currentWindow)
{
case Windows.NoWindows:
//display gui for no windows
break;
case Windows.Window1:
//display gui for Window1
if(GUI.Button(new Rect(0,0,10,10), "Close Window Button"))
{
currentState = Windows.NoWindows;
}
break;
//........
}
}
Thanks for the reply. For the enumerator where does the Windows currentWindow variable come in to play do I have to set that as the active window? how do I use it as part of the code?
Could I just add a noWindows to the enum and set that as the default so no windows will be shown?
Not knowing your project I can't be completely sure its the best solution, but from your code it looks like you only want one active window at a time so I think It would work alright. Yes you could just add a noWindows enum to the set for when no windows are shown. If you need more than one window state active at a time it might not be better.
I edited in a possible application of enumerations.
Ah I see. Its for a in game menu, I do only want one window open at a time as I want them all use the same positioning.
Your enum idea works great and saved me alot of time retyping out code.
Thanks again
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
One variable set TRUE when another must be false. TOGGLE 3 Answers
Using an Enum for GUI.Toggle? 1 Answer
Distribute terrain in zones 3 Answers
OnGUI sliders only accept float? C# 2 Answers