当前位置:网站首页>Unity gadget displays the file size of the resource directory
Unity gadget displays the file size of the resource directory
2022-07-28 20:37:00 【_ Mr orange】
Unity Gadget Displays the file size of the resource directory
This editor extension tool script mainly displays the storage space occupied by all files in the resource directory , It has little practical significance , In the actual development process, we can better allocate our resources by understanding the file size of each module .
Shortcut key Alt+K Can be opened or hidden
Put the following code into Editor Folder that will do
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEngine;
public 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 professionalColor = new Color(56f / 255, 56f / 255, 56f / 255, 1);
private static readonly Color personaloColor = new Color(194f / 255, 194f / 255, 194f / 255, 1);
private static Dictionary<string, long> DirSizeDictionary = new Dictionary<string, long>();
private static List<string> DirList = new List<string>();
private static bool isShowSize = true;
[MenuItem(" Kill gadget / Shortcut / Show file size &K")]
private static void OpenPlaySize()
{
isShowSize = !isShowSize;
EditorPrefs.SetBool(FILESIZE, isShowSize);
GetPropjectDirs();
AssetDatabase.Refresh();
}
[InitializeOnLoadMethod]
private static void InitializeOnLoadMethod()
{
EditorApplication.projectChanged += GetPropjectDirs;
// stay ProjectWindow in , Delegate for each visible list item OnGUI event .
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, GetDirectoriesSize(path));
}
}
private static void Init()
{
isShowSize = EditorPrefs.GetBool(FILESIZE);
DirSizeDictionary.Clear();
DirList.Clear();
}
// Refresh editor ui
private static void OnGUI(string guid, Rect selectionRect)
{
if (isShowSize == false || selectionRect.height > 16)
return;//>16 To prevent typographical confusion caused by the scaling of file icons
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;
long fileSize = 0;
if (DirSizeDictionary.ContainsKey(path))
{
fileSize = DirSizeDictionary[path];
}
else if (File.Exists(path))
{
fileSize = new FileInfo(path).Length;
}
else
{
return;
}
text = GetFormatSizeString((int)fileSize);
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;
EditorGUI.DrawRect(pos, UseDark() ? professionalColor : personaloColor);
Color defaultC = GUI.color;
if (fileSize > 1024 * 1024 * 10)
{
GUI.color = Color.red;
}
else if (fileSize > 1024 * 1024)
{
GUI.color = Color.yellow;
}
GUI.Label(pos, text);
GUI.color = defaultC;
}
/// <summary>
/// Get the skin
/// </summary>
private static bool UseDark()
{
PropertyInfo propertyInfo = typeof(EditorGUIUtility).GetProperty("skinIndex", BindingFlags.Static | BindingFlags.NonPublic);
bool useDark = (int)propertyInfo.GetValue(null) == 1;
return useDark;
}
// Calculate file size
private static string GetFormatSizeString(int size)
{
string[] ns = new string[] {
"Byte", "KB", "MB", "GB", "TB", "PB" };
double baseNum = 1024;
if (size <= 0)
{
return $"{
0.ToString("F2")} {
ns[0]}";
}
int pow = Math.Min((int)Math.Floor(Math.Log(size, baseNum)), ns.Length - 1);
return $"{
(size / Math.Pow(baseNum, pow)).ToString("F2")} {
ns[pow]}";
}
// obtain Asset All file paths in the directory
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);
}
}
// Get the size of the specific path file
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;
}
}
The article is reproduced in https://blog.csdn.net/qq_37310110/article/details/124295956?spm=1001.2014.3001.5501
边栏推荐
- Use of DDR3 (axi4) in Xilinx vivado (3) module packaging
- 3D激光SLAM:LeGO-LOAM论文解读---简介部分
- 卡通js射击小游戏源码
- 太空射击第10课: Score (繪畫和文字)
- 漂亮的蓝色背景表单输入框样式
- Gru neural network
- 超大模型工程化实践打磨,百度智能云发布云原生AI 2.0方案
- Durham High Lord (classic DP)
- Raspberry pie 4B deploy yolov5 Lite using ncnn
- Pytorch model parameter assignment - tensor.copy_ () method
猜你喜欢

Music says
![Teach you how to draw a map with ArcGIS [thermal map]](/img/16/993da4678667884a98e1d82db37d69.png)
Teach you how to draw a map with ArcGIS [thermal map]

Raspberry pie 4B uses MNN to deploy yolov5 Lite

同质化代币与 NFT 结合,如何使治理结构设计更灵活?

树行表达方式
![[task03: complex query methods - views, subqueries, functions, etc.]](/img/83/2cad48016199b079aca0251b7b4ee8.png)
[task03: complex query methods - views, subqueries, functions, etc.]
![[C language] comprehensively analyze the pointer and sort out the pointer knowledge points](/img/71/f4138b68c27d6447fbd5b5e0762929.png)
[C language] comprehensively analyze the pointer and sort out the pointer knowledge points

太空射击第15课: 道具

数据挖掘(数据预处理篇)--笔记

js图表散点图例子
随机推荐
Raspberry pie 4B uses MNN to deploy yolov5 Lite
Solve the problem that jupyter cannot import new packages
Unity makes prefabricated bodies with one key and modifies prefabricated bodies with one key
Introduction to seven kinds of polling (practice link attached)
CNN convolution neural network learning process (weight update)
产品力大幅提升 新款福特探险者发布
Durham High Lord (classic DP)
Related concepts of multitasking programming
一个程序员的水平能差到什么程度?尼玛,都是人才呀...
树行表达方式
Linxu 【权限,粘滞位】
Regular symbol description
同质化代币与 NFT 结合,如何使治理结构设计更灵活?
[task01: getting familiar with database and SQL]
SQL审核工具自荐Owls
一文了解 Rainbond 云原生应用管理平台
Scheduled backup of MySQL database under Windows system
上海交大牵手淘宝成立媒体计算实验室:推动视频超分等关键技术发展
Representation of base and number 2
h5微信射击小游戏源码