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模式下的后台处理的更多相关文章

  1. Unity3D Editor模式下批量修改prefab

    最经遇到一个需要批量修改已经做好的prefab的问题,查了一些资料最终实现了但是还是不够完美,通过学习也发现unity的编辑器功能还是非常强大的.废话不多说直接上代码: [ExecuteInEditM ...

  2. 二、Unity Editor模式下,操作选中对象

    使用Unity提供的工具类 UnityEditor.Selection public static GameObject activeGameObject public static UnityEng ...

  3. Editor模式下实例化Prefab

    PrefabUtility.InstantiatePrefab//需要关联 GameObject.Instantiate//不需要关联

  4. unity editor模式下读取文件夹资源

    string path = EditorUtility.OpenFolderPanel("Load png Textures", "", "" ...

  5. 继承MonoBehaviour类的优缺点和相关报错

    Unity3D文档里虽然说所有脚本继承MonoBehaviour类,但如果你想自定义类,就可以不用继承MonoBehaviour,但是这个类只能调用其中的方法和属性,无法拖到场景的物体中使用. 所有从 ...

  6. 窥探Swift之类的继承与类的访问权限

    上一篇博客<窥探Swift之别具一格的Struct和Class>的博客可谓是给Swift中的类开了个头.关于类的内容还有很多,今天就来搞一下类中的继承以及类的访问权限.说到类的继承,接触过 ...

  7. 继承Thread类

    Thread类在包java.lang中,从这个类中实例化的对象代表线程,启动一个新线程需要建立Thread实例,Thread类中常用的两个构造方法如下: (1)public Thread(String ...

  8. C++中虚继承派生类构造函数的正确写法

    最近工作中某个软件功能出现了退化,追查下来发现是一个类的成员变量没有被正确的初始化.这个问题与C++存在虚继承的情况下派生类构造函数的写法有关.在此说明一下错误发生的原因,希望对更多的人有帮助. 我们 ...

  9. C#中是否可以继承String类

    C#中是否可以继承String类? 答:String类是sealed类故不可以继承. 当对一个类应用 sealed 修饰符时,此修饰符会阻止其他类从该类继承. 在下面的示例中,类 HoverTree ...

随机推荐

  1. 多校联合训练&hdu5791 Two

    hdu5791 dp[i][j]表示的是序列A前i个数字和序列B前j个数字的公共子序列的总个数,那么的dp公式就可以这么表示 理解一下此公式若最尾部的a[i]和b[j]相等的话,那么单独的a[i]和b ...

  2. 查看Linux内核版本的命令

    方法一: 命令: uname -a  作用: 查看系统内核版本号及系统名称  方法二:  命令: cat /proc/version 作用: 查看目录"/proc"下version ...

  3. telnet IP:ERROR

    实验环境:CentOS6.8 主机:172.16.xxx.xxx:80 客户端:172.16.xxx.xxx [root@www ~18:32:27]#telnet 172.16.xxx.xxx 80 ...

  4. mybatis 学习四 源码分析 mybatis如何执行的一条sql

    总体三部分,创建sessionfactory,创建session,执行sql获取结果 1,创建sessionfactory      这里其实主要做的事情就是将xml的所有配置信息转换成一个Confi ...

  5. 【java并发编程艺术学习】(二)第一章 java并发编程的挑战

    章节介绍 主要介绍并发编程时间中可能遇到的问题,以及如何解决. 主要问题 1.上下文切换问题 时间片是cpu分配给每个线程的时间,时间片非常短. cpu通过时间片分配算法来循环执行任务,当前任务执行一 ...

  6. AJAX经常遇到的那些问题

    本文主要介绍了AJAX工作原理以及在面试题经常会遇到的问题,目录如下: 什么是Ajax 为什么要使用Ajax? Ajax特点? AJAX优缺点? Ajax流程? XMLhttprequest对象 AJ ...

  7. vue.js解决刷新404找不到页面问题

    1.将包解压到ROOT目录后创建WEB-INF目录 mkdir WEB-INF 2.进入WEB-INF目录,创建web.xml文件 cd WEB-INF touch web.xml 3.编辑web.x ...

  8. python tarfile模块基本使用

    1.压缩一个文件夹下的所有文件 #coding=utf8 import os import tarfile __author__ = 'Administrator' def main(): cwd = ...

  9. sharepoint Foundation 2013 error

    安装必须软件时提示以下错误 错误提示日志: 015-05-28 10:40:25 - Request for install time of 应用程序服务器角色.Web 服务器(IIS)角色2015- ...

  10. hdu1073

    #include<iostream> using namespace std; #define N 5050 char a[N],b[N],tmp[N]; void Read(char p ...