Skip to content

Commit

Permalink
Merge pull request #21 from Theo-Fourniez/master
Browse files Browse the repository at this point in the history
Add the ability to delete unused assets
  • Loading branch information
AlexeyPerov authored Mar 15, 2024
2 parents c3d0260 + f0c9f22 commit 3956f00
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
31 changes: 28 additions & 3 deletions Packages/DependenciesHunter/Editor/DependenciesHunter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// #define HUNT_ADDRESSABLES
// #define HUNT_ADDRESSABLES

using System;
using System.Collections.Generic;
Expand All @@ -7,6 +7,7 @@
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using Unity.VisualScripting;
using UnityEditor;
#if UNITY_2021_2_OR_NEWER
using UnityEditor.Build;
Expand All @@ -33,6 +34,20 @@ private class Result
public string OutputDescription { get; set; }
}

public int DeleteUnusedAssets(List<AssetData> assets)
{
int deletedAssetCount = 0;
foreach (AssetData resultAsset in assets)
{
bool hasDeletedAsset = AssetDatabase.DeleteAsset("Assets/" + resultAsset.ShortPath);
if (hasDeletedAsset)
{
deletedAssetCount += 1;
}
}
return deletedAssetCount;
}

private class AnalysisSettings
{
// ReSharper disable once StringLiteralTypo
Expand Down Expand Up @@ -300,7 +315,7 @@ private void OnGUI()

EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(_result.OutputDescription);

if (filteredAssets.Count < 1000)
{
if (GUILayout.Button("Save to Clipboard", GUILayout.Width(250f)))
Expand All @@ -318,6 +333,15 @@ private void OnGUI()
}
}

if (filteredAssets.Count > 0)
{
if(GUILayout.Button("Delete unused assets", GUILayout.Width(250f)))
{
int deletedCount = DeleteUnusedAssets(filteredAssets);
Debug.Log($"Deleted {deletedCount} assets");
EditorUtility.DisplayDialog("DependenciesHunter", $"Deleted {deletedCount} assets", "Ok");
}
}
EditorGUILayout.EndHorizontal();

_pagesScroll = EditorGUILayout.BeginScrollView(_pagesScroll);
Expand Down Expand Up @@ -1153,4 +1177,5 @@ public static bool IsAssetAddressable(string assetPath)
#endif
}
}
}
}

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://GitHub.com/Naereen/StrapDown.js/graphs/commit-activity)

##
This tool finds unreferenced assets in Unity project.
This tool finds and/or deletes unreferenced assets in Unity project.

All code combined into one script for easier portability.
So you can just copy-paste [DependenciesHunter.cs](./Packages/DependenciesHunter/Editor/DependenciesHunter.cs) to your project in any Editor folder.
Expand Down Expand Up @@ -42,6 +42,9 @@ The tool has two ways to use it. Each has a menu option, and an editor window.
## To list all unused assets in your project..
..click on "Tools/Dependencies Hunter" option which will open the "AllProjectAssetsReferencesWindow" window.

## To then delete the assets you filtered
..click on "Tools/Dependencies Hunter" and then click on Delete Unused Assets

![plot](./Screenshots/project_analysis_unused.png)

## To list all references towards selected assets..
Expand Down
Binary file modified Screenshots/project_analysis_all.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Screenshots/project_analysis_unused.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3956f00

Please sign in to comment.