unity小工具 在编辑器面板上显示文件和文件夹的大小

Posted lihengbk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity小工具 在编辑器面板上显示文件和文件夹的大小相关的知识,希望对你有一定的参考价值。

显示效果

技术图片

 

 代码部分如下:

#if UNITY_EDITOR
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public static class FileCapacity

    private const string REMOVE_STR = "Assets";
    private const string FILESIZE = "FileSize";

    private static readonly int mRemoveCount = REMOVE_STR.Length;
    private static readonly Color mColor = new Color(0.635f, 0.635f, 0.635f, 1);
    private static Dictionary<string, string> DirSizeDictionary = new Dictionary<string, string>();
    private static List<string> DirList = new List<string>();
    private static bool isShowSize = true;

    [MenuItem("Tools/FileSize/OpenPlaySize")]
    private static void OpenPlaySize()
    
        EditorPrefs.SetBool(FILESIZE, true);
        isShowSize = true;
        GetPropjectDirs();
    

    [MenuItem("Tools/FileSize/ClosePlaySize")]
    private static void ClosePlaySize()
    
        EditorPrefs.SetBool(FILESIZE, false);
        isShowSize = false;
        Init();
    

    [InitializeOnLoadMethod]
    private static void InitializeOnLoadMethod()
    
        EditorApplication.projectChanged += GetPropjectDirs;
        EditorApplication.projectWindowItemOnGUI += OnGUI;
    

    [UnityEditor.Callbacks.DidReloadScripts]
    private static void OnScriptsReloaded()
    
        GetPropjectDirs();
    

    private static void GetPropjectDirs()
    
        Init();
        if (isShowSize == false) return;
        GetAllDirecotries(Application.dataPath);
        foreach (string path in DirList)
        
            string newPath = path.Replace("\\\\", "/");
            DirSizeDictionary.Add(newPath, GetFormatSizeString((int)GetDirectoriesSize(path)));
        
    
    private static void Init()
    
        isShowSize = EditorPrefs.GetBool(FILESIZE);
        DirSizeDictionary.Clear();
        DirList.Clear();
    

    private static void OnGUI(string guid, Rect selectionRect)
    
        if (isShowSize == false) return;
        var dataPath = Application.dataPath;
        var startIndex = dataPath.LastIndexOf(REMOVE_STR);
        var dir = dataPath.Remove(startIndex, mRemoveCount);
        var path = dir + AssetDatabase.GUIDToAssetPath(guid);
        string text = null;
        if (DirSizeDictionary.ContainsKey(path))
        
            text = DirSizeDictionary[path];
        
        else if (File.Exists(path))
        
            var fileInfo = new FileInfo(path);
            var fileSize = fileInfo.Length;
            text = GetFormatSizeString((int)fileSize);
        
        else
        
            return;
        



        var label = EditorStyles.label;
        var content = new GUIContent(text);
        var width = label.CalcSize(content).x + 10;

        var pos = selectionRect;
        pos.x = pos.xMax - width;
        pos.width = width;
        pos.yMin++;

        var color = GUI.color;
        GUI.color = mColor;
        GUI.DrawTexture(pos, EditorGUIUtility.whiteTexture);
        GUI.color = color;
        GUI.Label(pos, text);
    

    private static string GetFormatSizeString(int size)
    
        return GetFormatSizeString(size, 1024);
    

    private static string GetFormatSizeString(int size, int p)
    
        return GetFormatSizeString(size, p, "#,##0.##");
    

    private static string GetFormatSizeString(int size, int p, string specifier)
    
        var suffix = new[]  "", "K", "M", "G", "T", "P", "E", "Z", "Y" ;
        int index = 0;

        while (size >= p)
        
            size /= p;
            index++;
        

        return string.Format(
            "01B",
            size.ToString(specifier),
            index < suffix.Length ? suffix[index] : "-"
        );
    

    private static void GetAllDirecotries(string dirPath)
    
        if (Directory.Exists(dirPath) == false)
        
            return;
        
        DirList.Add(dirPath);
        DirectoryInfo[] dirArray = new DirectoryInfo(dirPath).GetDirectories();
        foreach (DirectoryInfo item in dirArray)
        
            GetAllDirecotries(item.FullName);
        
    

    private static long GetDirectoriesSize(string dirPath)
    
        if (Directory.Exists(dirPath) == false)
        
            return 0;
        

        long size = 0;
        DirectoryInfo dir = new DirectoryInfo(dirPath);
        foreach (FileInfo info in dir.GetFiles())
        
            size += info.Length;
        

        DirectoryInfo[] dirBotton = dir.GetDirectories();
        foreach (DirectoryInfo info in dirBotton)
        
            size += GetDirectoriesSize(info.FullName);
        
        return size;
    

#endif

在Tools/FileSize 里面可以选择打开和关闭显示大小 并且可以保存上一次操作哟

技术图片

 

以上是关于unity小工具 在编辑器面板上显示文件和文件夹的大小的主要内容,如果未能解决你的问题,请参考以下文章

Unity 编辑器扩展七 Property Attributes

将图像应用到 Unity UI 面板

Unity 3D 实用的10个小技巧

在unity中,如何在脚本中根据路径找到文件,然后在project面板中用黄色的框框出来?

10.Unity2018中的地形——Terrain(一)

Unity3D的介绍(1):界面、菜单、面板、组件