Unity编辑器拓展之六:利用反射打开Unity Preferences Window
博客迁移
个人博客站点,欢迎访问,www.jiingfengji.tech
如何利用反射打开Unity Preferences Window
Unity Preferences Window如下图所示:
作为Unity 的参数设置窗口,这个窗口支持新增项,当然本文不介绍这个内容,本次介绍如何利用反射、以及Unity Editor代码来实现用代码打开Preferences Window以及设置选中项。
使用ILSpy工具反编译UnityEditor.dll后呢,在下图目录中的PreferencesWinndow.cs脚本
里面有一个接口是"ShowPreferencesWindow",从字面上就可以理解到是用来打开PreferencesWindow的。
private static void ShowPreferencesWindow()
{EditorWindow window = EditorWindow.GetWindow<PreferencesWindow>(true, "Unity Preferences");window.minSize = new Vector2(500f, 400f);window.maxSize = new Vector2(window.minSize.x, window.maxSize.y);window.position = new Rect(new Vector2(100f, 100f), window.minSize);window.m_Parent.window.m_DontSaveToLayout = true;
}
该函数是私有的,没办法直接调,利用反射试一下。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;public class OpenPreferencesWindow : EditorWindow
{[MenuItem("Tool/OpenPreferencesWindow")]public static void Open(){OpenPreferencesWindow editor = EditorWindow.GetWindow<OpenPreferencesWindow>();}private void OnGUI(){if (GUILayout.Button("打开PerferencesWindow")){Assembly assembly = Assembly.GetAssembly(typeof(UnityEditor.EditorWindow));Type type = assembly.GetType("UnityEditor.PreferencesWindow");type.GetMethod("ShowPreferencesWindow", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, null);}}
}
效果图:
接下来,就是如何实现在打开window之后直接选中某一项。
查看一下OnEnable函数,初始化里面做了啥?
private void OnEnable()
{this.prefWinExtensions = ModuleManager.GetPreferenceWindowExtensions();this.ReadPreferences();this.m_Sections = new List<PreferencesWindow.Section>();this.m_Sections.Add(new PreferencesWindow.Section("General", new PreferencesWindow.OnGUIDelegate(this.ShowGeneral)));this.m_Sections.Add(new PreferencesWindow.Section("External Tools", new PreferencesWindow.OnGUIDelegate(this.ShowExternalApplications)));this.m_Sections.Add(new PreferencesWindow.Section("Colors", new PreferencesWindow.OnGUIDelegate(this.ShowColors)));this.m_Sections.Add(new PreferencesWindow.Section("Keys", new PreferencesWindow.OnGUIDelegate(this.ShowKeys)));this.m_Sections.Add(new PreferencesWindow.Section("GI Cache", new PreferencesWindow.OnGUIDelegate(this.ShowGICache)));this.m_Sections.Add(new PreferencesWindow.Section("2D", new PreferencesWindow.OnGUIDelegate(this.Show2D)));if (Unsupported.IsDeveloperBuild() || UnityConnect.preferencesEnabled){this.m_Sections.Add(new PreferencesWindow.Section("Unity Services", new PreferencesWindow.OnGUIDelegate(this.ShowUnityConnectPrefs)));}this.m_RefreshCustomPreferences = true;
}
与PreferencesWindow图对比之后发现,m_Sections这个集合里存的Section对象应该就是Window左侧的可选项了。
接着看看Section这个类
private class Section
{public GUIContent content;public PreferencesWindow.OnGUIDelegate guiFunc;public Section(string name, PreferencesWindow.OnGUIDelegate guiFunc){this.content = new GUIContent(name);this.guiFunc = guiFunc;}public Section(string name, Texture2D icon, PreferencesWindow.OnGUIDelegate guiFunc){this.content = new GUIContent(name, icon);this.guiFunc = guiFunc;}public Section(GUIContent content, PreferencesWindow.OnGUIDelegate guiFunc){this.content = content;this.guiFunc = guiFunc;}
}
PreferencesWindow在OnEnable的时候,就有实例化几个对象,其中传入的第一个参数,例如“Colors”,就存在类的content中,那么只需要反射到这个content的,再遍历这个m_Sections,就可以实现选中左侧了。
完整代码,如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;public class OpenPreferencesWindow : EditorWindow
{[MenuItem("Tool/OpenPreferencesWindow")]public static void Open(){OpenPreferencesWindow editor = EditorWindow.GetWindow<OpenPreferencesWindow>();}private void OnGUI(){if (GUILayout.Button("打开PerferencesWindow")){Assembly assembly = Assembly.GetAssembly(typeof(UnityEditor.EditorWindow));Type type = assembly.GetType("UnityEditor.PreferencesWindow");type.GetMethod("ShowPreferencesWindow", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, null);EditorWindow window = EditorWindow.GetWindow(type);FieldInfo sectionsField = type.GetField("m_Sections", BindingFlags.Instance | BindingFlags.NonPublic);IList sections = sectionsField.GetValue(window) as IList;Type sectionType = sectionsField.FieldType.GetGenericArguments()[0];FieldInfo sectionContentField = sectionType.GetField("content", BindingFlags.Instance | BindingFlags.Public);for (int i = 0; i < sections.Count; i++){GUIContent sectionContent = sectionContentField.GetValue(sections[i]) as GUIContent;if (sectionContent.text == "Colors"){FieldInfo sectionIndexField = type.GetField("m_SelectedSectionIndex", BindingFlags.Instance | BindingFlags.NonPublic);sectionIndexField.SetValue(window, i);return;}}}}
}
效果图:
如果插件中有在PreferencesWindow中添加项,就可以通过这个让用户快捷打开了。
通过这篇文章,希望可以帮助大家学会如何分析反编译出的代码,分析UnityEditor如何实现的编辑器,以此来写出更加实用的插件
以上知识分享,如有错误,欢迎指出,共同学习,共同进步。
Unity编辑器拓展之六:利用反射打开Unity Preferences Window相关推荐
- unity编辑器拓展整理(主要是siki的视频教程)
编辑器拓展 https://blog.csdn.net/zxl321365712/article/details/80080586 蛮牛上一个详细的博客 http://www.manew.com/th ...
- Unity编辑器拓展之三:拓展Unity的Hierarchy面板
博客迁移 个人博客站点,欢迎访问,www.jiingfengji.tech 正文 效果图: 上图中在Hierarchy右侧绘制了Toggle,Label,以及自定义的texture和Unity原声的T ...
- Unity编辑器拓展(Handles/EditorTool)
Handles Scene视图中的自定义 3D GUI 控件和绘制操作. UnityEditor.Handles - Unity 脚本 APIhttps://docs.unity.cn/cn/curr ...
- Unity编辑器拓展-写一个查看当前所有PlayerPrefsKey的窗口
创建一个PlayerPrefsKey查看器 效果图 前言 开发 一.获取数据 目录结构 二.开发编辑器窗口 效果图 思路 一.绘制搜索栏 二.定义PlayerPrefs数据结构体 三.根据搜索框过滤数 ...
- Unity编辑器拓展(Gizmos)
Gizmos 辅助图标用于协助在 Scene 视图中进行视觉调试或设置. 所有辅助图标绘图都必须在此脚本的 OnDrawGizmos 或 OnDrawGizmosSelected 函数中进行. Uni ...
- Unity编辑器拓展之五:修改脚本icon
博客迁移 个人博客站点,欢迎访问,www.jiingfengji.tech 正文 Unity修改脚本在Project面板中显示的icon有两种方法, 先看示意图: 方法1:修改脚本的meta文件 脚本 ...
- Unity编辑器拓展(一)-MenuItem的使用
MenuItem的使用 一.参数介绍 二.验证函数 三.优先级 四.快捷键 五.在Hierarchy层级窗口增加右键菜单 六.在Assets资源窗口增加右键菜单 一.参数介绍 MenuItem是一个特 ...
- Unity编辑器拓展之二十四:基于Unity Node Editor、ScriptableObject的配置化新手引导系统
博客迁移 个人博客站点,欢迎访问,www.jiingfengji.tech 本文主要介绍根据一款Node Editor库开发的新手引导系统 git地址如下: https://github.com/lu ...
- unity编辑器拓展十一——将两张RGB图合并成一张
游戏角色shader,包含的东西比较多,还要做到动态合并,程序那边希望贴图经可能少,而我们美术用了两张图,一张图的RGB是高光.流光.自发光,另外一张图的RGB是不同区域的偏色,其实每个通道的一整张贴 ...
最新文章
- vs显式导入(代码注入)依赖库
- jdk7 for Mac
- linux问题排查常用命令详解
- SAP Fiori 修改catalog group名称的技术实现
- STL 中的链表排序
- HDU-6180 Schedule
- python中的内建函数
- date java format_java-DateFormat
- 在sap系统设置纸张打印格式(针式打印机)
- Antd Card study
- 腾讯是如何一刀刀,在15年间干死那些竞争对手的?
- 20191101(33) 针对 RT-Thread 下 ADS1256 移植说明(SPI)
- 大数据带来了哪些改变
- ssr服务器网站,ssr(服务端渲染)
- python中axis的理解
- fastboot常用指令
- Android 经典原生壁纸,福利:精选安卓之父手机原生壁纸 曾力抗苹果华为 如今仅剩经典!...
- diskgenius 数据迁移_如何无损将UEFI格式的系统从SATA盘迁移到NVME SSD中
- 小山村里的百寿宴:家家户户共享绿色福祉
- canvas塔防小游戏-保卫大司马