id | 用于每个窗口的唯一 ID。您将使用该 ID 与其进行交互。 |
screenRect | 屏幕上用于窗口的矩形。布局系统会尝试将窗口适配到其内部 - 如果无法完成,则调整矩形以进行适配。 |
func | 在窗口/内/创建 GUI 的函数。该函数必须接收一个参数 - 要为其创建 GUI 的窗口的 /id/。 |
text | 要显示为窗口标题的文本。 |
image | 在标题栏中显示图像的 Texture。 |
content | 该窗口的文本、图像和工具提示。 |
style | (可选)用于窗口的样式。如果省略,则使用当前 GUISkin 的 window 样式。 |
options | (可选)一个布局选项列表,用于指定额外的布局属性。此处传递的任何值都将覆盖 style 或您传入的 screenRect 定义的设置。另请参阅:GUILayout.Width、GUILayout.Height、GUILayout.MinWidth、GUILayout.MaxWidth、GUILayout.MinHeight、 GUILayout.MaxHeight、GUILayout.ExpandWidth、GUILayout.ExpandHeight。 |
Rect 窗口所在的矩形。其位置和大小可以与您传入的矩形不同。
创建一个对自身内容进行自动布局的弹出窗口。
窗口浮动在普通 GUI 控件上方,可通过点击获得焦点,可以选择是否允许最终用户拖动。
与其他控件不同,您需要为它们传递一个将 GUI 控件放置在窗口内的独立函数。下面是一个帮您入门的小示例:
\
游戏视图中的窗口。
using UnityEngine;
public class ExampleScript : MonoBehaviour { Rect windowRect = new Rect(20, 20, 120, 50);
void OnGUI() { // Register the window. Notice the 3rd parameter windowRect = GUILayout.Window(0, windowRect, DoMyWindow, "My Window"); }
// Make the contents of the window void DoMyWindow(int windowID) { // This button will size to fit the window if (GUILayout.Button("Hello World")) { print("Got a click"); } } }
您传入该函数的屏幕矩形仅用作指导。要对窗口应用额外限制,请传入一些额外的布局选项。在此应用的选项将覆盖计算出的大小。下面是一个小示例:
using UnityEngine;
public class ExampleScript : MonoBehaviour { Rect windowRect = new Rect(20, 20, 120, 50);
void OnGUI() { // Register the window. Here we instruct the layout system to // make the window 100 pixels wide no matter what. windowRect = GUILayout.Window(0, windowRect, DoMyWindow, "My Window", GUILayout.Width(100)); }
// Make the contents of the window void DoMyWindow(int windowID) { // This button is too large to fit the window // Normally, the window would have been expanded to fit the button, but due to // the GUILayout.Width call above the window will only ever be 100 pixels wide if (GUILayout.Button("Please click me a lot")) { print("Got a click"); } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.