zoukankan      html  css  js  c++  java
  • Unity在Project视图里面显示文件的拓展名

    Unity在Project视图里面显示文件的拓展名

    功能脚本如下:

    using System.IO;
    using System.Reflection;
    using UnityEngine;
    using UnityEditor;
    
    [InitializeOnLoad]
    public static class ShowFileExtensions
    {
        static ShowFileExtensions()
        {
            EditorApplication.projectWindowItemOnGUI += ProjectWindowItemOnGUI;
        }
    
        private static void ProjectWindowItemOnGUI(string guid, Rect rect)
        {
            string assetPath = AssetDatabase.GUIDToAssetPath(guid);
            Object obj = AssetDatabase.LoadAssetAtPath<Object>(assetPath);
    
            if (obj != null && AssetDatabase.IsMainAsset(obj) && !IsDirectory(obj))
            {
                if (showBigIcon)
                {
                    string extension = Path.GetExtension(assetPath);
                    GUI.Label(rect, extension, EditorStyles.boldLabel);
                }
                else
                {
                    var fileName = Path.GetFileName(assetPath);
                    var labelRect = rect.Translate();
                    GUI.Label(labelRect, fileName);
                }
            }
    
            EditorApplication.RepaintProjectWindow();
        }
    
        private static bool showBigIcon
        {
            get { return IsTwoColumnMode && listAreaGridSize > 16f; }
        }
    
    
        private static bool IsTwoColumnMode
        {
            get
            {
                var projectWindow = GetProjectWindow();
                var projectWindowType = projectWindow.GetType();
                var modeFileInfo = projectWindowType.GetField("m_ViewMode", BindingFlags.Instance | BindingFlags.NonPublic);
                int mode = (int) modeFileInfo.GetValue(projectWindow);
                return mode == 1;
            }
        }
    
        private static float listAreaGridSize
        {
            get
            {
                var projectWindow = GetProjectWindow();
                var projectWindowType = projectWindow.GetType();
                var propertyInfo = projectWindowType.GetProperty("listAreaGridSize", BindingFlags.Instance | BindingFlags.Public);
                return (float) propertyInfo.GetValue(projectWindow, null);
            }
        }
    
        private static EditorWindow GetProjectWindow()
        {
            if (EditorWindow.focusedWindow != null && EditorWindow.focusedWindow.titleContent.text == "Project")
            {
                return EditorWindow.focusedWindow;
            }
    
            return GetExistingWindowByName("Project");
        }
    
        private static EditorWindow GetExistingWindowByName(string name)
        {
            EditorWindow[] windows = Resources.FindObjectsOfTypeAll<EditorWindow>();
            foreach (var item in windows)
            {
                if (item.titleContent.text == name)
                {
                    return item;
                }
            }
    
            return default(EditorWindow);
        }
    
        private static Rect Translate(this Rect rect)
        {
            rect.x += 15.8f;
            rect.y += 0.9f;
            return rect;
        }
    
        private static bool IsDirectory(Object obj)
        {
            if (obj == null)
            {
                return false;
            }
    
            return obj is DefaultAsset && !AssetDatabase.IsForeignAsset(obj);
        }
    }
    

      

  • 相关阅读:
    node
    github
    [模块] pdf转图片-pdf2image
    python 15 自定义模块 随机数 时间模块
    python 14 装饰器
    python 13 内置函数II 匿名函数 闭包
    python 12 生成器 列表推导式 内置函数I
    python 11 函数名 迭代器
    python 10 形参角度 名称空间 加载顺序
    python 09 函数参数初识
  • 原文地址:https://www.cnblogs.com/kanekiken/p/10550931.html
Copyright © 2011-2022 走看看