GUI.Window 窗口

static function Window (id : int, clientRect : Rect, func : WindowFunction, text : String) : Rect
static function Window (id : int, clientRect : Rect, func : WindowFunction, image : Texture) : Rect
static function Window (id : int, clientRect : Rect, func : WindowFunction, content : GUIContent) : Rect
static function Window (id : int, clientRect : Rect, func : WindowFunction, text : String, style : GUIStyle) : Rect
static function Window (id : int, clientRect : Rect, func : WindowFunction, image : Texture, style : GUIStyle) : Rect
static function Window (id : int, clientRect : Rect, func : WindowFunction, title : GUIContent, style : GUIStyle) : Rect

Parameters参数

Returns

Rect - the rectangle the window is at.

返回 Rect类型,窗口所在的矩形。

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:

窗口浮动在普通GUI控件之上,点选焦点和能被终端用户随意拖动的特点,不像其他的控件,你需要为GUI控件传递她们一个单独的函数放进窗口。注意,如果你使用GUILayout在窗口中放你的组件,你需要使用GUILayout.Window。这儿是一个小例子助你理解:

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");

	}
}
var windowRect : Rect = Rect(20, 20, 120, 50);

function OnGUI () {
	// Register the window. Notice the 3rd parameter
	//注册窗口,注意第三个参数
	windowRect = GUI.Window(0, windowRect, DoMyWindow, "My Window");
}

// Make the contents of the window
//制作窗口的内容
function DoMyWindow (windowID : 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:

你可以使用一样的函数来创建多个窗口,要确定每一个窗口有自己的ID。

例如:

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));
	}
}
var windowRect0 : Rect = Rect(20, 20, 120, 50);
var windowRect1 : Rect = Rect(20, 100, 120, 50);

function OnGUI () {
	// Register the window. We create two windows that use the same function
	// Notice that their IDs differ
	//注册窗口,我们用一样的函数创建两个窗口,注意它们的ID不同
	windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, "My Window");
	windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, "My Window");
}

// Make the contents of the window
//制作窗口的内容
function DoMyWindow (windowID : int) {
if (GUI.Button(Rect (10,20,100,20), "Hello World"))
	print ("Got a click in window " + windowID);
	// Make the windows be draggable.
	//制作窗口可以拖动
	GUI.DragWindow(Rect (0,0,10000,10000));
}

To stop showing a window, simply stop calling GUI.Window from inside your main DoGUI function:

停止显示一个窗口,从你的主DoGUI函数中简单停止调用GUI.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");

	}
}
// boolean variable to decide whether to show the window or not.
// Change this from the in-game GUI, scripting, the inspector or anywhere else to
// decide whether the window is visible
//布尔变量决定窗口是否显示,你可以从游戏界面,脚本,检视面板或者其他地方决定窗口是否可见
var doWindow0 : boolean = true;

// Make the contents of the window.
//创建窗口的内容。
function DoWindow0 (windowID : int) {
	GUI.Button(Rect (10,30, 80,20), "Click Me!");
}

function OnGUI () {
	// Make a toggle button for hiding and showing the window
	//创建开关按钮,用于隐藏显示窗口
	doWindow0 = GUI.Toggle(Rect (10,10,100,20), doWindow0, "Window 0");

	// Make sure we only call GUI.Window if doWindow0 is true.
	//确定我们如果doWindow0为True,仅调用窗口GUI.Window
	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.

要创建一个自动GUI布局的窗口,使用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

窗口需要从后向前绘制。在其他窗口上面的窗口需要比他们下面的窗口晚绘制。你不能指望在你的Dowindow函数中调用任意特定排序。为了使整个工作无缝,当你创建窗口时下面的值被储存(使用窗口函数),并且当你的DoWindow被GUI.skin, GUI.enabled, GUI.color, GUI.backgroundColor, GUI.contentColor, GUI.matrix调用时,重新取回。

This means it's easy to do colored windows like this:

这意味着它很容易做这样的彩色窗口:

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));
	}
}
var windowRect0 : Rect = Rect(20, 20, 120, 50);
var windowRect1 : Rect = Rect(20, 100, 120, 50);

function OnGUI () {
	//创建2个窗口,我们设置每个窗口GUI.color颜色不同
	GUI.color = Color.red;
	windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, "Red Window");

	GUI.color = Color.green;
	windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, "Green Window");
}

//创建窗口的内容,GUI.color的值应用到在上面的代码中创建的窗口。
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));
}

Hint: you can use the alpha component of GUI.color to fade windows in and out.

提示:你可以使用GUI.color的组件通道,来淡入淡出窗口。

另见: DragWindow, BringWindowToFront, BringWindowToBack

Page last updated: 2011-1-14