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. shell教程-001:shell简介 什么是shell,shell命令的两种执行方式

    Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的. Shell既是一种命令语言,又是一种程序设计语言.作为命令语言,它交互式地解 ...

  2. MySQL 查看编码 排序规则

    查看数据库的排序规则 mysql> show variables like 'collation%'; +----------------------+-------------------+ ...

  3. socket()函数介绍

    socket()函数介绍 socket函数介绍 函数原型 domain type protocol errno 示例 函数原型 socket()函数的原型如下,这个函数建立一个协议族为domain.协 ...

  4. listener does not currently know of 。。。。

    打开Oracle的 listener.ora 文件: (../oracle/product/10.2.0/db_1/network/admin/listener.ora) 设置环境变量: Oracle ...

  5. linux vnc 安装

    http://blog.csdn.net/mchdba/article/details/49306383

  6. dede 添加自定义函数

    include/extend.func.php里添加函数 function GetTopTags($str){$dsql = new Dedesql(false);$row = $dsql->G ...

  7. 转!!!解释Eclipse下Tomcat项目部署路径问题(.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps)

    1.配置eclipse的开发环境,配置jdk的安装路径和tomcat安装路径. 2.在eclipse下建立Dynamic Web Project工程zhgy,在使用eclipse中new一个tomca ...

  8. HDU 2063 过山车(匈牙利算法)

    过山车 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissio ...

  9. ASP.NET Web Pages:WebMail 帮助器

    ylbtech-.Net-ASP.NET Web Pages:WebMail 帮助器 1.返回顶部 1. ASP.NET Web Pages - WebMail 帮助器 WebMail 帮助器 - 众 ...

  10. crontab 执行脚本,报错/home/scripts/eyeMonitor.sh: line 8: node: command not found

     报错现象:在shell下执行node没有任何问题,但crontab自动运行就会报错. 原因:node的安装路径:/root/.nvm/versions/node/v6.7.0/bin/node Sh ...