Unity检视面板的继承方法研究 (二)
之前做了普通对象的可继承的检视面板类, 现在想要实现对Unity自带的检视面板的继承的话, 要怎样写呢?
万变不离其宗, 仍然是围绕UnityEditor.Editor.CreateEditor 这个函数来实现:
/// <summary>
/// decorate Unity's built-in inspector Editor.
/// </summary>
public class DecoratorEditor<T> : UnityEditor.Editor where T : UnityEngine.Object
{
protected T _target;
protected UnityEditor.Editor _nativeEditor;
private static Type _inspectorEditorType = null; public virtual void OnEnable()
{
_target = target as T; if(_inspectorEditorType == null)
{
foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
// get Inspector or Editor
var tagType = assembly.GetType("UnityEditor." + typeof(T).Name + "Inspector")
?? assembly.GetType("UnityEditor." + typeof(T).Name + "Editor");
if(tagType != null)
{
_inspectorEditorType = tagType;
break;
}
}
} if(_inspectorEditorType != null)
{
_nativeEditor = UnityEditor.Editor.CreateEditor(serializedObject.targetObject, _inspectorEditorType);
}
else
{
_nativeEditor = UnityEditor.Editor.CreateEditor(serializedObject.targetObject);
}
} public override void OnInspectorGUI()
{
if(_nativeEditor)
{
_nativeEditor.OnInspectorGUI();
}
}
这里对于内置Unity Inspector的类型查找几乎是在走钢丝, 也只能碰运气了, 好在我只用来修改了一下TextureImporter的检视面板, 因为Unity2019里面没有显示spritePackingTag这个设置了, 可是这个序列化仍然存在.
#if UNITY_2019_1_OR_NEWER
[CustomEditor(typeof(TextureImporter))]
public class TextureImporterCustomEditor : DecoratorEditor<TextureImporter>
{
public override void OnInspectorGUI()
{
if(_target.textureType == TextureImporterType.Sprite)
{
_target.spritePackingTag = EditorGUILayout.TextField("Packing Tag", _target.spritePackingTag);
}
base.OnInspectorGUI();
}
}
#endif
这样我的设置又回来了.
补充 : 在重写 TextureImporter 的时候, 有一些东西是没有显示出来或是显示错乱的, 比如修改属性后的 Apply, Revert 按钮没有出现, 这就要自己去找它的原始基类里面的方法了...
#if UNITY_2019_1_OR_NEWER
[CustomEditor(typeof(TextureImporter))]
[CanEditMultipleObjects]
public class TextureImporterCustomEditor : DecoratorEditor<TextureImporter>
{
private static readonly BindingFlags MethodFlag = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod; private bool _changed = false;
private MethodInfo _apply = null;
private MethodInfo _resetValues = null; #region Mono Funcs
public override void OnEnable()
{
base.OnEnable(); _apply = _nativeEditor.GetType().GetMethod("Apply", MethodFlag);
_resetValues = _nativeEditor.GetType().GetMethod("ResetValues", MethodFlag);
}
public override void OnInspectorGUI()
{
if(_target.textureType == TextureImporterType.Sprite)
{
_target.spritePackingTag = EditorGUILayout.TextField("Packing Tag", _target.spritePackingTag);
} base.OnInspectorGUI(); if(GUI.changed)
{
_changed = true;
} if(_changed)
{
ChangedButtonGUI();
}
}
private void OnDestroy()
{
if(_changed)
{
if(CommonEditorUtils.MessageBox("Apply Changes ?"))
{
ApplyChanges();
}
else
{
RevertChanges(true);
}
}
}
#endregion #region GUI
protected void ChangedButtonGUI()
{
if(GUILayout.Button("Revert", GUILayout.MaxWidth(50.0f)))
{
RevertChanges();
}
if(GUILayout.Button("Apply", GUILayout.MaxWidth(50.0f)))
{
ApplyChanges();
}
}
#endregion #region Main Funcs
protected void ApplyChanges()
{
if(_changed)
{
_changed = false;
EditorUtility.SetDirty(_target);
if(_apply != null)
{
_apply.Invoke(_nativeEditor, null);
}
_target.SaveAndReimport();
}
}
protected void RevertChanges(bool unselectSelf = false)
{
if(_changed)
{
_changed = false;
_resetValues.Invoke(_nativeEditor, null);
}
if (unselectSelf)
{
Selection.activeObject = null;
}
}
#endregion
}
#endif
省略了一些公共代码, 用反射的方式找到基类方法去实现.
Unity检视面板的继承方法研究 (二)的更多相关文章
- Unity检视面板的继承方法研究 (一)
对于检视面板 Inspector 的面板继承方式对项目来说是很有必要的, 比如一个基类, 写了一个很好看的检视面板[CustomEditor(typeof(XXX))], 可是所有子类的面板无法直接继 ...
- 继承ViewGroup研究(汇总) 一、二、三
转载过来:为一.二.三版本. 仅供参考: 继承ViewGroup研究(1) --简介和一个小Demo 又翻开一个新篇章了,哈哈,上一回学习的是继承View,关于继承View个人感觉不是那么完美,做技术 ...
- 自定义Inspector检视面板
Unity中的Inspector面板可以显示的属性包括以下两类:(1)C#以及Unity提供的基础类型:(2)自定义类型,并使用[System.Serializable]关键字序列化,比如: [Sys ...
- Unity检测面板旋转值超过180度成负数的离奇bug
问题描述: 无意中在检视面板上对游戏物体的tansform进行旋转,结果发现旋转超过180度成负数的离奇bug 解决方案: 创建个新的unity工程,进行如上操作,一切正常…… 怀疑问题根源是配置出现 ...
- Unity UGUI图文混排源码(二)
Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...
- Unity Rain Ai 插件基本使用(二)
前言 在前面的教程中我们已经基本实现了路径导航和障碍物规避. 但是这样我们并没有让我们的角色学会思考,他只是机械的去完成一些步骤,这并不能体现Rain插件的智能. 一个角色他应该有多个不同的状态,待机 ...
- 详解Javascript的继承实现(二)
上文<详解Javascript的继承实现>介绍了一个通用的继承库,基于该库,可以快速构建带继承关系和静态成员的javascript类,好使用也好理解,额外的好处是,如果所有类都用这种库来构 ...
- .NET 扩展方法 (二)
上一篇随笔 .NET 扩展方法 (一) 已经对 扩展方法有了大致的介绍,这篇算是一个补充,让我们来看一下扩展方法的几个细节: 一.扩展方法具有继承性 当使用扩展方法扩展一个类型的时候,其也扩展了派生类 ...
- unity3d编辑器——检视面板部分(一)
最近在学习unity编辑器,so,记录总结一下. 以下介绍了一些简单的unity3d检视面板部分的使用技巧. using UnityEngine; using System.Collections; ...
随机推荐
- web的前台、后台、前端、后端
前台:呈现给用户的视觉和基本的操作.后台:用户浏览网页时,我们看不见的后台数据跑动.后台包括前端,后端.前端:对应我们写的html .javascript 等网页语言作用在前端网页.后端:对应jsp. ...
- CF-1175 B.Catch Overflow!
题目大意:有一个初始变量,值为0,三种操作 for x 一个循环的开始,循环x次 end 一个循环的结束 add 将变量值加一 问最后变量的值是否超过2^32-1,若超过,输出一串字符,不超过则输出变 ...
- Java 并发系列之十三:安全发布
1. 定义 发布对象(Publish): 使一个对象能够被当前范围之外的代码所使用 对象逸出(Escape): 一种错误的发布.当一个对象还没有构造完成时,就使它被其他线程所见 1.1 发布对象 pu ...
- 5G最新套餐以及对应限速标准
原文: http://news.mydrivers.com/1/654/654529.htm 再过两天,国内的5G就要正式运营了,中国移动.联通.电信的5G预约用户亿元超过千万,三家运营商的5G套餐费 ...
- [算法模版]Tarjan爷爷的几种图论算法
[算法模版]Tarjan爷爷的几种图论算法 前言 Tarjan爷爷发明了很多图论算法,这些图论算法有很多相似之处(其中一个就是我都不会).这里会对这三种算法进行简单介绍. 定义 强连通(strongl ...
- 树莓派4B 更新wiringPi库到2.52的方法的wiringPi库2.5.2版本wiringpi-latest.deb下载
树莓派4B 更新wiringPi库到2.52的方法 – 树莓派中文站 http://www.52pi.net/archives/1918 通过如上链接可知,需要通过如下命令下载wiringpi-lat ...
- Kubernetes SatefulSet(有状态应用部署)
Kubernetes SatefulSet(有状态应用部署) • 部署有状态应用• 解决Pod独立生命周期,保持Pod启动顺序和唯一性1. 稳定,唯一的网络标识符,持久存储2. 有序,优雅的部署和扩展 ...
- Windows Server 2008 R2 install Visual Studio 2015 failed
Please download and install Windows Server 2008 R2 Service Pack 1 (KB976932) . https://www.microsoft ...
- QT+OpenGL(02)-- zlib库的编译
1.zlib库的下载 http://www.zlib.net/ zlib1211.zip 2.解压 3.进入 zlib1211\zlib-1.2.11\contrib\vstudio\vc14 目录 ...
- MVC三层架构搭建
MVC三层架构搭建 项目主要是用三层来搭建项目,三层分为表现层,数据层和业务层.项目用了目前比较流行的IOC架构.目前流行的IoC 框架有AutoFac,Unity,Spring.NET等,项目中选用 ...