Note: It’s strongly recommended to use the UI Toolkit to extend the Unity Editor, as it provides a more modern, flexible, and scalable solution than IMGUI.
可以在该应用程序中创建任意数量的自定义窗口。这些窗口的行为与 Inspector、Scene 视图或任何其他内置视图类似。这是为游戏的子系统添加用户界面的好方法。
创建自定义编辑器窗口涉及以下简单步骤:
为了创建编辑器窗口,必须将脚本存储在称为“Editor”的文件夹中。在派生自 EditorWindow 的此脚本内创建一个类。然后在内部 OnGUI 函数中编写 GUI 控件。
using UnityEngine;
using UnityEditor;
using System.Collections;
public class Example : EditorWindow
{
void OnGUI () {
// 此处为实际窗口代码
}
}
MyWindow.cs - placed in a folder called ‘Editor’ within your project.
要在屏幕上显示窗口,请创建一个菜单项来显示该窗口。为此,需要创建一个由 MenuItem 属性 激活的函数。
The default behavior in Unity is to recycle windows, so selecting the menu item again would show existing windows. This is done by using the function EditorWindow.GetWindow Like this:
using UnityEngine;
using UnityEditor;
using System.Collections;
class MyWindow : EditorWindow {
[MenuItem ("Window/My Window")]
public static void ShowWindow () {
EditorWindow.GetWindow(typeof(MyWindow));
}
void OnGUI () {
// 此处为实际窗口代码
}
}
显示 MyWindow
此代码将创建一个标准、可停靠的编辑器窗口。该窗口可以在调用之间保存自己的位置,可以用在自定义布局中等等。为了更好地控制创建的内容,可以使用 GetWindowWithRect
应通过实现 OnGUI 函数来渲染窗口的实际内容。可以使用与游戏内 GUI 相同的 UnityGUI 类(__GUI__ 和 GUILayout__)。此外,我们提供了一些额外 GUI 控件,这些控件位于仅用于编辑器的 EditorGUI__ 和 EditorGUILayout 类中。这些类将添加到普通类中已有的控件,因此您可以随意混合和搭配。
以下 C# 代码显示了将 GUI 元素添加到自定义 EditorWindow 的方式:
using UnityEditor;
using UnityEngine;
public class MyWindow : EditorWindow
{
string myString = "Hello World";
bool groupEnabled;
bool myBool = true;
float myFloat = 1.23f;
// 将名为"My Window"的菜单项添加到 Window 菜单
[MenuItem("Window/My Window")]
public static void ShowWindow()
{
//显示现有窗口实例。如果没有,请创建一个。
EditorWindow.GetWindow(typeof(MyWindow));
}
void OnGUI()
{
GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
myString = EditorGUILayout.TextField ("Text Field", myString);
groupEnabled = EditorGUILayout.BeginToggleGroup ("Optional Settings", groupEnabled);
myBool = EditorGUILayout.Toggle ("Toggle", myBool);
myFloat = EditorGUILayout.Slider ("Slider", myFloat, -3, 3);
EditorGUILayout.EndToggleGroup ();
}
}
此示例将生成一个如下所示的窗口:
有关更多信息,请查看 EditorWindow 页面上的示例和文档。
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.