Skip to content

Instantly share code, notes, and snippets.

@randalfien
Last active October 24, 2022 09:51
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save randalfien/8bb5d134f64e162864ba4d92e61f1b4b to your computer and use it in GitHub Desktop.
Save randalfien/8bb5d134f64e162864ba4d92e61f1b4b to your computer and use it in GitHub Desktop.
Unity Mark Unused Assets
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
public class MarkUnused
{
private const string UnusedLabel = "Unused"; // All unused assets will be tagged with this label
[MenuItem("Assets/Mark Unused Assets", false, 59)]
private static void MarkUnusedAssets()
{
EditorUtility.DisplayProgressBar("Searching for unused assets", "Finding dependencies", 0.1f);
// find all scene GUIDs
var allScenes = AssetDatabase.FindAssets("t:scene");
// translate GUIDs to paths
var allScenesPaths = allScenes.Select(AssetDatabase.GUIDToAssetPath).ToArray();
// gather all dependencies (once only)
var dependencies = new HashSet<string>(AssetDatabase.GetDependencies(allScenesPaths));
// list all types you are interested in
var allItems = new List<string>(AssetDatabase.FindAssets("t:Sprite t:VideoClip t:AudioClip"));
var unusedCounter = 0;
var progress = 0;
foreach (var guid in allItems)
{
var path = AssetDatabase.GUIDToAssetPath(guid);
var loadedAsset = AssetDatabase.LoadMainAssetAtPath(path);
var isUsed = dependencies.Contains(path);
LabelAsset(loadedAsset, isUsed);
if (!isUsed)
{
unusedCounter++;
}
progress++;
EditorUtility.DisplayProgressBar("Searching for unused assets", "Labeling assets", (float) progress / allItems.Count * 0.9f + 0.1f);
}
AssetDatabase.Refresh();
Debug.Log("Found " + unusedCounter + " unused assets");
// Set search in Project browser to show all unused assets
try
{
var projectBrowserType = Type.GetType("UnityEditor.ProjectBrowser,UnityEditor");
var window = EditorWindow.GetWindow(projectBrowserType);
MethodInfo setSearchMethodInfo = projectBrowserType.GetMethod("SetSearch",
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
null, new[] {typeof(string)}, null);
setSearchMethodInfo.Invoke(window, new object[] {"l:" + UnusedLabel});
}
catch
{
// this might possibly break in the future
}
EditorUtility.ClearProgressBar();
}
// Labels asset as unused or removes the label if used
private static void LabelAsset(UnityEngine.Object asset, bool isUsed)
{
var labelList = new List<string>(AssetDatabase.GetLabels(asset));
bool changed = false;
if (labelList.Contains(UnusedLabel) && isUsed)
{
labelList.Remove(UnusedLabel);
changed = true;
}
else if (!labelList.Contains(UnusedLabel) && !isUsed)
{
labelList.Add(UnusedLabel);
changed = true;
}
if (changed)
{
AssetDatabase.SetLabels(asset, labelList.ToArray());
EditorUtility.SetDirty(asset);
}
}
}
@randalfien
Copy link
Author

Add this file to an Editor folder to add the ability to tag all unused assets in your project. It can run a long time if you have a lot of unused assets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment