【Unity3D】用继承EditorUpdater类来实现Editor模式下的后台处理
EditorWindow类的OnGUI函数只会在窗口焦点处于Editor窗口上的时候才会运行。如果希望焦点不在Editor窗口上的时候,它也能实时更新,可以实现以下方法:
| OnDestroy | OnDestroy is called when the EditorWindow is closed. |
| OnFocus | Called when the window gets keyboard focus. |
| OnGUI | Implement your own editor GUI here. |
| OnHierarchyChange | Called whenever the scene hierarchy has changed. |
| OnInspectorUpdate | OnInspectorUpdate is called at 10 frames per second to give the inspector a chance to update. |
| OnLostFocus | Called when the window loses keyboard focus. |
| OnProjectChange | Called whenever the project has changed. |
| OnSelectionChange | Called whenever the selection has changed. |
| Update | Called multiple times per second on all visible windows. |
但是,如果Editor窗口被贴到大窗口上后,选择和它平级的窗口,从而隐藏了Editor窗口,这样OnGUI函数仍然无法调用。所以,我们为了实现更有效的后台处理,可以采用继承一个自己写的EditorUpdate类的方式。
using System;
using System.Collections;
using System.Reflection;
using UnityEditor;
using UnityEngine; [InitializeOnLoad]
public class EditorMonoBehaviour
{
static EditorMonoBehaviour()
{
var type = Types.GetType ("UnityEditor.EditorAssemblies", "UnityEditor.dll");
var method = type.GetMethod("SubclassesOf",BindingFlags.Static|BindingFlags.NonPublic|BindingFlags.Instance,null,new Type[]{typeof(Type)},null);
var e = method.Invoke (null, new object[] { typeof(EditorMonoBehaviour) }) as IEnumerable;
foreach (Type editorMonoBehaviourClass in e)
{
method = editorMonoBehaviourClass.BaseType.GetMethod ("OnEditorMonoBehaviour", BindingFlags.NonPublic | BindingFlags.Instance);
if (method != null)
{
method.Invoke (System.Activator.CreateInstance (editorMonoBehaviourClass), new object[]);
}
}
} private void OnEditorMonoBehaviour()
{
EditorApplication.update += Update;
EditorApplication.hierarchyWindowChanged += OnHierarchyWindowChanged;
EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI;
EditorApplication.projectWindowChanged += OnProjectWindowChanged;
EditorApplication.projectWindowItemOnGUI += ProjectWindowItemOnGUI;
EditorApplication.modifierKeysChanged += OnModifierKeysChanged; EditorApplication.CallbackFunction function = () => OnGlobalEventHandler (Event.current);
FieldInfo info = typeof(EditorApplication).GetField ("globalEventHandler", BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
EditorApplication.CallbackFunction functions = (EditorApplication.CallbackFunction)info.GetValue (null);
function += function;
info.SetValue (null, (object)functions);
EditorApplication.searchChanged += OnSearchChanged;
EditorApplication.playmodeStateChanged += () => {
if(EditorApplication.isPaused)
{
OnPlaymodeStateChanged(PlayModeState.Paused);
}
if(EditorApplication.isPlaying)
{
OnPlaymodeStateChanged(PlayModeState.Playing);
}
if(EditorApplication.isPlayingOrWillChangePlaymode)
{
OnPlaymodeStateChanged(PlayModeState.PlayingOrWillChangePlayMode);
}
}; Start ();
} public virtual void Start()
{} public virtual void Update()
{} public virtual void OnHierarchyWindowChanged()
{} public virtual void HierarchyWindowItemOnGUI(int instanceID,Rect selectionRect)
{} public virtual void OnProjectWindowChanged()
{} public virtual void ProjectWindowItemOnGUI(string guid,Rect selectionRect)
{} public virtual void OnModifierKeysChanged()
{} public virtual void OnGlobalEventHandler(Event e)
{} public virtual void OnSearchChanged()
{} public virtual void OnPlaymodeStateChanged(PlayModeState playModeState)
{} public enum PlayModeState
{
Playing,
Paused,
Stop,
PlayingOrWillChangePlayMode
}
}
然后在另一个脚本中继承这个类,并重载Start和Update等方法,在这些方法中实现后台逻辑,就可以后台更新了。
using UnityEngine;
using System.Collections; public class UpdaterTest :EditorMonoBehaviour
{
public override void Update ()
{ }
}
【Unity3D】用继承EditorUpdater类来实现Editor模式下的后台处理的更多相关文章
- Unity3D Editor模式下批量修改prefab
最经遇到一个需要批量修改已经做好的prefab的问题,查了一些资料最终实现了但是还是不够完美,通过学习也发现unity的编辑器功能还是非常强大的.废话不多说直接上代码: [ExecuteInEditM ...
- 二、Unity Editor模式下,操作选中对象
使用Unity提供的工具类 UnityEditor.Selection public static GameObject activeGameObject public static UnityEng ...
- Editor模式下实例化Prefab
PrefabUtility.InstantiatePrefab//需要关联 GameObject.Instantiate//不需要关联
- unity editor模式下读取文件夹资源
string path = EditorUtility.OpenFolderPanel("Load png Textures", "", "" ...
- 继承MonoBehaviour类的优缺点和相关报错
Unity3D文档里虽然说所有脚本继承MonoBehaviour类,但如果你想自定义类,就可以不用继承MonoBehaviour,但是这个类只能调用其中的方法和属性,无法拖到场景的物体中使用. 所有从 ...
- 窥探Swift之类的继承与类的访问权限
上一篇博客<窥探Swift之别具一格的Struct和Class>的博客可谓是给Swift中的类开了个头.关于类的内容还有很多,今天就来搞一下类中的继承以及类的访问权限.说到类的继承,接触过 ...
- 继承Thread类
Thread类在包java.lang中,从这个类中实例化的对象代表线程,启动一个新线程需要建立Thread实例,Thread类中常用的两个构造方法如下: (1)public Thread(String ...
- C++中虚继承派生类构造函数的正确写法
最近工作中某个软件功能出现了退化,追查下来发现是一个类的成员变量没有被正确的初始化.这个问题与C++存在虚继承的情况下派生类构造函数的写法有关.在此说明一下错误发生的原因,希望对更多的人有帮助. 我们 ...
- C#中是否可以继承String类
C#中是否可以继承String类? 答:String类是sealed类故不可以继承. 当对一个类应用 sealed 修饰符时,此修饰符会阻止其他类从该类继承. 在下面的示例中,类 HoverTree ...
随机推荐
- 影响Scala语言设计的因素列表
Scala语言设计概述 Scala的设计受许多编程语言和研究思想的影响.事实上,仅很少的Scala的特点是全新的:大多数都已经被以另外的形式用在其他语言中了.Scala的革新主要来源于它是如何构造并放 ...
- centos7 查看启动ntp服务命令
标签(空格分隔): centos7 系统 1. 查看ntp服务命令: [root@node1 ~]# systemctl status ntpd * ntpd.service - Network Ti ...
- JS ES6 -- let命令
1.ES6新增了块级作用域的let和const 这新特性let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. for循环的计数器,就很合适使用let命令 ...
- springMVC绑定json参数之一
一.SpringMVC @RequestBody接收Json对象字符串 以前,一直以为在SpringMVC环境中,@RequestBody接收的是一个Json对象,一直在调试代码都没有成功,后来发现, ...
- free查看内存情况
free命令可以显示当前系统未使用的和已使用的内存数目,还可以显示被内核使用的内存缓冲区. free [option] -b:以Byte为单位显示内存使用情况: -k:以KB为单位显 ...
- 选择炸了(JIRA)的88个
作者:Martin Seibert SEIBERT MEDIA 首席执行官. 原文地址:http://seibert.biz/jirareasons 作者Martin Seibert 是德国互联网代理 ...
- [HDU1754]I Hate It线段树裸题
http://acm.hdu.edu.cn/showproblem.php?pid=1754 解题关键:刚开始死活超时,最后发现竟然是ch,和t1.t2每次循环都定义的锅,以后养成建全局变量的习惯. ...
- try catch 块中debug时发现错误细节的一次记录
在解决已有代码的一个问题时,有一个try catch块,基本代码如下: try { //do something } catch { LogHelper.Debug(typeof(myHelper), ...
- jQuery学习1
学习jQuery的过程中发现了一个博客把jquery的要点整理的很不错,摘抄其精华以备学习.感谢:http://blog.csdn.net/wph_1129/article/details/59932 ...
- 工作随记--div最小高度
给div添加最小高度 min-height:1000px;//IE7\FF height:100%;//IE6\IE7\FF 这个很重要,IE6定死高度后,需要再加上这条,才能自动延伸. _heigh ...