Version: 2020.1
public static Rect Window (int id, Rect clientRect, GUI.WindowFunction func, string text);
public static Rect Window (int id, Rect clientRect, GUI.WindowFunction func, Texture image);
public static Rect Window (int id, Rect clientRect, GUI.WindowFunction func, GUIContent content);
public static Rect Window (int id, Rect clientRect, GUI.WindowFunction func, string text, GUIStyle style);
public static Rect Window (int id, Rect clientRect, GUI.WindowFunction func, Texture image, GUIStyle style);
public static Rect Window (int id, Rect clientRect, GUI.WindowFunction func, GUIContent title, GUIStyle style);

参数

Style (可选)用于窗口的样式。如果省略,则使用当前 GUISkinwindow 样式。
id 窗口的 ID 编号(只要保证唯一,可以使用任意值)。
clientRect 表示窗口位置和大小的屏幕矩形。
func 显示窗口内容的脚本函数。
text 要在窗口内呈现的文本。
image 要在窗口内呈现的图像。
content 要在窗口内呈现的 GUIContent。
style 窗口的样式信息。
title 窗口标题栏中显示的文本。

返回

Rect 表示窗口位置和大小的屏幕矩形。

描述

创建一个弹出窗口。

窗口浮动在普通 GUI 控件上方,可通过点击获得焦点,可以选择是否允许最终用户拖动。与其他控件不同,您需要为它们传递一个在窗口内呈现 GUI 控件的独立函数。

注意:如果您使用 GUILayout 将组件放置在窗口内,则应使用 GUILayout.Window。此外,如果将 MonoBehaviour.useGUILayout 设置为 false,则调用 GUI.Window 没有任何作用,即使它不是 GUILayout 函数。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public Rect windowRect = new Rect(20, 20, 120, 50);

void OnGUI() { // Register the window. Notice the 3rd parameter windowRect = GUI.Window(0, windowRect, DoMyWindow, "My Window"); }

// Make the contents of the window void DoMyWindow(int windowID) { if (GUI.Button(new Rect(10, 20, 100, 20), "Hello World")) { print("Got a click"); } } }

可以使用相同的函数创建多个窗口。只需确保每个窗口都有自己的 ID。示例:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public Rect windowRect0 = new Rect(20, 20, 120, 50); public Rect windowRect1 = new Rect(20, 100, 120, 50);

void OnGUI() { // Register the window. We create two windows that use the same function // Notice that their IDs differ windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, "My Window"); windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, "My Window"); }

// Make the contents of the window void DoMyWindow(int windowID) { if (GUI.Button(new Rect(10, 20, 100, 20), "Hello World")) { print("Got a click in window " + windowID); }

// Make the windows be draggable. GUI.DragWindow(new Rect(0, 0, 10000, 10000)); } }

要停止显示窗口,只需在您的主 OnGUI 函数中停止调用 GUI.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

using UnityEngine; using System.Collections;

public class ExampleClass : MonoBehaviour { public bool doWindow0 = true;

// Make the contents of the window. void DoWindow0(int windowID) { GUI.Button(new Rect(10, 30, 80, 20), "Click Me!"); }

void OnGUI() { // Make a toggle button for hiding and showing the window doWindow0 = GUI.Toggle(new Rect(10, 10, 100, 20), doWindow0, "Window 0");

// Make sure we only call GUI.Window if doWindow0 is true. if (doWindow0) { GUI.Window(0, new Rect(110, 10, 200, 60), DoWindow0, "Basic Window"); } } }

要使窗口从自动 GUI 布局中获得其大小,请使用 GUILayout.WindowCall Ordering 窗口需要从后往前绘制;位于其他窗口之上的窗口在绘制其下方窗口后绘制。这意味着您不能假定系统会以任何特定顺序 调用您的 DoWindow 函数。为使工作能够无缝进行,在您创建窗口(使用 Window 函数)时将存储以下值,并在调用您的 DoWindow 时检索这些值: GUI.skinGUI.enabledGUI.colorGUI.backgroundColorGUI.contentColorGUI.matrix

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public Rect windowRect0 = new Rect(20, 20, 120, 50); public Rect windowRect1 = new Rect(20, 100, 120, 50);

void OnGUI() { // Here we make 2 windows. We set the GUI.color value to something before each. GUI.color = Color.red; windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, "Red Window");

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

// Make the contents of the window. // The value of GUI.color is set to what it was when the window // was created in the code above. 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); }

// Make the windows be draggable. GUI.DragWindow(new Rect(0, 0, 10000, 10000)); } }

注意,您可以使用 GUI.color 的 Alpha 分量来使窗口具有淡入淡出效果。

另请参阅:DragWindowBringWindowToFrontBringWindowToBack