- Home /
Hiding/Showing window on button
I've tried many things, but I can't get this javascript to work. I've got a hotbar with some buttons init, and I want to create another rect window, and have one of the buttons in the hotbar, open and close this window for the player.
Can you show us your current script?
Essentially you need a boolean variable that you toggle with one button and test for it in the code for the window via an if statement.
Answer by kenkennet20 · Jul 15, 2012 at 08:31 PM
var Interface_Hotbar_Window = Rect(0, 0, 265, 50);
var Interface_01_Window = Rect(100, 100, 200, 200);
var Interface_Hotbar_Image_01 : Texture;
var Interface_Hotbar_Image_02 : Texture;
var Interface_Hotbar_Image_03 : Texture;
var Interface_Hotbar_Image_04 : Texture;
var Interface_Hotbar_Image_05 : Texture;
var Interface_Hotbar_Image_06 : Texture;
function OnGUI ()
{
Interface_Hotbar_Window = GUI.Window (0, Interface_Hotbar_Window, Interface_Hotbar_Settings, "");
Interface_01_Window = GUI.Window (1, Interface_01_Window, Interface_01_Settings, "");
}
function Interface_Hotbar_Settings (windowID : int)
{
if (GUI.Button(Rect(5,5,40,40),Interface_Hotbar_Image_01));
// Not implemented.
if (GUI.Button(Rect(48,5,40,40),Interface_Hotbar_Image_02));
// Not implemented.
if (GUI.Button(Rect(91,5,40,40),Interface_Hotbar_Image_03));
// Not implemented.
if (GUI.Button(Rect(134,5,40,40),Interface_Hotbar_Image_04));
// Not implemented.
if (GUI.Button(Rect(177,5,40,40),Interface_Hotbar_Image_05));
// Not implemented.
if (GUI.Button(Rect(220,5,40,40),Interface_Hotbar_Image_06))
{
Application.Quit();
}
GUI.DragWindow ();
}
function Interface_01_Settings (windowID : int)
{
GUI.DragWindow ();
}
Answer by Jixi · Jul 15, 2012 at 11:18 PM
Here's how i would do it in c#(should be easy enough to convert it into js)
bool hideWindow = true;
void OnGUI()
{
if(GUI.Button(new Rect(30, 30, 100, 100), "Button"))
{
//When button is pressed it toggles the hideWindow boolean
if(hideWindow) hideWindow = false;
else hideWindow = true;
}
if(hideWindow)
{
//Your window code here
}
}
Answer by kenkennet20 · Jul 16, 2012 at 02:49 PM
Thanks a lot, it works almost fine, converted to JavaScript, but how do I reference to a window outside my rect. I mean, I created the buttons inside a rect, so the bar can be moved around, but this also means that if I do it this way, the window that I open, will be put inside this little area too. How do I make a window outside of this confined Interface_hotbar_settings, and have the true/false thing show/hide that window, outside?