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

Unity编辑器扩展:创建一个欢迎窗口,在启动Editor的时候显示自定义窗口。

Unity编辑器扩展:创建一个欢迎窗口,在启动Editor的时候显示自定义窗口。

在Unity开发过程中,经常会遇到需要向其他人展示重要信息的情况,比如项目文档、脚本说明、插件介绍等。这个窗口不仅能够展示必要的文档信息,还提供了便捷的交互功能,如打印文本以及欢迎控制窗口的显示与否。

实现效果展示

在这里插入图片描述

可以手动打开窗口
在这里插入图片描述

内容切换,点击顶部的按钮即可切换内容。

实现思路

UI编辑器,再启动编辑器的时候显示窗口。
点击按钮会读取指定路径下的文件并展示。

的的

教程

创建文件夹
\Assets\Editor\WelcomeWindow,创建脚本:ReadmeEditor.cs,将下面的代码粘贴进去。

一、设计目标:

  1. 创建一个简洁直观的界面,用于展示项目文档、脚本说明、插件介绍等内容。
  2. 提供用户友好的交互方式,允许用户选择显示不同的文本内容。
  3. (目前不允许用户复制当前显示的文本到剪贴板),并在控制台中输出。
  4. 用户可以选择是否在下次启动编辑器时自动显示该窗口。

二、功能实现:

  1. 文本加载: 使用File.ReadAllText方法从指定路径加载文本文件,并将其内容存储在字符串变量中。这样可以灵活地更新文档而无需重新编译代码。
  2. 用户界面: 利用Unity编辑器的GUI系统,创建了一个垂直布局,其中包含按钮和文本显示区域。按钮用于切换显示不同类型的文档内容。
  3. 复制和控制台输出: 当用户点击“Copy”按钮时,使用Debug.Log将当前文本内容输出到Unity控制台,并使用GUIUtility.systemCopyBuffer将文本复制到剪贴板。
  4. 显示控制: 我们使用Unity的SessionState来控制窗口是否在下次启动编辑器时显示。用户可以选择Don't show againShow again,并在相应的按钮点击时更新状态。

实现源码

using UnityEngine;
using UnityEditor;
using System.IO;namespace wuhuEditorExpand
{
[InitializeOnLoad]
public class ReadmeEditor : EditorWindow
{static string s_ShowedReadmeSessionStateName = "ReadmeEditor.showedReadme";static ReadmeEditor(){EditorApplication.delayCall += SelectReadmeAutomatically;}static void SelectReadmeAutomatically(){// 检查是否已经显示过该窗口if (!SessionState.GetBool(s_ShowedReadmeSessionStateName, false)){ShowHelloWindow();SessionState.SetBool(s_ShowedReadmeSessionStateName, true);}}static void ShowHelloWindow(){HelloWindow.ShowWindow();}
}public class HelloWindow : EditorWindow
{private static bool s_DontShowAgain = false;private string noticeFileContent = "";private string pluginFileContent = "";private string scriptFileContent = "";private string thanksFileContent = "";private static string windowsFormName = "Welcome Window";private string noticeFilePath = "Assets/Editor/ReadmeWindow/notice.txt";private string pluginFilePath = "Assets/Editor/ReadmeWindow/plugin.txt";private string scriptFilePath = "Assets/Editor/ReadmeWindow/script.txt";private string thanksFilePath = "Assets/Editor/ReadmeWindow/thanks.txt";private string currentContent = "";[MenuItem("Window/Welcome Window")]public static void ShowWindow(){GetWindow<HelloWindow>(windowsFormName);}void OnGUI(){if (noticeFileContent == "" || pluginFileContent == "" || scriptFileContent == "" || thanksFileContent == ""){noticeFileContent = LoadFileContent(noticeFilePath);pluginFileContent = LoadFileContent(pluginFilePath);scriptFileContent = LoadFileContent(scriptFilePath);thanksFileContent = LoadFileContent(thanksFilePath);currentContent = "1. Readme是用前须知\n" +"2. Scripts 脚本介绍\n"+"3. Plugins引用的插件介绍\n"+"4. 致谢\n"+"5. Don't show again 下次启动Editor的时候不显示\n"+"6. Show again 下载启动Editor的时候显示\n"+"7.Copy 拷贝当前显示的内容,并输出于控制台\n"+"-----end-----";}EditorGUILayout.BeginVertical();// 第一排按钮EditorGUILayout.BeginHorizontal();var buttonStyle = new GUIStyle(GUI.skin.button);buttonStyle.fontSize = 14; // 设置字体大小if (GUILayout.Button("Readme", buttonStyle, GUILayout.Height(30))){currentContent = noticeFileContent;}if (GUILayout.Button("Scripts", buttonStyle, GUILayout.Height(30))){currentContent = scriptFileContent;}if (GUILayout.Button("Plugins", buttonStyle, GUILayout.Height(30))){currentContent = pluginFileContent;}var buttonStylethx = new GUIStyle(GUI.skin.button);buttonStylethx.normal.textColor = Color.blue;buttonStylethx.fontSize = 16;if (GUILayout.Button("致谢", buttonStylethx, GUILayout.Height(30))){currentContent = thanksFileContent;}EditorGUILayout.EndHorizontal();EditorGUILayout.Space();// 底部文本区域EditorGUILayout.LabelField(currentContent, EditorStyles.textArea, GUILayout.Height(position.height - 90));EditorGUILayout.EndVertical();// 最下方显示 "Don't show again" 按钮EditorGUILayout.BeginHorizontal();var style = new GUIStyle(GUI.skin.button);style.normal.textColor = Color.white;if (GUILayout.Button("Don't show again", style, GUILayout.Width(position.width * 0.7f), GUILayout.Height(30))){s_DontShowAgain = true;Close();}if (GUILayout.Button("Show again", style, GUILayout.Width(position.width * 0.2f), GUILayout.Height(30))){s_DontShowAgain = true;Close();}if (GUILayout.Button("Copy", style, GUILayout.Width(position.width * 0.1f), GUILayout.Height(30))){Debug.Log("拷贝到剪贴板");Debug.Log(currentContent);}EditorGUILayout.EndHorizontal();}private string LoadFileContent(string _filePath){if (File.Exists(_filePath)){try{return File.ReadAllText(_filePath);}catch (System.Exception e){Debug.LogError("文件读取失败Error reading file: " + _filePath);return "没有读取到文件" + _filePath;}}else{Debug.LogError("文件没有找到File not found: " + _filePath);return "文件没有找到" + _filePath;}}
}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【信息学奥赛一本通】1008:计算(a+b)/c的值
  • easypoi模板导出word多页导出加强版
  • 5 分钟 Stable Diffusion 本地安装指南
  • Android14 蓝牙设备类型修改
  • 本地Docker部署Navidrome音乐服务器与远程访问听歌详细教程
  • 根据状态的不同,显示不同的背景颜色
  • 数据库学习
  • 动手实现基于Reactor模型的高并发Web服务器(一):epoll+多线程版本
  • 制作docker镜像
  • 打卡51天------图论(深搜/广搜应用题)
  • OpenCV图像滤波(Image Filtering)常用函数及其用法详解
  • CART决策树-基尼指数(全网最详解)
  • 克服编程学习中的挫折感:从心态到策略的全方位指南
  • Jenkins汉化配置详解
  • Maven继承和聚合特性
  • 「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • Java面向对象及其三大特征
  • js 实现textarea输入字数提示
  • Lsb图片隐写
  • Mybatis初体验
  • vue学习系列(二)vue-cli
  • #我与Java虚拟机的故事#连载18:JAVA成长之路
  • (01)ORB-SLAM2源码无死角解析-(56) 闭环线程→计算Sim3:理论推导(1)求解s,t
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (Oracle)SQL优化基础(三):看懂执行计划顺序
  • (笔记)Kotlin——Android封装ViewBinding之二 优化
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (二)WCF的Binding模型
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (三)mysql_MYSQL(三)
  • (十) 初识 Docker file
  • (十八)用JAVA编写MP3解码器——迷你播放器
  • (五)c52学习之旅-静态数码管
  • (转载)Linux网络编程入门
  • .NET Framework与.NET Framework SDK有什么不同?
  • .net 获取url的方法
  • .net开发时的诡异问题,button的onclick事件无效
  • .Net通用分页类(存储过程分页版,可以选择页码的显示样式,且有中英选择)
  • .NET未来路在何方?
  • .net用HTML开发怎么调试,如何使用ASP.NET MVC在调试中查看控制器生成的html?
  • @GetMapping和@RequestMapping的区别
  • @四年级家长,这条香港优才计划+华侨生联考捷径,一定要看!
  • [8] CUDA之向量点乘和矩阵乘法
  • [ACM独立出版]2024年虚拟现实、图像和信号处理国际学术会议(ICVISP 2024)
  • [ASP]青辰网络考试管理系统NES X3.5
  • [Bada开发]初步入口函数介绍
  • [C#]科学计数法(scientific notation)显示为正常数字
  • [Codeforces1137D]Cooperative Game
  • [Django开源学习 1]django-vue-admin
  • [IOI2018] werewolf 狼人
  • [JAVA设计模式]第二部分:创建模式
  • [leetcode]Symmetric Tree
  • [MQTT]服务器EMQX搭建SSL/TLS连接过程(wss://)
  • [NISACTF 2022]level-up