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. Debain install Jupyter

    1. install Anaconda https://www.anaconda.com/download/#linux 2. config jupyter $ ipython from notebo ...

  2. SQL查询语句大全集锦

    SQL查询语句大全集锦 一. 简单查询 简单的Transact-SQL查询只包括选择列表.FROM子句和WHERE子句.它们分别说明所查询列.查询的 表或视图.以及搜索条件等. 例如,下面的语句查询t ...

  3. SpringSecurity02 表单登录、SpringSecurity配置类

    1 功能需求 springSecuriy默认的登录窗口是一个弹出窗口,而且会默认对所有的请求都进行拦截:要求更改登录页面(使用表单登录).排除掉一些请求的拦截 2 编写一个springSecurity ...

  4. location.assign()、location.href、location.replace(url)的不同

    window.location.assign(url) : 加载 URL 指定的新的 HTML 文档. 就相当于一个链接,跳转到指定的url,当前页面会转为新页面内容,可以点击后退返回上一个页面. w ...

  5. JavaScript学习系列2一JavaScript中的变量作用域

    在写这篇文章之前,再次提醒一下 JavaScript 是大小写敏感的语言 // 'test', 'Test', 'TeSt' , 'TEST' 是4个不同的变量名 JavaScript中的变量,最重要 ...

  6. 【Qt官方例程学习笔记】Application Example(构成界面/QAction/退出时询问保存/用户偏好载入和保存/文本文件的载入和保存/QCommandLineParser解析运行参数)

    The Application example shows how to implement a standard GUI application with menus, toolbars, and ...

  7. C#回调函数的简单讲解与应用例子

    using System; namespace CallBackTest{ class Program //用户层,执行输入等操作 { static void Main(string[] args) ...

  8. 测试merge效率

    测试说明: MERGE是oracle提供的一种特殊的sql语法,非常适用于数据同步场景,即: (把A表数据插到B表,如果B表存在相同主键的记录则使用A表数据对B表进行更新) 数据同步的常规做法是先尝试 ...

  9. PPT2010学习笔记(共20讲)

    第1讲  商务PPT中的必备元素 # 设计需打破规范 第2讲  封面页设计(一) 大图型封面页 # 基础知识点: 插入矩形和圆形 设置半透明色 设置字体变形效果 图片增强工具 利用过渡色虚化图片边缘 ...

  10. [CentOS7] at, bash, cron, anacron

    声明:本文主要总结自:鸟哥的Linux私房菜-第十五章.例行性工作排程(crontab),如有侵权,请通知博主 at => /var/spool/at /etc/at.allow, /etc/a ...