注意
:考虑到编辑器扩展的内容比较多,我将编辑器扩展的内容分开,并全部整合放在【unity游戏开发------编辑器扩展】专栏里,感兴趣的小伙伴可以前往逐一查看学习。
文章目录
- 前言
- 一、确认弹窗
- 二、进度条
- 三、文件夹操作
-
- 1、选择现有文件夹
-
- [1.1 介绍](#1.1 介绍)
- [1.2 示例](#1.2 示例)
- 2、选择或者创建某个文件夹
-
- [2.1 介绍](#2.1 介绍)
- [2.2 示例](#2.2 示例)
- 四、文件操作
-
- 1、打开现有文件
-
- [1.1 介绍](#1.1 介绍)
- [1.2 示例](#1.2 示例)
- 2、保存或者覆盖文件
-
- [2.1 介绍](#2.1 介绍)
- [2.2 示例](#2.2 示例)
- [3、保存或者覆盖 Unity 资源文件](#3、保存或者覆盖 Unity 资源文件)
-
- [3.1 介绍](#3.1 介绍)
- [3.2 示例](#3.2 示例)
- 五、其他
-
- [1、将 Texture2D 纹理压缩为指定的纹理格式](#1、将 Texture2D 纹理压缩为指定的纹理格式)
- 2、查找指定对象所依赖的所有资源
-
- [2.1 介绍](#2.1 介绍)
- [2.2 示例](#2.2 示例)
- 专栏推荐
- 完结
前言
EditorUtility 是 Unity 编辑器中的一个工具类,专门用于编辑器脚本开发,提供了一系列辅助功能(如文件操作、进度条、弹窗等),帮助简化自定义编辑器功能的实现。
官方文档:EditorUtility
一、确认弹窗
1、确认弹窗
1.1 主要API
csharp
EditorUtility.DisplayDialog("标题", "显示信息", "确定键名");
注意:窗口显示会阻塞逻辑 即一定要对提示窗口做处理后才会显示其他逻辑
1.2 示例
csharp
using UnityEditor;
using UnityEngine;
public class TestEditorUtilityWindow : EditorWindow
{
[MenuItem("编辑器拓展/自定义窗口拓展/EditorUtility窗口拓展")]
private static void OpenWindow()
{
TestEditorUtilityWindow win = EditorWindow.GetWindow<TestEditorUtilityWindow>();
win.Show();
}
private void OnGUI()
{
if(GUILayout.Button("确认消息窗口"))
{
if(EditorUtility.DisplayDialog("确认弹窗", "确定要进行该操作吗?", "确认"))
{
Debug.Log("点击确认");
}
else
{
Debug.Log("点击取消");
}
Debug.Log("执行完毕");
}
}
}
效果
2、三按钮弹窗
2.1 主要API
csharp
int EditorUtility.DisplayDialogComplex("标题", "显示信息", "按钮1名字", "取消按钮名字", "按钮2名字");
返回值
-
0-按钮1按下
-
1-取消按钮按下
-
2-按钮2按下
2.2 示例
csharp
using UnityEditor;
using UnityEngine;
public class TestEditorUtilityWindow : EditorWindow
{
[MenuItem("编辑器拓展/自定义窗口拓展/EditorUtility窗口拓展")]
private static void OpenWindow()
{
TestEditorUtilityWindow win = EditorWindow.GetWindow<TestEditorUtilityWindow>();
win.Show();
}
private void OnGUI()
{
if (GUILayout.Button("三按钮确认弹窗"))
{
int result = EditorUtility.DisplayDialogComplex("三按钮确认弹窗", "确定要进行该操作吗?", "选项1", "取消", "选项2");
switch (result)
{
case 0:
Debug.Log("选项1被按下了");
break;
case 1:
Debug.Log("取消被按下了");
break;
case 2:
Debug.Log("选项2被按下了");
break;
default:
break;
}
Debug.Log("三按钮确认弹窗显示完毕");
}
}
}
效果
二、进度条
1、主要API
-
显示进度条
csharpEditorUtility.DisplayProgressBar("进度条", "显示信息", 进制值0~1);
-
关闭进度条
csharpEditorUtility.ClearProgressBar();
注意
:进度条窗口不会卡逻辑,但是需要配合关闭进度条使用
2、示例
csharp
using UnityEditor;
using UnityEngine;
public class TestEditorUtilityWindow : EditorWindow
{
float value;
[MenuItem("编辑器拓展/自定义窗口拓展/EditorUtility窗口拓展")]
private static void OpenWindow()
{
TestEditorUtilityWindow win = EditorWindow.GetWindow<TestEditorUtilityWindow>();
win.Show();
}
private void OnGUI()
{
if(GUILayout.Button("显示更新进度条"))
{
//每次点击加进度条进度
value += 0.1f;
EditorUtility.DisplayProgressBar("进度条标题", "进度条窗口显示内容", value);
Debug.Log("进度条窗口显示完毕");
}
if(GUILayout.Button("关闭进度条"))
{
value = 0;
EditorUtility.ClearProgressBar();
}
}
}
效果
三、文件夹操作
1、选择现有文件夹
1.1 介绍
选择现有文件夹(仅能选择已经存在的目录)
csharp
string path = EditorUtility.OpenFolderPanel("窗口标题", "初始打开的文件夹路径", "默认选中的文件夹名称");
返回带上文件夹的完整路径,点击取消返回空字符串
1.2 示例
csharp
using UnityEditor;
using UnityEngine;
public class TestEditorUtilityWindow : EditorWindow
{
[MenuItem("编辑器拓展/自定义窗口拓展/EditorUtility窗口拓展")]
private static void OpenWindow()
{
TestEditorUtilityWindow win = EditorWindow.GetWindow<TestEditorUtilityWindow>();
win.Show();
}
private void OnGUI()
{
if (GUILayout.Button("显示打开文件夹面板"))
{
string str4 = EditorUtility.OpenFolderPanel("得到一个文件路径", Application.dataPath, "");
if (str4 != "")
{
Debug.Log(str4);
}
}
}
}
效果

2、选择或者创建某个文件夹
2.1 介绍
选择或新建文件夹(允许输入新文件夹名称并创建)
csharp
string path = EditorUtility.SaveFolderPanel("窗口标题", "初始打开的文件夹路径", "默认选中的文件夹名称");
返回带上文件夹的完整路径,点击取消返回空字符串
2.2 示例
csharp
using UnityEditor;
using UnityEngine;
public class TestEditorUtilityWindow : EditorWindow
{
[MenuItem("编辑器拓展/自定义窗口拓展/EditorUtility窗口拓展")]
private static void OpenWindow()
{
TestEditorUtilityWindow win = EditorWindow.GetWindow<TestEditorUtilityWindow>();
win.Show();
}
private void OnGUI()
{
if (GUILayout.Button("打开某个文件夹"))
{
string str3 = EditorUtility.SaveFolderPanel("窗口标题", Application.dataPath, "Editor");
Debug.Log(str3);
}
}
}
效果
四、文件操作
1、打开现有文件
1.1 介绍
选择现有文件(仅能选择已经存在的文件)
csharp
string path = EditorUtility.OpenFilePanel("窗口标题", "文件路径", "后缀格式");
1.2 示例
csharp
using System.IO;
using UnityEditor;
using UnityEngine;
public class TestEditorUtilityWindow : EditorWindow
{
[MenuItem("编辑器拓展/自定义窗口拓展/EditorUtility窗口拓展")]
private static void OpenWindow()
{
TestEditorUtilityWindow win = EditorWindow.GetWindow<TestEditorUtilityWindow>();
win.Show();
}
private void OnGUI()
{
if (GUILayout.Button("选择某个文件"))
{
string str4 = EditorUtility.OpenFilePanel("窗口标题", Application.dataPath, "txt");
// 会得到带上文件名的存储路径,可以利用路径读取资源
Debug.Log(str4);
if (str4 != "")
{
string txt = File.ReadAllText(str4);
Debug.Log(txt);
}
}
}
}
效果
2、保存或者覆盖文件
2.1 介绍
保存或覆盖文件(允许输入新文件名称并创建)
csharp
string path = EditorUtility.SaveFilePanel("窗口标题", "打开的目录", "保存的文件的名称", "文件后缀格式")
2.2 示例
csharp
using System.IO;
using UnityEditor;
using UnityEngine;
public class TestEditorUtilityWindow : EditorWindow
{
[MenuItem("编辑器拓展/自定义窗口拓展/EditorUtility窗口拓展")]
private static void OpenWindow()
{
TestEditorUtilityWindow win = EditorWindow.GetWindow<TestEditorUtilityWindow>();
win.Show();
}
private void OnGUI()
{
if (GUILayout.Button("打开文件存储面板"))
{
string str = EditorUtility.SaveFilePanel("保存我的文件", Application.dataPath, "测试文件", "txt");
// 会得到带上文件名的存储路径,可以利用路径写入
Debug.Log(str);
if (str != "") File.WriteAllText(str, "内容");
}
}
}
效果
3、保存或者覆盖 Unity 资源文件
3.1 介绍
保存或覆盖文件(允许输入新文件名称并创建)。但是仅限Asset项目内,这是与 SaveFilePanel 的主要区别。返回相对路径,从Asset开始拼接的文件路径
csharp
string path = EditorUtility.SaveFilePanelInProject("窗口标题", "保存的文件的名称", "后缀格式", "在对话框窗口中显示的文本摘要");
3.2 示例
csharp
using System.IO;
using UnityEditor;
using UnityEngine;
public class TestEditorUtilityWindow : EditorWindow
{
[MenuItem("编辑器拓展/自定义窗口拓展/EditorUtility窗口拓展")]
private static void OpenWindow()
{
TestEditorUtilityWindow win = EditorWindow.GetWindow<TestEditorUtilityWindow>();
win.Show();
}
private void OnGUI()
{
// 显示文件存储面板(默认为工程目录中)
if (GUILayout.Button("限制在当前 Unity 项目目录内选择或指定保存位置"))
{
string str2 = EditorUtility.SaveFilePanelInProject("窗口标题", "测试文件2", "txt", "要在对话窗口中显示的文本摘要");
// 只会从Asset开始拼接路径
Debug.Log(str2);
if (str2 != "") File.WriteAllText(str2, "内容2");
}
}
}
效果
五、其他
1、将 Texture2D 纹理压缩为指定的纹理格式
EditorUtility.CompressTexture
是 Unity 编辑器中的一个静态方法,用于显式地将 Texture2D 纹理压缩为指定的纹理格式。
csharp
public static void CompressTexture(
Texture2D texture,
TextureFormat format,
TextureCompressionQuality quality
);
参数说明
-
texture (Texture2D)
: 需要压缩的纹理对象 -
format (TextureFormat)
: 目标压缩格式- 常用格式: TextureFormat.DXT1 , TextureFormat.DXT5 , TextureFormat.ETC2_RGBA8 , TextureFormat.ASTC_4x4 等
-
quality (TextureCompressionQuality)
: 压缩质量-
TextureCompressionQuality.Fast: 快速压缩,质量较低
-
TextureCompressionQuality.Normal: 正常压缩
-
TextureCompressionQuality.Best: 最佳质量,速度最慢
-
该知识点会配合之后的资源导入相关知识点使用
2、查找指定对象所依赖的所有资源
2.1 介绍
EditorUtility.CompressTexture
是 Unity 编辑器中的一个静态方法,,用于查找指定对象所依赖的所有资源
csharp
object[] EditorUtility.CollectDependencies(Object[] roots);
返回一个包含所有输入对象及其所有依赖项的 Object[] 数组。
2.2 示例
csharp
using UnityEditor;
using UnityEngine;
public class TestEditorUtilityWindow : EditorWindow
{
GameObject obj;
[MenuItem("编辑器拓展/自定义窗口拓展/EditorUtility窗口拓展")]
private static void OpenWindow()
{
TestEditorUtilityWindow win = EditorWindow.GetWindow<TestEditorUtilityWindow>();
win.Show();
}
private void OnGUI()
{
obj = EditorGUILayout.ObjectField("想要查找关联资源的对象", obj, typeof(GameObject), true) as GameObject;
if (GUILayout.Button("检索依赖资源") && obj != null)
{
Object[] objs = EditorUtility.CollectDependencies(new Object[] { obj });
// 用Selection类选中所有依赖的资源
Selection.objects = objs;
}
}
}
效果
专栏推荐
完结
好了,我是向宇
,博客地址:https://xiangyu.blog.csdn.net,如果学习过程中遇到任何问题,也欢迎你评论私信找我。
赠人玫瑰,手有余香!如果文章内容对你有所帮助,请不要吝啬你的点赞评论和关注
,你的每一次支持
都是我不断创作的最大动力。当然如果你发现了文章中存在错误
或者有更好的解决方法
,也欢迎评论私信告诉我哦!