码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-09-20 23:07:54      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:保存   cto   ++   选择   文件   database   class   edit   文件夹   

显示效果

技术图片

 

 代码部分如下:

#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(
            "{0}{1}B",
            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小工具 在编辑器面板上显示文件和文件夹的大小

标签:保存   cto   ++   选择   文件   database   class   edit   文件夹   

原文地址:https://www.cnblogs.com/lihengbk/p/11560108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!