Lesson12 - EditorGUIUtility 工具类

EditorGUIUtility 概述

EditorGUIUtility 是 EditorGUI 的实用工具类,提供编辑器拓展开发中常用的辅助功能,如资源加载、对象选择、事件传递等。

资源加载 - Editor Default Resources

创建特殊文件夹

在 Assets 下创建 Editor Default Resources 文件夹(注意:文件夹名称必须完全相同,包括大小写和空格)。这个文件夹中的资源可以被 EditorGUIUtility 加载。

加载资源(不存在返回 null)

1
Texture img = EditorGUIUtility.Load("HelloWorld.png") as Texture;

如果资源不存在,返回 null。必须包含文件后缀名。

加载资源(不存在报错)

1
Texture img = EditorGUIUtility.LoadRequired("HelloWorld.png") as Texture;

如果资源不存在会直接报错。用于必须存在的资源。

对象选择器(Object Picker)

打开对象选择窗口

1
EditorGUIUtility.ShowObjectPicker<Texture>(null, false, "Editor", 0);

参数说明:

  • 默认被选中的对象(null 表示无)
  • 是否允许选择场景中的对象(false 只能选择项目资源)
  • 名称过滤(如 “Editor” 表示文件名包含 Editor 的会显示)
  • controlID(通常写 0)

监听选择事件

选择窗口打开后,通过事件监听获取用户的选择结果。

1
2
3
4
5
6
7
8
9
Event currentEvent = Event.current;

if (currentEvent.commandName == "ObjectSelectorUpdated") {
Texture selected = EditorGUIUtility.GetObjectPickerObject() as Texture;
Debug.Log("选择了:" + selected.name);
}
else if (currentEvent.commandName == "ObjectSelectorClosed") {
Debug.Log("选择窗口已关闭");
}

重要:需要在每次事件检查时缓存 Event.current,避免在布局计算期间多次访问导致布局错误。

正确做法:

1
2
3
4
Event currentEvent = Event.current;
if (currentEvent.commandName == "ObjectSelectorUpdated") {
// 处理选择
}

对象高亮提示

PingObject

1
EditorGUIUtility.PingObject(gameObject);

在 Project 窗口或 Hierarchy 中高亮显示指定对象,用于提示用户某个对象的位置。

窗口间事件传递

发送自定义事件

从一个窗口向另一个窗口发送自定义事件。

1
2
3
4
5
6
7
8
// 创建事件
Event customEvent = EditorGUIUtility.CommandEvent("我是自定义事件");

// 获取目标窗口
MyWindow targetWindow = EditorWindow.GetWindow<MyWindow>();

// 发送事件
targetWindow.SendEvent(customEvent);

接收事件

在目标窗口中监听事件。

1
2
3
4
5
6
7
8
9
private void OnGUI() {
Event currentEvent = Event.current;

if (currentEvent.type == EventType.ExecuteCommand) {
if (currentEvent.commandName == "我是自定义事件") {
Debug.Log("收到自定义事件");
}
}
}

发送事件时,如果允许一个窗口有多个可以用 CreateWindow API 创建窗口。

绘制色板(Color Swatch)

绘制颜色块

1
EditorGUIUtility.DrawColorSwatch(new Rect(0, 0, 100, 100), color);

在指定的矩形区域内绘制一个颜色块,通常配合 ColorField 使用。

绘制曲线(Curve Swatch)

绘制曲线预览

1
2
3
4
5
6
7
EditorGUIUtility.DrawCurveSwatch(
new Rect(0, 0, 100, 100), // 绘制区域
curve, // AnimationCurve 对象
null, // SerializedProperty(通常为 null)
Color.white, // 曲线颜色
Color.black // 背景颜色
);

通常配合 CurveField 使用,在界面上预览曲线。

坐标转换

屏幕坐标与 GUI 坐标转换

1
2
3
4
5
6
7
8
9
// GUI 坐标转屏幕坐标
Vector2 screenPoint = EditorGUIUtility.GUIToScreenPoint(guiPoint);

// 屏幕坐标转 GUI 坐标
Vector2 guiPoint = EditorGUIUtility.ScreenToGUIPoint(screenPoint);

// 矩形区域转换
Rect screenRect = EditorGUIUtility.GUIToScreenRect(guiRect);
Rect guiRect = EditorGUIUtility.ScreenToGUIRect(screenRect);
  • 屏幕坐标系:原点在屏幕左上角
  • GUI 坐标系:原点在当前窗口左上角

鼠标光标

指定区域设置鼠标光标

1
EditorGUIUtility.AddCursorRect(new Rect(x, y, width, height), MouseCursor.Link);

常用 MouseCursor 枚举值:

  • Arrow:普通箭头
  • Text:文本光标
  • ResizeVertical:垂直调整
  • ResizeHorizontal:水平调整
  • Link:链接光标
  • MoveArrow:移动光标
  • RotateArrow:旋转光标
  • Pan:平移光标(手形)