• 热门专题

Unity处理Hierarchy面板上的一些特殊的对象的特殊标识

作者:  发布日期:2015-05-27 21:29:54
Tag标签:标识  面板  对象  
  •  

    要实现下面的效果:、其实主要是要标识一些比较特殊的对象(或者是具有特殊组件的对象)

    实现方式就是监听Unity的一些事件被


    EditorApplication.hierarchyWindowChanged += HierarchyWindowChanged;
    EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI;

    需要指明的是这个类是 AssetPostprocessor的派生类。

    资源导入后处理类, 最好不要在这里执行太多费时的操作,否则Unity编辑器会卡顿,是在执行预编译和导入处理在编辑器的右下角都有小圆圈再转会有长时间的卡顿的

     

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using DajiaGame.Px;
    using UnityEditor;
    
    /// <summary>
    /// 导入资源后处理类
    /// 
    /// 孙广东  现在是处理Hierarchy面板上的一些特殊的对象的特殊标识
    /// </summary>
    public class PxCustomHierarchy : AssetPostprocessor
    {
        /// <summary>
        /// 内部类存储 图标
        /// </summary>
        public static class Icons
        {
            public static Texture2D poseBones;
            public static Texture2D image;
            public static Texture2D boundingBox;
            public static Texture2D mesh;
            public static Texture2D warning;
    
    
            public static void Initialize()
            {
                poseBones = (Texture2D)AssetDatabase.LoadMainAssetAtPath(SpineEditorUtilities.editorGUIPath + "/icon-poseBones.png");
                image = (Texture2D)AssetDatabase.LoadMainAssetAtPath(SpineEditorUtilities.editorGUIPath + "/icon-image.png");
                boundingBox = (Texture2D)AssetDatabase.LoadMainAssetAtPath(SpineEditorUtilities.editorGUIPath + "/icon-boundingBox.png");
                mesh = (Texture2D)AssetDatabase.LoadMainAssetAtPath(SpineEditorUtilities.editorGUIPath + "/icon-mesh.png");
                warning = (Texture2D)AssetDatabase.LoadMainAssetAtPath(SpineEditorUtilities.editorGUIPath + "/icon-warning.png");
            }
        }
    
        // 数据
        public static string editorPath = "";
        public static string editorGUIPath = "";
        static Dictionary<int, GameObject> skeletonRendererTable;
        static Dictionary<int, Canvas> CanvasTable;
        public static float defaultScale = 0.01f;
        public static float defaultMix = 0.2f;
        public static string defaultShader = "Spine/Skeleton";
        public static bool initialized;
    
        const string DEFAULT_MIX_KEY = "SPINE_DEFAULT_MIX";
    
        // 静态构造函数
        static PxCustomHierarchy()
        {
    		Initialize();
    	}
    
    	static void Initialize () {
    		defaultMix = EditorPrefs.GetFloat(DEFAULT_MIX_KEY, 0.2f);
    
    		DirectoryInfo rootDir = new DirectoryInfo(Application.dataPath);
    		FileInfo[] files = rootDir.GetFiles("SpineEditorUtilities.cs", SearchOption.AllDirectories);
    		editorPath = Path.GetDirectoryName(files[0].FullName.Replace("\", "/").Replace(Application.dataPath, "Assets"));
    		editorGUIPath = editorPath + "/GUI";
    
    		Icons.Initialize();
    
            CanvasTable = new Dictionary<int, Canvas>();
    
    		EditorApplication.hierarchyWindowChanged += HierarchyWindowChanged;
    		EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI;
    
    		HierarchyWindowChanged();
    		initialized = true;
    	}
    
        static void HierarchyWindowChanged()
        {
            CanvasTable.Clear();
    
            Canvas[] boneArr = Object.FindObjectsOfType<Canvas>();
            foreach (Canvas b in boneArr)
                CanvasTable.Add(b.gameObject.GetInstanceID(), b);
        }
    
        /// <summary>
        /// 根节点在后面显示,否则是在前面
        /// </summary>
        /// <param name="instanceId"></param>
        /// <param name="selectionRect"></param>
        static void HierarchyWindowItemOnGUI(int instanceId, Rect selectionRect)
        {
            if (CanvasTable.ContainsKey(instanceId))
            {
    
                if (CanvasTable[instanceId].transform.root == CanvasTable[instanceId].transform)
                {
                    //MyDebugLog.Log("这是根节点root:" + CanvasTable[instanceId].gameObject.name);
                    Rect r = new Rect(selectionRect);
                    r.x = r.width - 15;
                    r.width = 15;
    
                    GUI.Label(r, SpineEditorUtilities.Icons.poseBones);
                }
                else if (CanvasTable[instanceId] != null)
                {
                    Rect r = new Rect(selectionRect);
                    r.x -= 26;
    
                    if (CanvasTable[instanceId].transform.childCount == 0)
                        r.x += 13;
    
                    r.y += 2;
    
                    r.width = 13;
                    r.height = 13;
    
                    GUI.DrawTexture(r, SpineEditorUtilities.Icons.poseBones);
                }
    
            }
    
        }
    }

    孙广东 2015.5.27

    转载请注明出处:http://blog.csdn.net/u010019717

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规