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; ...
随机推荐
- 18.Java基础_关键字this及其内存原理
this使用 this的内存原理 执行函数setName时,首先是创建函数栈空间,然后创建形参name,除此之外还会有一个this(实质是堆内存中对象的首地址),这个this会索引对象的成员变量nam ...
- 1.Python网络编程_UDP(简略版)
# -*- coding: utf-8 -*- #2019-11-24 import socket def recv(): udp_socket=socket.socket(socket.AF_INE ...
- c# 第24节 分部方法
本节内容: 1:分部方法 1:分部方法 2:实现分部方法 实现:
- Pwn-TestYourMemory
题目地址 https://dn.jarvisoj.com/challengefiles/memory.838286edf4b832fd482d58ff1c217561 32位的程序,有NX保护,拖到I ...
- 多线程(六)多线程同步_SemaPhore信号量
信号量依然是一种内核同步对象,它的作用在于控制共享资源的最大访问数量 例如:我们有一个服务器,为这服务器创建一个线程池,线程池有五个线程,每个线程处理1个请求.当五个线程都在处理请求时,这个线程池己到 ...
- ADB端口5037被占用 导致adb 停止工作
解决ADB端口占用问题 方式一 5037为adb默认端口,若5037端口被占用,打开cmd,输入以下指令, 1.查看占用端口的进程PID netstat -aon|findstr 5037 2. 通 ...
- Python文件读取中:f.seek(0)和f.seek(0,0)有什么区别
file.seek()方法标准格式是:seek(offset,whence=0)offset:开始的偏移量,也就是代表需要移动偏移的字节数whence:给offset参数一个定义,表示要从哪个位置开始 ...
- C语言中关于输出n个数后就换行的问题。
例如:n=10 ........; n++; if(n%10==0&&n!=0) //因为当n=0时,n%10的值也是0,就也会转行,为了防止这种情况的发生,就用了&&a ...
- Python连载34-信息队列
一.生产者消费者模型 1.一个模型.可以用来搭建消息队列:queue是一个用来存放变量的数据结构,特点是:先进先出 import threading import time import queue ...
- 海边拾贝-A-算法篇
收集若干算法博客地址,不定期会更新: 算法 陈浩,leetcode 的C++解法 https://github.com/haoel/leetcode 王亮,很多题目讲解的比较详细 https:/ ...