当前位置:网站首页>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
边栏推荐
- Simple example of C language 1
- 卡通js射击小游戏源码
- FPGA programming experience
- Speech controlled robot based on ROS (I): realization of basic functions
- Common instructions of vim software
- LeetCode-297-二叉树的序列化与反序列化
- Does any elder brother know how to solve the huge flinksql log
- MySQL startup error 1607 unexpected process termination
- C语言数据 3(1)
- Unity makes prefabricated bodies with one key and modifies prefabricated bodies with one key
猜你喜欢

h5微信射击小游戏源码

Shanghai Jiaotong University joined hands with Taobao to set up a media computing laboratory: promoting the development of key technologies such as video super score

How to make the design of governance structure more flexible when the homogenization token is combined with NFT?

WUST-CTF2021-re校赛wp

Quick sort template

C language data 3 (2)

Raspberry pie 4B uses MNN to deploy yolov5 Lite

读取json配置文件,实现数据驱动测试

Raspberry pie CM4 -- using metartc3.0 to integrate ffmpeg to realize webrtc push-pull streaming

超大模型工程化实践打磨,百度智能云发布云原生AI 2.0方案
随机推荐
Shanghai Jiaotong University joined hands with Taobao to set up a media computing laboratory: promoting the development of key technologies such as video super score
太空射击第10课: Score (繪畫和文字)
PXE_ KS unattended system
Raspberry Pie 3 connected to WiFi
js win7透明桌面切换背景开始菜单js特效
Solve the cookie splitting problem (DP)
CNN convolutional neural network structure
js网页黑白背景开关js特效
Explain RESNET residual network in detail
Networkx common operations summary (for personal use)
Durham High Lord (classic DP)
The engineering practice of super large model was polished, and Baidu AI Cloud released the cloud native AI 2.0 solution
数据挖掘(数据预处理篇)--笔记
Solve the problem of adding the least number of parentheses (interval DP)
Residual network RESNET source code analysis - pytoch version
类与对象(中)
Raspberry pie 4B uses MNN to deploy yolov5 Lite
[pytorch] LSTM neural network
7/27 训练日志(位运算+后缀数组)
同质化代币与 NFT 结合,如何使治理结构设计更灵活?