How can I via button click to close a GUI window. In other words, How can I make it with one click, you can close a GUI window.
Thanks
How can I via button click to close a GUI window. In other words, How can I make it with one click, you can close a GUI window.
Thanks
First of all: You should probably have a look at the GUI Scripting Guide to get an understanding of how immediate mode GUIs work.
Just don't call the method rendering the window anymore (e.g. using a boolean member variable that you set to false when the button is clicked). Example in C#:
public class MyScript : MonoBehaviour {
private bool render = false;
private Rect windowRect = new Rect (20, 20, 120, 50);
public void ShowWindow() {
render = true;
}
public void HideWindow() {
render = false;
}
public void OnGUI() {
if (GUI.Button (new Rect (10,20,100,20), "Show Window"))
ShowWindow();
if (GUI.Button (new Rect (10,60,100,20), "Hide Window"))
HideWindow();
if (render) {
windowRect = GUI.Window (0, windowRect, DoMyWindow, "My Window");
}
}
public void DoMyWindow(int windowID) {
if (GUI.Button (new Rect (10,20,100,20), "Hello World"))
print ("Got a click");
}
}
Hi below is some JS for you I have included some notes for centering etc
//sets your windows size and centers to screen
var windowRect : Rect = Rect (Rect (Screen.width / 2 + 0, Screen.height / 2 +159, 400, 41));
// with above certain gui items need to be adjusted to center perfectly for example if width 400 Screen.width / 2 + 200 or Screen.width / 2 - 200 depends on how its looking //
function OnGUI () {
windowRect = GUI.Window (0,windowRect, WindowFunction, "");
}
function WindowFunction (windowID : int) { // Draw any Controls inside the window here
if (GUI.Button (Rect (1,20, 397, 20), "Close", "button")) {
print ("you closed the window");
Destroy (this);
}
}
define a variable for window drawing.
if (wDraw == true )
{
//draw the window
}
if (GUI.Button(Rect (10,10,300,120),"window"))
{
wDraw = !(wDraw);
}
Please in Javascript!
Thanks
I was scrolling for something like this cause i was having the same problem, tho the answers given here did not please me, after a few experiments i think i got to a more suitable answer so ill share it here with everyone for future people in need.
The answers given above did not please me because i wanted something more dinamic, so i made a vector of ‘Rects’ where i could ‘store’ several open windows that i would create and close as i wish in some script i had created for my GUI. And where i could even store several types of ‘windows’.
I dont know if it is very correct do to this way but in my head making a script for a window that is not yet created did not fit in my head so i made it this way.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class MyGUIScript : MonoBehaviour {
public Texture2D icon;
List<Rect> rect;
// Use this for initialization
void Start () {
rect = new List<Rect>();
rect.Add(new Rect( Screen.width/2 - 300, Screen.height - 50, 603, 50));
}
void OnGUI ()
{
for(int i=1; i<rect.Count; i++)
{
rect _= GUI.Window (i, rect*, NewXPTOWindow, "My XPTO Window");*_
* }*
* // Make a background box to be our bar*
* GUI.Box(rect[0], “”);*
* for(int i=0; i<12; i++)*
* {*
_ if (GUI.Button (new Rect (Screen.width/2 - 297 + 50i, Screen.height - 47, 46, 46), icon))_
_ {_
_ rect.Add(new Rect( Screen.width/2 - 300, Screen.height/2 - 200, 600, 300 ));_
_ return;_
_ }_
_ }_
_ }*_
* public void NewXPTOWindow (int windowID) { // Make the contents of the window*
* if (GUI.Button (new Rect (550,0,50,20), “Close”)) // the close window button*
* {*
* rect.RemoveAt(windowID);*
* }*
* // Make the window be draggable.*
* GUI.DragWindow (new Rect (0,0,10000,10000));*
* }*
}
So this way one can open and close windows, menus of diferent kinds etc in one GUI.
Please anyone correct me if I’m wrong, I only started with unity a week ago… anyway I’m using this method on my litle game :3
ps: i do ask sorry if there is some minor error in the code, in my game there’s already a few elements in the windows, menus and buttons and more stuff around, so i just tried to take all that out and leave only the core of it.