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. 【转】 Pro Android学习笔记(十九):用户界面和控制(7):ListView

    目录(?)[-] 点击List的item触发 添加其他控件以及获取item数据 ListView控件以垂直布局方式显示子view.系统的android.app.ListActivity已经实现了一个只 ...

  2. 常见的CSS和HTML面试题

    1. 常用那几种浏览器测试?有哪些内核(Layout Engine)? 浏览器:IE,Chrome,FireFox,Safari,Opera. 内核:Trident,Gecko,Presto,Webk ...

  3. mkfs在特定的分区上建立 linux 文件系统

    Linux mkfs命令用于在特定的分区上建立 linux 文件系统 使用方式 : mkfs [-V] [-t fstype] [fs-options] filesys [blocks]参数 :    ...

  4. 共用体的定义和应用【C++】

    定义: 使用覆盖技术,几个变量相互覆盖,从而使几个不同变量共占同一段内存的结构,成为共同体类型的结构. 共同体的定义类似结构体,不过共同体的所有成员都在同一段内存中存放,起始地址一样,并且同一时刻只能 ...

  5. unreal3控制台窗口属性调整

    在windows平台上,unreal3的console窗口类是FOutputDeviceConsoleWindows 启动时,它可以从XXXGame.ini中读取诸如窗口大小之类的属性,具体的代码在 ...

  6. 8、SRR数据下载https://ftp-trace.ncbi.nlm.nih.gov/sra/sdk/2.8.2/

    1.prefetch SRRxxxxxx         -/ncbi/public/sra 2.fastq-dump --split-files xxxxxxsra 3.SRA.SAM以及Fastq ...

  7. 《精通Spring4.X企业应用开发实战》读后感第五章(FactoryBean)

  8. Entity Framework Code-First(9.10):DataAnnotations - NotMapped Attribute

    DataAnnotations - NotMapped Attribute: NotMapped attribute can be applied to properties of a class. ...

  9. IOS 获取系统通讯录

    进入正题  获取系统通讯录,不想多讲,留下链接http://my.oschina.net/joanfen/blog/140146 通常做法: 首先创建一个ABAddressBookRef类的对象add ...

  10. hdu 2897 邂逅明下 (简单巴什博弈)

    题目链接 邂逅明下 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...