How do you properly load Asset Bundles with dependencies?

I’ve read the Unity docs about Asset Bundle dependencies and seen several examples of using Push/PopAssetDependencies when building the bundles. However, I have yet to find any examples of actually loading these dependent bundles at runtime. Can someone show me how that should be done?

So, for example, say I have multiple character prefabs that all share the same huge skin texture. And let’s say I want to make each prefab its own bundle. Then, ideally, I want each prefab bundle to be dependent on a separate skin texture bundle. I know I need to load the texture bundle first. But does just calling Load on the texture bundle magically make the reference when the prefab bundle is loaded?

Answering my own question here. Yes, it more or less happens magically, which is nice. Here’s a simplified version of the code I used:

// Load the dependency bundle required by the prefab bundle
// This contains things like Textures that wouldn't be instantiated separately
WWW www = WWW.LoadFromCacheOrDownload( depBundleURL, depBundleVersion );
yield return www;
www.assetBundle.LoadAll( );

// Load the prefab bundle containing references to assets in the dependency bundle
// This bundle contains things like prefabs that will actually be instantiated
www = WWW.LoadFromCacheOrDownload( prefabBundleURL, prefabBundleVersion );
yield return www;
GameObject prefab = www.assetBundle.Load( prefabName );

That’s it. The first LoadAll call loads all the dependency assets into memory but apparently you don’t need to store references to them or anything. I assume they are referenced later by GUID or something because loading the prefab and instantiating it just works with no extra effort. And it’s always a welcome surprise when Unity just works!

They should have called it www.LoadAssetBundleHead() because it is putting the asset bundle header into memory. Basically once the header is in memory Unity will know to go load stuff from that bundle when needed, be it explicit load calls or dependencies.

e.g.

AssetBundle-A contains materialA
AssetBundle-B contains a prefab-A that uses materialA

When you called www.assetBundle for the loading of AssetBundle-A it will load this bundle’s header into memory. This bundle’s header has information that is holds MaterialA.

Now when you load AssetBundleB using the same method, then you explicitly load prefab-A from AssetBundleB, unity knows that prefab-A has a dependency of MaterialA in AssetBundleA, so it will go and load MaterialA from AssetBundleA automatically for you.