之前做了普通对象的可继承的检视面板类, 现在想要实现对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检视面板的继承方法研究 (二)的更多相关文章

  1. Unity检视面板的继承方法研究 (一)

    对于检视面板 Inspector 的面板继承方式对项目来说是很有必要的, 比如一个基类, 写了一个很好看的检视面板[CustomEditor(typeof(XXX))], 可是所有子类的面板无法直接继 ...

  2. 继承ViewGroup研究(汇总) 一、二、三

    转载过来:为一.二.三版本. 仅供参考: 继承ViewGroup研究(1) --简介和一个小Demo 又翻开一个新篇章了,哈哈,上一回学习的是继承View,关于继承View个人感觉不是那么完美,做技术 ...

  3. 自定义Inspector检视面板

    Unity中的Inspector面板可以显示的属性包括以下两类:(1)C#以及Unity提供的基础类型:(2)自定义类型,并使用[System.Serializable]关键字序列化,比如: [Sys ...

  4. Unity检测面板旋转值超过180度成负数的离奇bug

    问题描述: 无意中在检视面板上对游戏物体的tansform进行旋转,结果发现旋转超过180度成负数的离奇bug 解决方案: 创建个新的unity工程,进行如上操作,一切正常…… 怀疑问题根源是配置出现 ...

  5. Unity UGUI图文混排源码(二)

    Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...

  6. Unity Rain Ai 插件基本使用(二)

    前言 在前面的教程中我们已经基本实现了路径导航和障碍物规避. 但是这样我们并没有让我们的角色学会思考,他只是机械的去完成一些步骤,这并不能体现Rain插件的智能. 一个角色他应该有多个不同的状态,待机 ...

  7. 详解Javascript的继承实现(二)

    上文<详解Javascript的继承实现>介绍了一个通用的继承库,基于该库,可以快速构建带继承关系和静态成员的javascript类,好使用也好理解,额外的好处是,如果所有类都用这种库来构 ...

  8. .NET 扩展方法 (二)

    上一篇随笔 .NET 扩展方法 (一) 已经对 扩展方法有了大致的介绍,这篇算是一个补充,让我们来看一下扩展方法的几个细节: 一.扩展方法具有继承性 当使用扩展方法扩展一个类型的时候,其也扩展了派生类 ...

  9. unity3d编辑器——检视面板部分(一)

    最近在学习unity编辑器,so,记录总结一下. 以下介绍了一些简单的unity3d检视面板部分的使用技巧. using UnityEngine; using System.Collections; ...

随机推荐

  1. springmvc+strut2比较

    常见web框架中Struts2和SpringMVC独占鳌头,SpringMVC和Struts有什么不同? 我们可以从各个方面进行对比: 一:框架的思想设计上 SpringMVC控制器是基于方法上拦截, ...

  2. Pwn-level2

    题目地址 https://dn.jarvisoj.com/challengefiles/level2.54931449c557d0551c4fc2a10f4778a1 先看一下文件的属性   32位 ...

  3. python3.5.3rc1学习七:多线程

    import threading def exampleFun(): #打印当前激活的线程数量 print(threading.active_count) #查看上面激活的线程是哪几个 print(t ...

  4. MongoDB图形化工具(三)

    一.安装 下载地址:https://www.mongodbmanager.com/download 注意:在下载的时候需要对应上自己安装的mongodb版本. 双击安装 选择“Full install ...

  5. Tensorflow的不足之处

    Tensorflow还是有不足的地方.第一体现在Tensorflow的数据机制,由于tensor只是占位符,在没有用tf.Session().run接口填充值之前是没有实际值的.

  6. Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid 最小生成树

    D. Shichikuji and Power Grid</centerD.> Shichikuji is the new resident deity of the South Blac ...

  7. 使用Vue-cli3搭建Vue+TypeScript项目

    一,创建项目 使用 npm 安装 vue-cli 3 和typescript npm i -g @vue/cli typescript 使用vue create命令快速搭建新项目的脚手架 vue cr ...

  8. Vue.js 源码分析(十五) 指令篇 v-bind指令详解

    指令是Vue.js模板中最常用的一项功能,它带有前缀v-,比如上面说的v-if.v-html.v-pre等.指令的主要职责就是当其表达式的值改变时,相应的将某些行为应用到DOM上,先介绍v-bind指 ...

  9. WPF 中如何变相让 ListBox 宽度(Width) 100%,高度(Height) 100%,从而达到 Filled 的效果

    直接贴代码了: XAML: <Window x:Class="HelloWorld.MainWindow" xmlns="http://schemas.micros ...

  10. Linux chattr 文件保护

    Linux chattr 文件保护 chattr命令的用法:chattr [ -RV ] [ -v version ] [ mode ] files…注:最关键的是在[mode]部分,[mode]部分 ...