【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 ...
随机推荐
- 机器学习:k-NN算法(也叫k近邻算法)
一.kNN算法基础 # kNN:k-Nearest Neighboors # 多用于解决分裂问题 1)特点: 是机器学习中唯一一个不需要训练过程的算法,可以别认为是没有模型的算法,也可以认为训练数据集 ...
- 问题7:如何实现用户的历史记录功能(最多n条)
实例:制作猜字游戏,添加历史记录功能,显示用户最近猜过的数字 解决方案:使用容量为n的队列存储历史记录 使用标准库colections中的deque,一个双端循环队列 程序退出前,可以使用pickle ...
- python 基础 序列化
转自https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00138683221577 ...
- Java 数据类型间的相互转化
Java中常见基本数据类型包括(String除外[引用]) Date(int year,int month,int day,int hour,int minute,int sec); String 格 ...
- js中object、字符串与正则表达式的方法
对象 1.object.hasOwnProperty(name) 检测object是否包含一个名为name的属性,那么hasOwnProperty方法返回true,但是不包括其原型上的属性. 正则表达 ...
- SpringMVC 学习笔记(请求方法的返回值和参数)
在用注解对配置 处理器时,一般是一个方法处理一个请求,不同方法的返回类型有着不同的意义. 返回值为 ModelAndView 类型 ModelAndView 是Model 和 View 的一个集合类型 ...
- Spring 3.x 企业引用开发实战(陈雄华/林开雄)
目录 ... 第一章:Spring概述 IoC:BeanFactory.Context.El(SpringEL表达式) AOP:允许JVM虚拟机启动时使用代理类在运行时期修改指定类的字节码,改变一个类 ...
- 关于ArcGis for javascrept之Map类
ArcGis for javascrept_ESRI_Map类: 1. 构造方法:esri.Map(); 参数: extent 如果设置了该选项,一旦这个选项的投影被设置,那么所有的图层都在定义的投 ...
- php小块代码
//页面本身网址 "http://".$_SERVER["HTTP_HOST"].preg_replace("/[^\/]+$/",&quo ...
- 使用form 组件写一个用户注册,并用 bootstrap渲染
需求:使用form组件,写一个用户注册系统,包含用户名, 密码, 确认密码,手机号,性别,爱好,注册.并用bootsrap渲染,成果如下: 首先创建一个django 项目.然后在连接pymysql数据 ...