static function Window (id : int, clientRect : Rect, func : WindowFunction, title : GUIContent, style : GUIStyle) : Rect
Parameters
Name | Description |
Style |
An optional style to use for the window. If left out, the window style from the current GUISkin is used.
|
Returns
Rect - The rectangle the window is at.
Description
Make a popup window.
Windows float above normal GUI controls, feature click-to-focus and can optionally be dragged around by the end user.
Unlike other controls, you need to pass them a separate function for the GUI controls to put inside the window.
Note: If you are using GUILayout to place your components inside the window, you should use GUILayout.Window.
Here is a small example to get you started:
var windowRect : Rect = Rect (20, 20, 120, 50);
function OnGUI () {
windowRect = GUI.Window (0, windowRect, DoMyWindow, "My Window");
}
function DoMyWindow (windowID : int) {
if (GUI.Button (Rect (10,20,100,20), "Hello World"))
print ("Got a click");
}
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Rect windowRect = new Rect(20, 20, 120, 50);
void OnGUI() {
windowRect = GUI.Window(0, windowRect, DoMyWindow, "My Window");
}
void DoMyWindow(int windowID) {
if (GUI.Button(new Rect(10, 20, 100, 20), "Hello World"))
print("Got a click");
}
}
import UnityEngine
import System.Collections
class example(MonoBehaviour):
public windowRect as Rect = Rect(20, 20, 120, 50)
def OnGUI():
windowRect = GUI.Window(0, windowRect, DoMyWindow, 'My Window')
def DoMyWindow(windowID as int):
if GUI.Button(Rect(10, 20, 100, 20), 'Hello World'):
print('Got a click')
You can use the same function to create multiple windows. Just make sure that
each window has its own ID. Example:
var windowRect0 : Rect = Rect (20, 20, 120, 50);
var windowRect1 : Rect = Rect (20, 100, 120, 50);
function OnGUI () {
windowRect0 = GUI.Window (0, windowRect0, DoMyWindow, "My Window");
windowRect1 = GUI.Window (1, windowRect1, DoMyWindow, "My Window");
}
function DoMyWindow (windowID : int) {
if (GUI.Button (Rect (10,20,100,20), "Hello World"))
print ("Got a click in window " + windowID);
GUI.DragWindow (Rect (0,0,10000,10000));
}
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Rect windowRect0 = new Rect(20, 20, 120, 50);
public Rect windowRect1 = new Rect(20, 100, 120, 50);
void OnGUI() {
windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, "My Window");
windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, "My Window");
}
void DoMyWindow(int windowID) {
if (GUI.Button(new Rect(10, 20, 100, 20), "Hello World"))
print("Got a click in window " + windowID);
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}
}
import UnityEngine
import System.Collections
class example(MonoBehaviour):
public windowRect0 as Rect = Rect(20, 20, 120, 50)
public windowRect1 as Rect = Rect(20, 100, 120, 50)
def OnGUI():
windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, 'My Window')
windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, 'My Window')
def DoMyWindow(windowID as int):
if GUI.Button(Rect(10, 20, 100, 20), 'Hello World'):
print(('Got a click in window ' + windowID))
GUI.DragWindow(Rect(0, 0, 10000, 10000))
To stop showing a window, simply stop calling
GUI.Window from inside your main OnGUI function:
var doWindow0 : boolean = true;
function DoWindow0 (windowID : int) {
GUI.Button (Rect (10,30, 80,20), "Click Me!");
}
function OnGUI () {
doWindow0 = GUI.Toggle (Rect (10,10,100,20), doWindow0, "Window 0");
if (doWindow0)
GUI.Window (0, Rect (110,10,200,60), DoWindow0, "Basic Window");
}
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public bool doWindow0 = true;
void DoWindow0(int windowID) {
GUI.Button(new Rect(10, 30, 80, 20), "Click Me!");
}
void OnGUI() {
doWindow0 = GUI.Toggle(new Rect(10, 10, 100, 20), doWindow0, "Window 0");
if (doWindow0)
GUI.Window(0, new Rect(110, 10, 200, 60), DoWindow0, "Basic Window");
}
}
import UnityEngine
import System.Collections
class example(MonoBehaviour):
public doWindow0 as bool = true
def DoWindow0(windowID as int):
GUI.Button(Rect(10, 30, 80, 20), 'Click Me!')
def OnGUI():
doWindow0 = GUI.Toggle(Rect(10, 10, 100, 20), doWindow0, 'Window 0')
if doWindow0:
GUI.Window(0, Rect(110, 10, 200, 60), DoWindow0, 'Basic Window')
To make a window that gets its size from automatic GUI layouting, use
GUILayout.Window.
Call Ordering
Windows need to be drawn back-to-front; windows on top of other windows need to be drawn later than the ones below them. This means that you can not count on your DoWindow functions to
be called in any particular order. In order for this to work seamlessly, the following values are stored when you create your window (using the
Window function), and retrieved when your DoWindow gets called:
GUI.skin,
GUI.enabled,
GUI.color,
GUI.backgroundColor,
GUI.contentColor,
GUI.matrix
This means it's easy to do colored windows like this:
var windowRect0 : Rect = Rect (20, 20, 120, 50);
var windowRect1 : Rect = Rect (20, 100, 120, 50);
function OnGUI () {
GUI.color = Color.red;
windowRect0 = GUI.Window (0, windowRect0, DoMyWindow, "Red Window");
GUI.color = Color.green;
windowRect1 = GUI.Window (1, windowRect1, DoMyWindow, "Green Window");
}
function DoMyWindow (windowID : int) {
if (GUI.Button (Rect (10,20,100,20), "Hello World"))
print ("Got a click in window with color " + GUI.color);
GUI.DragWindow (Rect (0,0,10000,10000));
}
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Rect windowRect0 = new Rect(20, 20, 120, 50);
public Rect windowRect1 = new Rect(20, 100, 120, 50);
void OnGUI() {
GUI.color = Color.red;
windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, "Red Window");
GUI.color = Color.green;
windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, "Green Window");
}
void DoMyWindow(int windowID) {
if (GUI.Button(new Rect(10, 20, 100, 20), "Hello World"))
print("Got a click in window with color " + GUI.color);
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}
}
import UnityEngine
import System.Collections
class example(MonoBehaviour):
public windowRect0 as Rect = Rect(20, 20, 120, 50)
public windowRect1 as Rect = Rect(20, 100, 120, 50)
def OnGUI():
GUI.color = Color.red
windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, 'Red Window')
GUI.color = Color.green
windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, 'Green Window')
def DoMyWindow(windowID as int):
if GUI.Button(Rect(10, 20, 100, 20), 'Hello World'):
print(('Got a click in window with color ' + GUI.color))
GUI.DragWindow(Rect(0, 0, 10000, 10000))
Hint: you can use the alpha component of
GUI.color to fade windows in and out.
See Also: DragWindow,
BringWindowToFront,
BringWindowToBack.