语言英语
  • C#

EditorWindow.CreateGUI()

描述

CreateGUI 在 EditorWindow 的 rootVisualElement 准备好填充时被调用。

使用 CreateGUI 将 UI 工具包 UI 元素添加到您的窗口。

创建自定义编辑器窗口时,请遵循以下指南

  • 将依赖于 UXML/USS 加载的代码放在 CreateGUI 方法中,以确保所有必要的资源都可用。
  • 将事件注册代码保留在 CreateGUI 内或 CreateGUI 调用之后。

需要注意的是,在第一次调用 Update 之前,可能不会调用 CreateGUI。有关更多信息,请参阅 编辑器窗口的执行顺序

有关如何创建编辑器窗口以响应用户输入的示例,请参阅 使用 C# 脚本创建自定义编辑器窗口

// The window appears in front of the Editor.
// The window shows the type of a Unity object the cursor is over.

using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;

public class MouseOverWindowExample : EditorWindow
{
    string mouseOver = "Nothing...";
    Label label;

    [MenuItem("Examples/Mouse Over Example")]
    static void Init()
    {
        GetWindow<MouseOverWindowExample>("mouseOver");
    }

    void CreateGUI()
    {
        label = new Label($"Mouse over: {mouseOver}");
        rootVisualElement.Add(label);
    }

    void Update()
    {
        label.schedule.Execute(() =>
        {
            mouseOver = EditorWindow.mouseOverWindow ?
                        EditorWindow.mouseOverWindow.ToString() : "Nothing...";
            label.text = $"Mouse over: {mouseOver}";
        }).Every(10);
    }
}

Did you find this page useful? Please give it a rating: