【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 ...
随机推荐
- Java常见设计模式之代理模式
指由一个代理主题来操作真实主题,真实主题执行具体的业务操作,而代理主题负责其它相关业务的处理.比如生活中的通过代理访问网络,客户通过网络代理连接网络(具体业务),由代理服务器完成用户权限和访问限制等与 ...
- bean validator - Hibernate validator
在后台开发过程中,对参数的校验成为开发环境不可缺少的一个环节.比如参数不能为null,email那么必须符合email的格式,如果手动进行if判断或者写正则表达式判断无意开发效率太慢,在时间.成本.质 ...
- 关于hist
""" Demo of the histogram (hist) function with a few features. In addition to the bas ...
- 布尔类型(Boolean)
布尔类型(Boolean) 布尔类型仅包含真假,与Python不同的是其首字母小写. == 比较值相等 != 不等于 === 比较值和类型相等 !=== 不等于 || ...
- swiper实现左右滑动图片
ref:http://www.swiper.com.cn/usage/index.html help:https://segmentfault.com/a/1190000002962202 src: ...
- JavaScript学习系列4 ----- JavaScript中的扩展运算符 三个点(...)
在JavaScript中, ES6开始有rest参数 和 三个点扩展运算符 (spread运算符) 我们来看看他们各自的用处 1. rest参数 rest参数的形式为 ...变量名 ...
- hdu1085
#include <iostream> #include <cstring> using namespace std; int n[3],a[9000],b[9000],i,j ...
- Ajax.BeginForm 使用过程中遇到的问题
一.Ajax.BeginForm设置的路由在浏览器中解析不到. 问题截图:在视图中设置"Counter/Index",在浏览器中显示的是"action='/'" ...
- 创建、配置Servlet
1.创建Servlet 2.选择继承的类及需要覆盖的方法 3.Servlet结构 package com.sysker.servlet; import java.io.IOException; imp ...
- 莫队算法-小Z的袜子
小Z的妹子袜子这道题用的是莫队算法,据说解决离线区间询问几乎无敌. 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于 ...