Writing Out Every Asset Reference
Hello. I'm Hirayama from the Technology Department.
Today I'll talk about a tool that tells you things like "which assets reference this asset" and "what gets loaded along with this asset when you load it".
I basically built it with a "good enough for what I need right now" attitude, so I didn't design it with other people in mind. For tools like this, I think it's better to share just a template and let each product customize it.
How to use it
Grab the script from GitHub and drop it into an Editor folder somewhere, and "Kayac/Make Reference Graph" appears in the menu.
Run it, and it spits out four files into the project folder.
- referenceGraph.txt
- A list of the assets each asset directly depends on
- referenceGraphPrefab.txt
- A list of all the assets each prefab depends on, with reference counts.
- referenceGraphScene.txt
- A list of all the assets each scene depends on, with reference counts.
- referenceGraphScriptable.txt
- A list of all the assets each ScriptableObject depends on, with reference counts.
As I said above, requirements will differ from product to product, so I think you'll be better off modifying it, or using it as a reference to write your own.
The reference counts are there "to see whether prefabs have been carelessly placed in large numbers". As we saw in an earlier article, placing several copies of a prefab makes the data grow in proportion. In the current implementation the number of prefab placements and the reference count don't match, but at the very least, a large count means something is suspicious.
What's it good for?
It has a few uses.
When you want to delete something but worry it's still used
When you want to delete an asset but worry that it's being used, search referenceGraph.txt, and if it doesn't appear "on the referenced side", you know it's safe to delete.
There's a slight catch, though. Since the implementation uses AssetDatabase.GetDependencies, it can't pick up references from the project settings, so you can't tell, for example, whether something is used as the Splash Screen image or the app icon. And of course, if it's under Resources or StreamingAssets, you can't tell either.
You may also want "a list of the unused ones"; I think a small change to this program would get you that quickly.
Checking that scenes/prefabs haven't grown carelessly large
Sometimes a particular scene/prefab is oddly slow to load. A reference to a large prefab crept in without anyone noticing, say, and the amount of data being loaded ballooned as one thing dragged in the next. You can spot this by looking at the referenceGraphなんとか.txt files.
You can investigate once symptoms like "it's slow" or "it's huge" show up, or look them over preventively. I used it this time while working on:
- Reducing memory usage
- Improving startup speed
- Reducing spikes, or moving when they happen
You may also notice a strangely large image being loaded, wonder why, and discover that "the texture is an atlas, and a model using only a tiny part of it was placed in the scene".
Implementation
It's similar to my earlier article on listing textures and article on preventing duplicate assets between AssetBundles and the app itself.
Enumerate assets with AssetDatabase.FindAssets(), and get their dependencies with AssetDatabase.GetDependencies(). Then use this information to turn the relationships into a graph structure. After that, it's just a matter of using it for whatever you need.
This time I want to know the relationships of all assets, so I simply enumerate every asset, call GetDependencies on all of them, put them in a dictionary for easy lookup, and build the asset reference relationships into a graph structure. The vertices (nodes) that make up the graph are of this type:
class Node
{
public string path;
public string[] dependencies;
public List<Node> children;
}
- An asset can reference multiple assets
- An asset can be referenced by multiple assets
- There can be loops (A→B→A..)
With that much freedom, you can't write it out as text in a simple tree structure like JSON or XML.
"There can be loops" is especially serious: when building lists with recursive processing, loop detection is a must. Otherwise you get an infinite loop. In this tool, asset paths are pushed onto a Stack, and as they're pushed and popped, there's a check that says "if a path that's already on the current stack comes up, skip it".
GUIDs and paths
Another thing to watch out for is that there can be "multiple assets in one file". In other words, one path can have multiple GUIDs. FindAssets gives you GUIDs, and converting them to paths with AssetDatabase.GUIDToAssetPath() can give you the same path more than once. So I stuff them into a HashSet first to remove duplicates:
static IList<string> FindAssets(string filter)
{
var guids = AssetDatabase.FindAssets(filter);
var set = new HashSet<string>();
foreach (var guid in guids)
{
var path = AssetDatabase.GUIDToAssetPath(guid);
set.Add(path);
}
var list = new List<string>();
list.AddRange(set);
return list;
}
If you can write this elegantly with Linq or the like, that's probably better (Distinct, was it?).
Extending/customizing
If "just dumping it to a text file" feels a bit bland, adding a nice UI that lets you "click to jump to that asset" might delight people. But with a GUI you also have to think about drawing cost, so it's quite a lot of work. Unless you do something like drawing only what fits in the window, you can't display hundreds or thousands of entries.
"Making a list of unused assets" is also a good one. You just add a step that checks whether each asset is referenced. But this too needs customization; there will probably be requests like:
- Exclude certain types (code, for example)
- Exclude certain folders (Resources, StreamingAssets, app icons and so on)
- Check only under the selected folder
You'll probably also get requests like "I want to know which GameObject, specifically, references a given asset", which is a bit harder. Unity's GetDependencies isn't enough, and when I did it before, I read the YAML myself. I've also heard that you can do the same thing without touching the YAML by using SerializedObject, but I haven't tried it. Whichever way you go, I think it's a feature that doesn't hurt to have in a large project.
In closing
In Unity, any asset you use goes into the build automatically, so "before I knew it, the build had become absurdly huge" happens a lot. You may think "isn't that only a problem for large-scale games?", but these days there's a reason you can't say that: the Asset Store.
In the old days, making a large-scale game meant mass-producing data with a large-scale development team, but now you can easily get assets from the Asset Store, so even with a small team the data can get large. I suspect accidents like "before I knew it, startup waits and memory usage had gotten out of hand" are more likely than they used to be.
By the way, tools of this kind stimulate the algorithmic part of your brain, so I think writing one now and then is a good way to keep your brain from aging. For people with little experience in algorithms, it should be good practice. With a bad design it easily gets 10 or 100 times slower, so doing it once in a while makes for a fun puzzle.