DecoratorEditor.cs

using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine; /// <summary>
/// A base class for creating editors that decorate Unity's built-in editor types.
/// </summary>
public abstract class DecoratorEditor : Editor
{
// empty array for invoking methods using reflection
private static readonly object[] EMPTY_ARRAY = new object[]; #region Editor Fields /// <summary>
/// Type object for the internally used (decorated) editor.
/// </summary>
private System.Type decoratedEditorType; /// <summary>
/// Type object for the object that is edited by this editor.
/// </summary>
private System.Type editedObjectType; private Editor editorInstance; #endregion private Dictionary<string, MethodInfo> decoratedMethods = new Dictionary<string, MethodInfo>(); private static Assembly editorAssembly = Assembly.GetAssembly(typeof(Editor)); protected Editor EditorInstance
{
get
{
if (editorInstance == null && targets != null && targets.Length > )
{
editorInstance = Editor.CreateEditor(targets, decoratedEditorType);
} if (editorInstance == null)
{
Debug.LogError("Could not create editor !");
} return editorInstance;
}
} public DecoratorEditor (string editorTypeName)
{
this.decoratedEditorType = editorAssembly.GetTypes().Where(t => t.Name == editorTypeName).FirstOrDefault(); /*System.Type[] types = editorAssembly.GetTypes();
for (int i=0; i<types.Length; i++)
{
string name=types[i].Name;
if(name.IndexOf("Collider")>-1)Debug.Log(name);
}*/ Init (); // Check CustomEditor types.
/*var originalEditedType = GetCustomEditorType(decoratedEditorType);
Debug.Log(originalEditedType);
if (originalEditedType != editedObjectType)
{
throw new System.ArgumentException(
string.Format("Type {0} does not match the editor {1} type {2}",
editedObjectType, editorTypeName, originalEditedType));
}*/
} private System.Type GetCustomEditorType(System.Type type)
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance; var attributes = type.GetCustomAttributes(typeof(CustomEditor), true) as CustomEditor[];
var field = attributes.Select(editor => editor.GetType().GetField("m_InspectedType", flags)).First(); return field.GetValue(attributes[]) as System.Type;
} private void Init()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance; var attributes = this.GetType().GetCustomAttributes(typeof(CustomEditor), true) as CustomEditor[];
var field = attributes.Select(editor => editor.GetType().GetField("m_InspectedType", flags)).First(); editedObjectType = field.GetValue(attributes[]) as System.Type;
} void OnDisable()
{
if (editorInstance != null)
{
DestroyImmediate(editorInstance);
}
} /// <summary>
/// Delegates a method call with the given name to the decorated editor instance.
/// </summary>
protected void CallInspectorMethod(string methodName)
{
MethodInfo method = null; // Add MethodInfo to cache
if (!decoratedMethods.ContainsKey(methodName))
{
var flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public; method = decoratedEditorType.GetMethod(methodName, flags); if (method != null)
{
decoratedMethods[methodName] = method;
}
else
{
Debug.LogError(string.Format("Could not find method {0}", method));
}
}
else
{
method = decoratedMethods[methodName];
} if (method != null)
{
method.Invoke(EditorInstance, EMPTY_ARRAY);
}
} public void OnSceneGUI()
{
CallInspectorMethod("OnSceneGUI");
} protected override void OnHeaderGUI ()
{
CallInspectorMethod("OnHeaderGUI");
} public override void OnInspectorGUI ()
{
EditorInstance.OnInspectorGUI();
} public override void DrawPreview (Rect previewArea)
{
EditorInstance.DrawPreview (previewArea);
} public override string GetInfoString ()
{
return EditorInstance.GetInfoString ();
} public override GUIContent GetPreviewTitle ()
{
return EditorInstance.GetPreviewTitle();
} public override bool HasPreviewGUI ()
{
return EditorInstance.HasPreviewGUI ();
} public override void OnInteractivePreviewGUI (Rect r, GUIStyle background)
{
EditorInstance.OnInteractivePreviewGUI (r, background);
} public override void OnPreviewGUI (Rect r, GUIStyle background)
{
EditorInstance.OnPreviewGUI (r, background);
} public override void OnPreviewSettings ()
{
EditorInstance.OnPreviewSettings ();
} //5.x才有以下方法
/*public override void ReloadPreviewInstances ()
{
EditorInstance.ReloadPreviewInstances ();
}*/ public override Texture2D RenderStaticPreview (string assetPath, Object[] subAssets, int width, int height)
{
return EditorInstance.RenderStaticPreview (assetPath, subAssets, width, height);
} public override bool RequiresConstantRepaint ()
{
return EditorInstance.RequiresConstantRepaint ();
} public override bool UseDefaultMargins ()
{
return EditorInstance.UseDefaultMargins ();
}
}

不影响Inspector布局拓展类的更多相关文章

  1. Unity3D研究院编辑器之不影响原有布局拓展Inspector

    今天无意间发现了一篇好文章,也让我解决了一个很久都没解决的难题.问题是这样的,假如我想去拓展Unity自带的inspector但是并不想影响原有布局. 比如下面这段代码:     1 2 3 4 5 ...

  2. spring源码分析系列 (5) spring BeanFactoryPostProcessor拓展类PropertyPlaceholderConfigurer、PropertySourcesPlaceholderConfigurer解析

    更多文章点击--spring源码分析系列 主要分析内容: 1.拓展类简述: 拓展类使用demo和自定义替换符号 2.继承图UML解析和源码分析 (源码基于spring 5.1.3.RELEASE分析) ...

  3. three.js入门系列之导入拓展类

    先来看一下three.js包的目录结构: 我们使用的时候,可以一次性import所有的功能,也可以按需引入,全依赖three.module.js这个文件对three.js的功能作了模块化处理: 但是, ...

  4. 一个CookieContainer的拓展类

    最近项目中需要频繁用到服务器返回的Cookie,由于项目采用的是HttpClient,并且用CookieContainer自动托管Cookie,在获取Cookie的时候不太方便.所以就写了个拓展类. ...

  5. CSS-弹性布局-伪类选择器-复杂选择器

    1.定位 1.堆叠顺序 一旦将元素变为已定位元素的话,元素们则有可能出现堆叠的效果. 如何改变堆叠顺序? 属性:z-index 取值:无单位的数字,数字越大越靠上. 注意: 1.父子元素间,z-ind ...

  6. css3 flex流动自适应响应式布局样式类

    1.再说css3 flex 一旦一个容器赋予了display:flex属性,将会有以下特点: 项目无法设置浮动. 列表的样式会被清除. 无法使用vertical-align设置垂直对齐方式. 目前互联 ...

  7. IOS不用AutoLayout也能实现自己主动布局的类(3)----MyRelativeLayout横空出世

    对于IOS开发人员来说,在自己主动布局出现前仅仅能通过计算和设置frame的值来处理.这样设置位置时就会出现非常多硬编码,同一时候在屏幕旋转和不同屏幕之间适配时须要编码又一次调整位置和尺寸,我们也能够 ...

  8. Android之布局Application类

    转载:https://blog.csdn.net/pi9nc/article/details/11200969 一 Application源码描述 * Base class for maintaini ...

  9. Flutter 容器Container类和布局Layout类

    1.布局和容器 [布局]是把[容器]按照不同的方式排列起来. Scaffold包含的主要部门:appBar,body,bottomNavigator 其中body可以是一个布局组件,也可以是一个容器组 ...

随机推荐

  1. Sqoop之 Sqoop 1.4.6 安装

    1. sqoop数据迁移 1.1 概述 sqoop是apache旗下一款“Hadoop和关系数据库服务器之间传送数据”的工具. 导入数据:MySQL,Oracle导入数据到Hadoop的HDFS.HI ...

  2. python之 序列与字典遍历

    在Python中有六种内建的序列:列表.元组.字符串.Unicode字符串.buffer对象和xrange对象.在这里暂时只讨论字符串.列表和元组的遍历. 一. 序列遍历 序列有两种遍历:一种通过值 ...

  3. C# ObjectArx AutoCAD二次开发(转帖)

    http://www.cnblogs.com/houlinbo/p/3325898.html 1.开发基本资料准备 用Vs2010进行Autocad 2010开发,首先下载ObjectArx 2010 ...

  4. bzoj 3572 [Hnoi2014]世界树——虚树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3572 关于虚树:https://www.cnblogs.com/zzqsblog/p/556 ...

  5. maven学习(4)-Maven 构建Web 项目

    紧接着上一节(3),现在maven新建web项目,user-web.模拟一个用户登录的需求: 工程结构: pom.xml: <project xmlns="http://maven.a ...

  6. Win7_64位 CHM打不开

    (2)在命令行运行regsvr32 itss.dll (3)在命令行运行regsvr32 hhctrl.ocx (4)开始--运行--输入“regedit”,打开注册表,找到以下分支: HKEY_LO ...

  7. java study1

    java安装 java优势-跨平台:一次编写,到处运行. jdk开发工具包,提供了开发人员需要的开发工具.jdk中包含了jre jre java的运行环境,负责程序的运行,jre中,包含程序运行时需要 ...

  8. Apache JMeter配置、安装

    一. 工具描述 apache jmeter是100%的java桌面应用程序,它被设计用来加载被测试软件功能特性.度量被测试软件的性能.设计jmeter的初衷是测试web应用,后来又扩充了其它的功能.j ...

  9. [UE4]C++创建对象的三种方式

    #include <iostream> using namespace std; class A { private: int n; public: A(int m):n(m) { } ~ ...

  10. 深入理解HTTP协议之POST方法——ajax实例

    作者:吴俊杰 性别:男 邮箱:sshroot@126.com 文章类型:原创 博客:http://www.cnblogs.com/voiphudong/ 一.说明http协议其实是一个相对比较简单的应 ...