Last active
May 13, 2022 01:21
-
-
Save Unity-Javier/230682198b4fd136b4fc79072bb0f337 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using UnityEngine; | |
using UnityEditor; | |
using System.Text; | |
using UnityEditor.Experimental; | |
public class AssetSizeEstimation | |
{ | |
[MenuItem("AssetDatabase/PrintOutPathToFileSizes")] | |
public static void PrintOutPathToFileSizes() | |
{ | |
var allAssetPaths = AssetDatabase.GetAllAssetPaths(); | |
int index = 0; | |
StringBuilder assetPathInfo = new StringBuilder(); | |
assetPathInfo.Append("Project Path,Library Path, Size (bytes), Extension"); | |
assetPathInfo.AppendLine(); | |
foreach (var assetPath in allAssetPaths) | |
{ | |
//Don't update the progress bar too often | |
if ((index % 10) == 0) | |
{ | |
float progress = (float)index / (float)allAssetPaths.Length; | |
EditorUtility.DisplayProgressBar("Gather path information", "Getting data", progress); | |
} | |
var guidString = AssetDatabase.AssetPathToGUID(assetPath); | |
var hash = AssetDatabaseExperimental.GetArtifactHash(guidString); | |
AssetDatabaseExperimental.GetArtifactPaths(hash, out var paths); | |
foreach (var curVirtualPath in paths) | |
{ | |
//The virtual path redirects somewhere, so we get the | |
//actual path on disk (or on the in memory database, accordingly) | |
var libraryPath = Path.GetFullPath(curVirtualPath); | |
var fileInfo = new FileInfo(libraryPath); | |
//We have some additional files that we create that we want to ignore | |
//They all have a size of 40 | |
if (fileInfo.Length == 40) | |
continue; | |
var extension = (string.CompareOrdinal(fileInfo.Extension, libraryPath) == 0) ? "" : fileInfo.Extension; | |
assetPathInfo.Append($"{assetPath},{libraryPath},{fileInfo.Length},{extension}"); | |
assetPathInfo.AppendLine(); | |
} | |
++index; | |
} | |
//Data is ready, output it to a CSV file | |
var csvFilePath = "Assets/../FileSizes.csv"; | |
File.WriteAllText(csvFilePath, assetPathInfo.ToString()); | |
EditorUtility.ClearProgressBar(); | |
Debug.Log($"Data saved to: {Path.GetFullPath(csvFilePath)}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment