BLOG

Notes from the KAYAC hyper-casual team

Keeping Assets from Being Duplicated Between AssetBundles and the App Itself

Takashi Hirayama

Hello. I'm Hirayama from the Technology Department.

Today I'll introduce a tool for cutting down on the kind of accident where you go, "Hey, the app's own prefabs are still referencing material that's supposed to go into AssetBundles!"

The code is on GitHub (AssetReferenceFinder.cs). You can probably use it as is, but it's even better if you customize it. More on that later.

How to use it

First, here's how to use it.

Installing

Put the .cs file above into an Editor folder somewhere in your project. Kayac/AssetReferenceFinder will then appear in the menu.

Using it

In the Project view, select the folder containing the assets you don't want the app itself to reference. The idea is to point it at the folder where you keep your AssetBundle material.

Next, choose Kayac/AssetReferenceFinder from the menu.

A text file called "AssetReferenceFinder.txt" is then created in the project folder, and every reference from an asset outside the specified folder to an asset inside it gets written there.

Depending on the size of the project, it may take anywhere from a few seconds to a few tens of seconds before the writing finishes and the Unity Editor is usable again. Patience and a generous spirit, please.

I can't show you output from a real product (it has far too many problems in it), but it looks roughly like this.

Asset/Prefab/hoge.prefab
    Asset/AssetBundleResources/Character/hero.png
    Asset/AssetBundleResources/Animation/hero.anim

You can see that the prefab hoge references two things under AssetBundleResources that very much look like they shouldn't be in the app itself. Time to fix it.

Note that besides the selected folder, it automatically searches Resources and StreamingAssets folders as well. Those are for things you load by path at run time, so if you're going to reference something statically, it's better to put it somewhere else. I've also heard that startup gets slower as Resources grows, so nothing good comes of putting things there.

Motivation

Why did I make this?

It's obvious, really: to prevent the accident where material you turned into AssetBundles precisely to shrink the app ends up inside the app anyway. The app gets bigger for no reason. And since the AssetBundles are probably delivered separately as well, you end up shipping multiple copies of the same thing.

Another problem with this accident is that "something you planned to deliver later as an AssetBundle reaches customers early". If the art for a new character meant for an event a month from now is actually in the app, anyone who digs into the app will spot it right away.

In fact, it's safer to keep AssetBundle data in a project separate from the app itself. Then you can never reference it by mistake, and team members who don't work with the AssetBundle data spend less time waiting on imports and git pulls.

However, some teams turn whole scenes or prefabs, scripts and shaders included, into AssetBundles, and then you can't split the project. If you have no choice but to keep everything in one project, you'll probably need an inspection tool like this one.

Implementation

The menu entry point

public class AssetReferenceFinder
{
    [MenuItem("Kayac/AssetRefereneFinder")]
    public static void EntryPoint()
    {
        var instance = new AssetReferenceFinder();
        instance.Process();
    }

All you need is a static function with the MenuItem attribute so it shows up in the menu, and the processing happens there. To make it easy to hold state, it creates an instance with new and calls a method on it.

Getting the dependencies

To actually check dependencies, use AssetDatabase.GetDependencies(). Since every asset gets checked here, it's enough to find only the assets each one references directly; no recursive search is needed. Pass false as the second argument.

Customizing

The tool as presented here has no product-specific customization at all, so it's probably awkward to use in practice.

  • Specify the folders you don't want referenced in code, so there's no need to select them
  • References to Resources are allowed, so that feature isn't needed
  • Freezing the editor completely is a problem, so yield every so often in a coroutine to keep the editor usable
  • Output to an external text file is hard to read, so make a window, show the list there, and jump to the asset when an item is clicked

You could make all sorts of modifications like these.

Bonus: similar processing

"Checking asset dependencies" comes up for quite a variety of purposes.

One is "make a list of assets that aren't referenced from anywhere, and maybe even delete them automatically", which can be done in much the same way as this tool. It doesn't hurt to have one ready.

Another fairly common request is "I want to find the assets that reference a certain asset". If it were "I want to list the assets that a certain asset references", one GetDependencies call would do it, but the direction is reversed, so as in this tool you end up calling AssetDatabase.GetDependencies() on every asset. In that case, it's better to dump the reference relationships of all assets to text first. That way, when you want to check several assets, you don't have to run it several times, so it's fast.

Going further, there's also "I want to identify the GameObject that references a certain asset". At that point GetDependencies() isn't enough, and you end up parsing the yaml of prefabs and scenes yourself. We do this at our company too, and I expect someone else will write about it someday.