zoukankan      html  css  js  c++  java
  • 【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 ()
        {
            
        }
    }
  • 相关阅读:
    当我们开发一个接口时需要注意些什么
    一条查询语句在MySQL服务端的执行过程
    痞子衡嵌入式:基于恩智浦i.MXRT1060的MP4视频播放器(RT-Mp4Player)设计
    痞子衡嵌入式:基于恩智浦i.MXRT1010的MP3音乐播放器(RT-Mp3Player)设计
    《痞子衡嵌入式半月刊》 第 18 期
    痞子衡嵌入式:MCUBootUtility v2.4发布,轻松更换Flashloader文件
    华为HMS Core助力开发者打造精品应用,共创数智生活
    HMS Core 6.0即将发布 加码应用生态升级
    华为开发者联盟生态市场企业特惠GO第1期—应用软件专题
    卡片跳转快应用指定页面,如何点返回直接退出快应用回到卡片
  • 原文地址:https://www.cnblogs.com/qiuxiangmuyu/p/5826980.html
Copyright © 2011-2022 走看看