Files
Deroc_Virtual_3D/Assets/CleanningFolders.cs
T
Alex38Lyon a49244f81f Création 4
2025-06-03 08:59:40 +02:00

67 lines
2.2 KiB
C#

using UnityEditor;
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
using System.IO;
public class UnusedAssetFinder
{
[MenuItem("Tools/Find Unused Assets")]
static void FindUnusedAssets()
{
string[] allAssetPaths = AssetDatabase.GetAllAssetPaths()
.Where(path => path.StartsWith("Assets/") && !AssetDatabase.IsValidFolder(path))
.ToArray();
HashSet<string> usedAssets = new HashSet<string>();
string[] scenes = EditorBuildSettings.scenes
.Where(s => s.enabled)
.Select(s => s.path)
.ToArray();
string[] prefabPaths = AssetDatabase.FindAssets("t:Prefab")
.Select(AssetDatabase.GUIDToAssetPath)
.ToArray();
string[] dependencies = AssetDatabase.GetDependencies(scenes.Concat(prefabPaths).ToArray(), true);
foreach (string dep in dependencies)
{
usedAssets.Add(dep);
}
List<string> unusedAssets = allAssetPaths
.Where(path => !usedAssets.Contains(path))
.ToList();
List<string> reportLines = new List<string>();
long totalSize = 0;
reportLines.Add("Assets non utilisés (avec taille en Ko) :\n");
foreach (string assetPath in unusedAssets)
{
string fullPath = Path.Combine(Directory.GetCurrentDirectory(), assetPath);
if (File.Exists(fullPath))
{
long sizeBytes = new FileInfo(fullPath).Length;
long sizeKB = sizeBytes / 1024;
totalSize += sizeBytes;
reportLines.Add($"{assetPath} - {sizeKB} Ko");
}
else
{
reportLines.Add($"{assetPath} - fichier introuvable");
}
}
reportLines.Add($"\nTaille totale des fichiers non utilisés : {totalSize / 1024} Ko ({totalSize / (1024 * 1024f):F2} Mo)");
string outputPath = Path.Combine(Directory.GetCurrentDirectory(), "UnusedAssets.txt");
File.WriteAllLines(outputPath, reportLines);
Debug.Log($"✔️ Analyse terminée : {unusedAssets.Count} fichiers non utilisés détectés.\n📄 Rapport généré dans : {outputPath}");
}
}