用于显示从 PopupWindowContent 继承的弹出窗口的类。
弹出窗口没有边距,不可拖动,也无法调节大小。此外,它们还会在失去焦点时自动关闭。它们用于显示短暂的信息或选项。
编辑器中的弹出窗口的一个示例是编辑器“Scene View”工具栏中的“Scene View Effects”选项:
下面是一个自定义弹出窗口的示例,此弹出窗口通过编辑器窗口中的一个按钮显示。该弹出窗口具有三个开关值,并会在失去焦点时自动关闭。此示例以两个脚本的形式提供。第一个脚本定义可通过菜单项打开的编辑器窗口。该编辑器窗口具有一个可显示弹出窗口的按钮。第二个脚本以一个单独的类的形式定义弹出窗口本身的内容。
首先,下面是启动弹出窗口的简单编辑器窗口的代码:
using UnityEngine; using UnityEditor;
public class EditorWindowWithPopup : EditorWindow { // Add menu item [MenuItem("Example/Popup Example")] static void Init() { EditorWindow window = EditorWindow.CreateInstance<EditorWindowWithPopup>(); window.Show(); }
Rect buttonRect; void OnGUI() { { GUILayout.Label("Editor window with Popup example", EditorStyles.boldLabel); if (GUILayout.Button("Popup Options", GUILayout.Width(200))) { PopupWindow.Show(buttonRect, new PopupExample()); } if (Event.current.type == EventType.Repaint) buttonRect = GUILayoutUtility.GetLastRect(); } } }
接下来,下面是弹出窗口本身的代码:
using UnityEngine; using UnityEditor;
public class PopupExample : PopupWindowContent { bool toggle1 = true; bool toggle2 = true; bool toggle3 = true;
public override Vector2 GetWindowSize() { return new Vector2(200, 150); }
public override void OnGUI(Rect rect) { GUILayout.Label("Popup Options Example", EditorStyles.boldLabel); toggle1 = EditorGUILayout.Toggle("Toggle 1", toggle1); toggle2 = EditorGUILayout.Toggle("Toggle 2", toggle2); toggle3 = EditorGUILayout.Toggle("Toggle 3", toggle3); }
public override void OnOpen() { Debug.Log("Popup opened: " + this); }
public override void OnClose() { Debug.Log("Popup closed: " + this); } }
以上每段代码应另存为以其类名称命名的单独文件。这些代码都不是行为,因此无需将它们放在游戏对象上。将它们放在项目中后,通过转到新的“Example”菜单并选择“Popup Example”即可试用。然后,单击新编辑器窗口中的按钮可显示弹出选项窗口。.
Show | 显示具有给定 PopupWindowContent 的弹出窗口。 |
focusedWindow | 当前已获得键盘焦点的 EditorWindow。(只读) |
mouseOverWindow | 当前在鼠标光标下的 EditorWindow。(只读) |
autoRepaintOnSceneChange | 窗口是否会在场景每次发生变化时自动重绘? |
docked | Returns true if EditorWindow is docked. |
hasFocus | Returns true if EditorWindow is focused. |
hasUnsavedChanges | This property specifies whether the Editor prompts the user to save or discard unsaved changes before the window closes. |
maximized | 此窗口是否已最大化? |
maxSize | The maximum size of this window when it is floating or modal. The maximum size is not used when the window is docked. |
minSize | The minimum size of this window when it is floating or modal. The minimum size is not used when the window is docked. |
position | 窗口在屏幕空间中的理想位置。 |
rootVisualElement | 获取窗口层级视图的根视觉元素。 |
saveChangesMessage | The message that displays to the user if they are prompted to save |
titleContent | 用于绘制 EditorWindow 标题的 GUIContent。 |
wantsLessLayoutEvents | Specifies whether a layout pass is performed before all user events (for example, EventType.MouseDown or [[EventType, KeyDown]]), or is only performed before repaint events. |
wantsMouseEnterLeaveWindow | 检查是否已在此编辑器窗口的 GUI 中收到 MouseEnterWindow 和 MouseLeaveWindow 事件。 |
wantsMouseMove | 检查是否已在此编辑器窗口的 GUI 中收到 MouseMove 事件。 |
hideFlags | 该对象应该隐藏、随场景一起保存还是由用户修改? |
name | 对象的名称。 |
BeginWindows | 标记所有弹出窗口的开始区域。 |
Close | 关闭编辑器窗口。 |
DiscardChanges | Discards unsaved changes to the contents of the window. |
EndWindows | 关闭由 EditorWindow.BeginWindows 开始的窗口组。 |
Focus | 将键盘焦点移动到另一个 EditorWindow。 |
GetExtraPaneTypes | 获取与窗口关联的额外窗格。 |
RemoveNotification | 停止显示通知消息。 |
Repaint | 重绘窗口。 |
SaveChanges | Performs a save action on the contents of the window. |
SendEvent | 将事件发送到窗口。 |
Show | 显示 EditorWindow 窗口。 |
ShowAsDropDown | 显示包含下拉菜单和样式的窗口。 |
ShowAuxWindow | 在辅助窗口中显示编辑器窗口。 |
ShowModal | 显示模态编辑器窗口。 |
ShowModalUtility | 将 EditorWindow 显示为浮动模态窗口。 |
ShowNotification | 显示通知消息。 |
ShowPopup | 使用弹出式框架显示编辑器窗口。 |
ShowUtility | 将 EditorWindow 显示为浮动实用程序窗口。 |
TryGetOverlay | Get an Overlay with matching ID from an EditorWindow canvas. |
GetInstanceID | Gets the instance ID of the object. |
ToString | 返回对象的名称。 |
OnBackingScaleFactorChanged | Called when the UI scaling for this EditorWindow is changed. |
CreateWindow | 创建类型为 T 的 EditorWindow。 |
FocusWindowIfItsOpen | 聚焦发现的第一个指定类型的 EditorWindow(如果已打开)。 |
GetWindow | 返回当前屏幕上第一个 t 类型的 EditorWindow。 |
GetWindowWithRect | 返回当前屏幕上第一个 t 类型的 EditorWindow。 |
HasOpenInstances | 检查编辑器窗口是否已打开。 |
Destroy | 移除 GameObject、组件或资源。 |
DestroyImmediate | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 |
DontDestroyOnLoad | 在加载新的 Scene 时,请勿销毁 Object。 |
FindObjectOfType | 返回第一个类型为 type 的已加载的激活对象。 |
FindObjectsOfType | Gets a list of all loaded objects of Type type. |
Instantiate | 克隆 original 对象并返回克隆对象。 |
CreateInstance | 创建脚本化对象的实例。 |
bool | 该对象是否存在? |
operator != | 比较两个对象是否引用不同的对象。 |
operator == | 比较两个对象引用,判断它们是否引用同一个对象。 |
Awake | 在新窗口打开时调用。 |
CreateGUI | CreateGUI is called when the EditorWindow's rootVisualElement is ready to be populated. |
hasUnsavedChanges | This property specifies whether the Editor prompts the user to save or discard unsaved changes before the window closes. |
OnDestroy | 调用 OnDestroy 以关闭 EditorWindow 窗口。 |
OnFocus | 在窗口获得键盘焦点时调用。 |
OnGUI | 在此处实现您自己的 Editor GUI。 |
OnHierarchyChange | 处理程序,用于在层级视图中的对象或对象组发生更改时发送的消息。 |
OnInspectorUpdate | OnInspectorUpdate 以每秒 10 帧的速度调用,以便检视面板有机会进行更新。 |
OnLostFocus | 在窗口失去键盘焦点时调用。 |
OnProjectChange | 处理程序,用于在项目状态发生更改时发送的消息。 |
OnSelectionChange | 每当选择发生更改时调用。 |
saveChangesMessage | The message that displays to the user if they are prompted to save |
Update | 在所有可见窗口上每秒调用多次。 |
Awake | 当 ScriptableObject 脚本启动时调用此函数。 |
OnDestroy | 当脚本化对象将销毁时调用此函数。 |
OnValidate | Editor-only function that Unity calls when the script is loaded or a value changes in the Inspector. |
Reset | 重置为默认值。 |
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.