Skip to content

Instantly share code, notes, and snippets.

@seprab
Forked from Unity-Javier/AssetSizeEstimation.cs
Last active May 13, 2022 01:22
Show Gist options
  • Save seprab/1fd575a2f370567c72758cf8f3df15ee to your computer and use it in GitHub Desktop.
Save seprab/1fd575a2f370567c72758cf8f3df15ee to your computer and use it in GitHub Desktop.
Javier's script is not anymore compatible with Unity 2021, so here it is. I like to use it because it helps to identify the corresponding artifact per asset.
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEditor;
using System.Text;
using UnityEditor.Experimental;
public class AssetSizeEstimation
{
private static readonly string[] ExcludeRules = {"com.unity."};
[MenuItem("Support/PrintOutPathToFileSizes")]
public static void PrintOutPathToFileSizes()
{
var allAssetPaths = AssetDatabase.GetAllAssetPaths();
int index = 0;
StringBuilder assetPathInfo = new StringBuilder();
assetPathInfo.Append("Project Path,Library Path, Size (bytes)");
assetPathInfo.AppendLine();
try
{
foreach (var assetPath in allAssetPaths)
{
if ((index % 10) == 0)
{
float progress = (float) index / (float) allAssetPaths.Length;
EditorUtility.DisplayProgressBar("Gather path information", "Getting data", progress);
}
if (Directory.Exists(assetPath)) continue;
if (ExcludeRules.Any(x => assetPath.Contains(x))) continue;
string guidString = AssetDatabase.AssetPathToGUID(assetPath);
ArtifactKey artifactKey = new ArtifactKey(new GUID(guidString));
ArtifactID artifactID = AssetDatabaseExperimental.LookupArtifact(artifactKey);
AssetDatabaseExperimental.GetArtifactPaths(artifactID, out string[] paths);
foreach (string curVirtualPath in paths)
{
var libraryPath = Path.GetFullPath(curVirtualPath);
if (!libraryPath.Contains("Library\\Artifacts\\")) continue;
var fileInfo = new FileInfo(libraryPath);
var fileSize = fileInfo.Length;
if (fileSize == 40)
continue;
var extension = (string.CompareOrdinal(fileInfo.Extension, libraryPath) == 0)
? ""
: fileInfo.Extension;
assetPathInfo.Append($"{assetPath},{libraryPath},{fileSize},{extension}");
assetPathInfo.AppendLine();
}
++index;
}
//Data is ready, output it to a CSV file
var csvFilePath = "Assets/../FileSizes.csv";
File.WriteAllText(csvFilePath, assetPathInfo.ToString());
Debug.Log($"Data saved to: {Path.GetFullPath(csvFilePath)}");
}
finally
{
EditorUtility.ClearProgressBar();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment