Unity3D用继承EditorUpdater类来实现Editor模式下的后台处理

Posted 秋乡暮雨的学习笔记

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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[0]);
            }
        }
    }

    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中实现Finite State Machine System有限状态机框架系统

不能使用继承的 Django 模型的 Meta 类来配置在继承的抽象模型中定义的字段

PHP之final

实现如下类之间的继承关系,并编写Music类来测试这些类。

实现如下类之间的继承关系,并编写Music类来测试这些类。

3、在Unity3D中创建一个脚本,默认继承哪个类。( )?