Trying to understand AssetBundles

I’ve read unity documentation regarding asset bundles and watched some tutorials, however few things are still unclear to me.

for example I have many large textures that I want to place in a separate asset bundle. I also have prefabs that are using these textures which I dont want to place in that asset bundle… How would my prefab know that a texture AB is loaded? Do I have to manually update its texture property?

Furthermore, now I drag the prefab into a scene and press play (or use Instantiate method), texture still shows even without loading an AB. Why? Does the prefab also need to be in AB for texture AB to actually work?

By default, unity will include any objects references by your prefab into the build. So in this case, your including the texture in the build and in the asset bundle. To prevent this, you need to load the prefab from an asset bundle as well.

The best way to think of asset bundles are as a separate file system from your project structure. To use them correctly, you really need to make sure that those assets are only referenced by things in other bundles (otherwise you’ll end up with the asset in both a bundle and the app), and that every asset needed by a bundle is also in a bundle as well (otherwise you risk the chance of loading that asset twice).

so if I understand correctly, I must not have any references to assets in AB by objects that are not in AB?

For what you described, that is correct.

E.g. you have a prefab which has 1 texture in it. If you load that 1 texture from an asset bundle, then the bundle will contain the 1 texture, and the base game will contain the 1 prefab + 1 referenced texture (doubling up the texture twice). There is nothing that will break when you do this, but it is using up extra space with no benefit.

But one option would be for the Prefab to have a script on it which loads the texture out of the asset bundle using AssetBundle.LoadAsset and thus removing the direct reference to that texture.

Basically you would treat Asset Bundles as a remotely-downloaded “Resources” directory, which you can load from using AssetBundle.LoadAsset instead of Resources.Load. In fact part of our game has a helper Resources class which will scan for the asset in either resources or asset bundles and load it wherever it can be found.

You don’t need to explicitly load the texture via a script. Once the prefab is in an asset bundle, it will include a copy of all the assets needed (referenced) by that prefab. If those assets happen to be in an asset bundle, then it will create a dependency on that asset bundle and not include it in it’s own bundle. Breaking, it down, if we have a case like so:

MyPrefab
— Mesh
------ Material
---------- Texture

SecondPrefab
— Second Mesh
------ Second Material
---------- Texture (same texture as above)

Here we have two prefabs which share a common texture. If we simply put each prefab into an asset bundle, and do not reference any of these assets anywhere else in the build, then none of this will be included in the build and the asset bundles will contain all the assets needed for that prefab (mesh, etc). However, the texture will be duplicated into both bundles, and you’ll have two copies of this texture in memory if both prefabs are loaded/instanced at once.

However, if we move the texture into it’s own bundle, both of the prefab bundles will create a dependency to that bundle. At load time, you will need to get the dependency list from the assetbundle.manifest and make sure that the texture’s bundle is loaded first. Once that’s done, you can load both prefabs and they will share the same texture.

Now, if your working on a team of people; it’s likely that your assets get used in ways you didn’t expect. Lets imagine someone uses one of those meshes for something else. Since the mesh isn’t bundled, it’s now included in your app and duplicated in memory. So you go ahead and put that into a bundle for them; great, now it’s not in the app anymore, but it’s still duplicated in memory because the mesh isn’t in a bundle and is included in two bundles. So you have to move that mesh into a bundle as well.

Basically, the system is a mess of potential, easy to make errors, which will eventually bite you if you are not extremely diligent. It’s clearly an afterthought system, bolted on and working around the existing build system, with little in the way of tools to help. It works best if your assets are extremely atomic - ie: here is a level which uses no shared resources, or a prefab with no shared resources, or if you don’t care about memory and download waste (not likely).

I’m also not sure if the 5.0 version is actually an improvement (yet). Basically, it builds the dependency graph for you (yay), but switches the error metric from “Hey, why didn’t my texture show up” to “Hey, how come I have 7 copies of this texture in memory?”, which just delays the inevitable process of having to manually manage everything in the most optimal way. I personally prefer when things error as soon as possible, and loudly, instead of silently working until you run out of memory.

Our project is currently around 3000+ separate asset bundles. Our app has almost no assets in it, and everything is loaded via asset bundles. It took a long time to organize the data in this manner and get all the duplicates out. I wrote a considerable amount of tooling to check the data in our tree and point out issues.

For the next project, I’m seriously considering going to a 1 asset == 1 bundle approach, where every path just gets turned into an asset bundle name for every asset at import time. This seems a little crazy, but it’s the only way you can guarantee that the user only downloads what they actually need. And basically, it just switches the workflow from “Make sure everything is broken into the correct bundles” to “group things when you can into clustered bundles”. I’d probably go this way now, but there is a bug in the asset bundle loading code which prevents you from opening more than 256 asset bundles at a time (really about 220 since unity opens about 30). Note that unity’s own recommendations are to break everything into lots of small bundles (to deal with the fact that WWW will allocate a ton of mono-memory for the downloads), and to keep asset bundles open when in use and use Unload(true) when your done with their contents. Of course, this rubs up against the 220 asset bundles open limit pretty fast, so you have to cluster your data to avoid this. Another alternative recommended by unity is to write your own asset bundle downloader and not use WWW to load them, which avoids the mono bloat issues (but not the 220 asset bundles open at once issue).

Anyway, maybe it’ll all be sorted out by version 6… But right now, it’s a giant field of land mines unless your use case is simple.

1 Like

Thanks guys this clarifies things a lot.

Would having more bundles affect your request limit on S3?

If you have one, yeah, you’re pulling more files. But the idea is that you end up in relatively the same place in terms of asset bundle counts; but rather than slowly moving everything into bundles and fighting asset duplication along the way, you start out with 1 asset == 1 bundle and move things into ‘collection bundles’ as need be. Basically, you just scan through all of the tiny bundles and look for things that can be clustered together or included in higher level bundles instead of tracking down duplicated textures at runtime, etc…

You could likely also check the references to an asset at build time and suck them into parent bundles when they have only one reference. For instance, I never load materials directly from a bundle, so if it only has one reference, assign it to be part of the bundle that references it. For the most part, every texture or mesh is already large enough to be in it’s own bundle, and prefabs/scenes usually have to be in their own bundle so you can load/unload them correctly, so most of the bloat would only come from materials, .text files, shaders, and possibly scriptable objects…

jbooth, I have a question on a prefab in an asset bundle. It contains a text with reference to a font under the Assets/Fonts and an image with a sprite referenced to an atlas within the same asset bundle.

When unloading, the reference to the prefab and atlas remains in memory due to the text using a font in Assets/Fonts. Do I really have to put the font in the asset bundle? I do have many other prefabs that use the same font.

It’ll end up in a asset bundle either way, and likely end up in memory more than once. Basically, things in an asset bundle cannot reference things in your build- so when Unity encounters a dependency on something not in an asset bundle, it copies that data into the asset bundle that needs that data.

So if you have 10 prefabs which use a font, each in their own asset bundle, and that font is not in it’s own asset bundle, each prefab will get a full copy of that font added to it, and if you load all ten prefabs, you will have ten copies of your font in memory. You can easily end up with dozens of copies of a texture this way - the whole system is poorly thought out, IMO.