2025-04-06 Unity Editor 实践 1 —— Editor 窗体框架
文章目录
- 1 介绍
- 2 实现
- 3 效果
- 4 扩展方式
- 附:整体代码
1 介绍
窗体框架由“页面标签”和“页面内容”组成。
点击“页面标签”时,会显示对应的“页面内容”。

2 实现
创建 MyIMGUIEditorWindow.cs 脚本,并继承 EditorWindow。
// ------------------------------------------------------------
// @file MyIMGUIEditorWindow.cs
// @brief
// @author zheliku
// @Modified 2025-04-06 03:04:51
// @Copyright Copyright (c) 2025, zheliku
// ------------------------------------------------------------
namespace EditorExtension
{
using System;
using UnityEditor;
using UnityEngine;
public class MyIMGUIEditorWindow : EditorWindow
{
...
}
}
-
打开窗体
public class MyIMGUIEditorWindow : EditorWindow { ... [MenuItem("EditorExtension/02.IMGUI/02.MyIMGUIEditorWindow")] private static void ShowWindow() { var window = GetWindow<MyIMGUIEditorWindow>(); window.Show(); } ... }
-
配置页面标签
定义
PageId
枚举,表示每个页面标签。public class MyIMGUIEditorWindow : EditorWindow { enum PageId { Basic, Other } [MenuItem("EditorExtension/02.IMGUI/02.MyIMGUIEditorWindow")] private static void ShowWindow() { var window = GetWindow<MyIMGUIEditorWindow>(); window.Show(); } private PageId _currentPageId = PageId.Basic; ... }
-
OnGUI 主逻辑
通过
GUILayout.Toolbar
显示页面标签,并返回当前页面 Id。依据 Id 执行对应绘制函数。
public class MyIMGUIEditorWindow : EditorWindow { ... private void OnGUI() { _currentPageId = (PageId) GUILayout.Toolbar((int) _currentPageId, Enum.GetNames(typeof(PageId))); if (_currentPageId == PageId.Basic) { Basic(); } else { Other(); } } ... }
-
为每个“页面标签”创建绘制方法。
使用
#region
包裹每个“页面标签”的内容。public class MyIMGUIEditorWindow : EditorWindow { ... #region Basic private int i = 0; // Basic 页面中使用的变量,如果定义最好放在 Basic 块中 private void Basic() { GUILayout.Label("Basic: Hello IMGUI"); } #endregion #region Other private void Other() { GUILayout.Label("Other: Hello IMGUI"); } #endregion }
3 效果
在 Basic 页面中,绘制 “Basic: Hello IMGUI”。

切换到 Other 页面,绘制 “Other: Hello IMGUI”。

4 扩展方式
-
在枚举处增加“页面标签”
public class MyIMGUIEditorWindow : EditorWindow { enum PageId { Basic, Other1, Other2, ... // 添加你的“页面标签” } ... }
-
增加绘制方法
public class MyIMGUIEditorWindow : EditorWindow { ... #region Basic private int i = 0; // Basic 页面中使用的变量,如果定义最好放在 Basic 块中 private void Basic() { GUILayout.Label("Basic: Hello IMGUI"); } #endregion #region Other1 private void Other1() { GUILayout.Label("Other: Hello IMGUI"); } #endregion #region Other2 // 添加你的绘制方法 private float _other2 = 0.6f; private void Other2() { GUILayout.Label("Other: Hello IMGUI"); } #endregion ... }
-
OnGUI 中增添绘制入口
public class MyIMGUIEditorWindow : EditorWindow { ... private void OnGUI() { _currentPageId = (PageId) GUILayout.Toolbar((int) _currentPageId, Enum.GetNames(typeof(PageId))); if (_currentPageId == PageId.Basic) { Basic(); } else if (_currentPageId == PageId.Other1) { Other1(); } else if (_currentPageId == PageId.Other2) // 添加绘制入口 { Other2(); } } ... }
附:整体代码
// ------------------------------------------------------------
// @file MyIMGUIEditorWindow.cs
// @brief
// @author zheliku
// @Modified 2025-04-06 03:04:51
// @Copyright Copyright (c) 2025, zheliku
// ------------------------------------------------------------
namespace EditorExtension
{
using System;
using UnityEditor;
using UnityEngine;
public class MyIMGUIEditorWindow : EditorWindow
{
enum PageId
{
Basic,
Other
}
[MenuItem("EditorExtension/02.IMGUI/02.MyIMGUIEditorWindow")]
private static void ShowWindow()
{
var window = GetWindow<MyIMGUIEditorWindow>();
window.Show();
}
private PageId _currentPageId = PageId.Basic;
private void CreateGUI()
{ }
private void OnGUI()
{
_currentPageId = (PageId) GUILayout.Toolbar((int) _currentPageId, Enum.GetNames(typeof(PageId)));
if (_currentPageId == PageId.Basic)
{
Basic();
}
else
{
Other();
}
}
#region Basic
private void Basic()
{
GUILayout.Label("Basic: Hello IMGUI");
}
#endregion
#region Other
private void Other()
{
GUILayout.Label("Other: Hello IMGUI");
}
#endregion
}
}