您好,欢迎来到Unity之家!   unity.jb51.net 
  • 首 页
  • 你问我答
  • 当前位置:首页 > 程序开发 > 代码编程 >
    使用事件来监听Hierachy、Project等视图结构的变化
    时间:2014-12-12 10:36 来源:互联网 作者:网络 浏览:收藏 挑错 推荐 打印

    网上发现了一个日本人写的文档,实现的原理很有意思,内容不错就帮手翻译一下


    原文地址:http: // anchan828.hatenablog.jp/entry/2013/12/29/015306


    请注意一定把这两个监听的脚本放在Editor文件夹下


    直接为大家贴代码


    一.先是基类


    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;
     
     
            // globalEventHandler
            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);
            functions += 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);
                }
            };
     
        }
     
        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
        }
    }



    (责任编辑:脚印)
    免责声明:Unity之家部分内容来源于互联网,如有侵权,请联系我们,本站将立即进行处理。