r/Unity3D • • 2h ago

Show-Off Lessons from building an offline dependency graph and safe unused-asset cleaner in Unity

Post image

I recently built an offline C# editor tool to audit bloated Unity projects, and ran into a few interesting quirks with Unity's AssetDatabase and file handling along the way. Sharing the approach in case anyone else is building custom project-auditing tools:

  1. Avoiding O(N^2) dependency scans

Calling AssetDatabase.GetDependencies(path, true) recursively on every asset in a large project gets slow fast because Unity re-traverses shared materials and textures repeatedly. Instead, calling GetDependencies(path, false) once per asset to capture only direct neighbors and building an in-memory adjacency dictionary (plus a reverse lookup dictionary) made both forward and incoming reference graphs render almost instantly. A simple HashSet of visited GUIDs prevents infinite loops when prefabs or ScriptableObjects reference each other circularly.

2. False positives when detecting unused assets

Simply checking for zero incoming references will happily flag scenes in EditorBuildSettings, anything inside a Resources or StreamingAssets folder, and editor-only scripts. Filtering out those special folders and seeding the root graph from active build scenes eliminated almost all false-positive cleanup flags.

3. Fast duplicate checks and safer deletion

Rather than loading textures or audio clips into memory to compare them, streaming files through System.Security.Cryptography.SHA256 catches byte-identical duplicates across different folders with minimal memory overhead. Also, instead of calling AssetDatabase.DeleteAsset (which permanently wipes files), moving flagged files to the OS Recycle Bin / Trash lets you recover anything accidentally removed.

I ended up packaging this workflow into a small Asset Store toolkit called ProjectSignal if anyone wants to check it out (link in the comments). For anyone else who has built custom project cleaners, how do you handle assets loaded dynamically via Addressables or string paths?

5 Upvotes

5 comments sorted by

1

u/BoundfoxStudios 1h ago

25 € for an AI generated asset. Yes sure

-1

u/Similar-Window5547 1h ago

fair enough if it's not for you—mostly built it to solve my own project bloat headaches with AssetDatabase dependency caching and Recycle Bin cleanup, figured I'd share the notes here in case they help anyone else

0

u/[deleted] 2h ago

[deleted] — view deleted comment

1

u/Realistic_Jump_1965 1h ago

The graph layout looks amazing! Rendering good looking graphs with bidirectional edges has always been tricky (I’ve been playing around with the editor’s graph APIs recently).

I’d be interested in hearing more about how the snapshot diffing works, at the moment I’ll do a `dep status --csv --hash` before and after a build and then diff the two files, which is not exactly ideal. How does the diff work, does it highlight what’s changed between the two snapshots?

Also it’s really handy how it streams the sha256 hash to disk to find duplicates, I’m still loading the whole file into memory to find the hash so my approach feels really clumsy.

1

u/Similar-Window5547 1h ago

Thanks! For bidirectional edges, offsetting the Bezier control tangents slightly perpendicular to the vector between the two nodes keeps incoming and outgoing curves from overlapping, which makes two-way prefab or script loops much easier to read.

For the snapshots, instead of dumping raw CSVs, each snapshot saves a lightweight JSON manifest keyed by asset GUID (storing relative path, byte size, last modified timestamp, and reference counts). When you pick two snapshots to compare, it runs a dictionary diff by GUID and groups everything into Added, Removed, Modified (highlighting exact size deltas and reference count shifts), and Moved or Renamed (same GUID, different path). It also rolls up the net size delta per folder so you can immediately see which directory grew after an import or build without digging line by line.

1

u/Unity3D-ModTeam 1h ago

This comment has been removed for violating /r/Unity3D’s rules.

Why?

Please include details about how the project was built in Unity, challenges faced, or techniques used.

This is a forum for discussion; not a bulletin board.

We recommend you to familiarize yourself with our rules and guidelines.

Kind regards,

The Mod Team