当前位置: 首页 > ai >正文

【unity游戏开发——编辑器扩展】EditorUtility编辑器工具类实现如文件操作、进度条、弹窗等操作

注意:考虑到编辑器扩展的内容比较多,我将编辑器扩展的内容分开,并全部整合放在【unity游戏开发——编辑器扩展】专栏里,感兴趣的小伙伴可以前往逐一查看学习。

文章目录

  • 前言
  • 一、确认弹窗
    • 1、确认弹窗
      • 1.1 主要API
      • 1.2 示例
    • 2、三按钮弹窗
      • 2.1 主要API
      • 2.2 示例
  • 二、进度条
    • 1、主要API
    • 2、示例
  • 三、文件夹操作
    • 1、选择现有文件夹
      • 1.1 介绍
      • 1.2 示例
    • 2、选择或者创建某个文件夹
      • 2.1 介绍
      • 2.2 示例
  • 四、文件操作
    • 1、打开现有文件
      • 1.1 介绍
      • 1.2 示例
    • 2、保存或者覆盖文件
      • 2.1 介绍
      • 2.2 示例
    • 3、保存或者覆盖 Unity 资源文件
      • 3.1 介绍
      • 3.2 示例
  • 五、其他
    • 1、将 Texture2D 纹理压缩为指定的纹理格式
    • 2、查找指定对象所依赖的所有资源
      • 2.1 介绍
      • 2.2 示例
  • 专栏推荐
  • 完结

前言

EditorUtility 是 Unity 编辑器中的一个工具类,专门用于编辑器脚本开发,提供了一系列辅助功能(如文件操作、进度条、弹窗等),帮助简化自定义编辑器功能的实现。

官方文档:EditorUtility

一、确认弹窗

1、确认弹窗

1.1 主要API

EditorUtility.DisplayDialog(“标题”, “显示信息”, “确定键名”);

注意:窗口显示会阻塞逻辑 即一定要对提示窗口做处理后才会显示其他逻辑

1.2 示例

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

int EditorUtility.DisplayDialogComplex(“标题”, “显示信息”, “按钮1名字”, “取消按钮名字”, “按钮2名字”);

返回值
- 0-按钮1按下
- 1-取消按钮按下
- 2-按钮2按下

2.2 示例

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

  • 显示进度条

    EditorUtility.DisplayProgressBar(“进度条”, “显示信息”, 进制值0~1);
    
  • 关闭进度条

    EditorUtility.ClearProgressBar();
    

注意:进度条窗口不会卡逻辑,但是需要配合关闭进度条使用

2、示例

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 介绍

选择现有文件夹(仅能选择已经存在的目录)

string path = EditorUtility.OpenFolderPanel(“窗口标题”, “初始打开的文件夹路径”, “默认选中的文件夹名称”);

返回带上文件夹的完整路径,点击取消返回空字符串

1.2 示例

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 介绍

选择或新建文件夹(允许输入新文件夹名称并创建)

string path = EditorUtility.SaveFolderPanel(“窗口标题”, “初始打开的文件夹路径”, “默认选中的文件夹名称”);

返回带上文件夹的完整路径,点击取消返回空字符串

2.2 示例

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 介绍

选择现有文件(仅能选择已经存在的文件)

string path = EditorUtility.OpenFilePanel(“窗口标题”, “文件路径”, “后缀格式”);

1.2 示例

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 介绍

保存或覆盖文件(允许输入新文件名称并创建)

string path = EditorUtility.SaveFilePanel(“窗口标题”, “打开的目录”, “保存的文件的名称”, “文件后缀格式”)

2.2 示例

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开始拼接的文件路径

string path = EditorUtility.SaveFilePanelInProject(“窗口标题”, “保存的文件的名称”, “后缀格式”, “在对话框窗口中显示的文本摘要”);

3.2 示例

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 纹理压缩为指定的纹理格式。

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 编辑器中的一个静态方法,,用于查找指定对象所依赖的所有资源

object[] EditorUtility.CollectDependencies(Object[] roots);

返回一个包含所有输入对象及其所有依赖项的 Object[] 数组。

2.2 示例

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;}}
}

效果
在这里插入图片描述


专栏推荐

地址
【unity游戏开发入门到精通——C#篇】
【unity游戏开发入门到精通——unity通用篇】
【unity游戏开发入门到精通——unity3D篇】
【unity游戏开发入门到精通——unity2D篇】
【unity实战】
【制作100个Unity游戏】
【推荐100个unity插件】
【实现100个unity特效】
【unity框架/工具集开发】
【unity游戏开发——模型篇】
【unity游戏开发——InputSystem】
【unity游戏开发——Animator动画】
【unity游戏开发——UGUI】
【unity游戏开发——联网篇】
【unity游戏开发——优化篇】
【unity游戏开发——shader篇】
【unity游戏开发——编辑器扩展】

完结

好了,我是向宇,博客地址:https://xiangyu.blog.csdn.net,如果学习过程中遇到任何问题,也欢迎你评论私信找我。

赠人玫瑰,手有余香!如果文章内容对你有所帮助,请不要吝啬你的点赞评论和关注,你的每一次支持都是我不断创作的最大动力。当然如果你发现了文章中存在错误或者有更好的解决方法,也欢迎评论私信告诉我哦!
在这里插入图片描述

http://www.xdnf.cn/news/9430.html

相关文章:

  • 计算机网络学习20250528
  • (增强)基于sqlite、mysql、redis的消息存储
  • OpenCV---Canny边缘检测
  • 在 CAD C# 二次开发中,Clipper2、CGAL 和 NTS(NetTopologySuite)对比
  • 上交具身机器人的视觉运动导航!HTSCN:融合空间记忆与语义推理认知的导航策略
  • 11.14 LangGraph检查点系统实战:AI Agent会话恢复率提升287%的企业级方案
  • cuda编程笔记(2)--传递参数、设备属性
  • RabbitMQ监控:关键技术、技巧与最佳实践
  • 【华为战报】4月、5月 HCIP考试战报!
  • 理解并解决高丢包率问题,构建清晰流畅的实时音视频通话
  • 硬件实时时钟(RTC)
  • java调用C语言的dll方法
  • JWT安全:假密钥.【签名随便写实现越权绕过.】
  • PHP+MySQL开发语言 在线下单订水送水小程序源码及搭建指南
  • TypeScript 中的剩余参数:灵活处理可变数量参数
  • Prometheus + Grafana 监控常用服务
  • 《Scientific Reports撤稿门技术节分析》——从图像篡改检测到学术伦理重建的技术透视
  • Golang | gRPC索引服务
  • HTTP协议接口三种测试方法之-JMeter(保姆教程)
  • 大模型在先天性肌性斜颈诊疗全流程中的应用研究报告
  • Flink SQL 编程详解:从入门到实战难题与解决方案
  • 论文笔记:Towards Explainable Traffic Flow Prediction with Large Language Models
  • 查询oracle进程数和会话数进行优化
  • Gemini Pro 2.5 输出
  • P2014 [CTSC1997] 选课
  • 53、用例(Use Case)详解
  • 封装索引列表
  • AXI3、AXI4 和 AXI5 的详细差异对比
  • 第三章、运动学逆解(双足轮根据腿高求舵机角度)
  • 完全卸载VS Code--Windows版