- Home /
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?
How do you make the character prefab bundle/s without including the texture? Do you code it explicitly not to include that dependency or something like that?
Answer by casimps1 · Apr 09, 2013 at 03:40 PM
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!
If you www.assetBundle.Unload(false) on the first bundle, will the second bundle still find it's asset references that were loaded by the LoadAll()? Or, do you have to have both bundles' file data in memory simultaneously for dependencies to be resolved?
In my experience you don't need to call dependencyWww.assetBundle.LoadAll( );
.
Just accessing the www.assetBundle
getter is enough to make dependencies work, which I think is a pretty confusing API decision.
Basically you need to do
WWW www = WWW.LoadFromCacheOrDownload( depBundleURL, depBundleVersion );
yield return www;
var temp = www.assetBundle;
which is not intuitive at all. The .assetBundle
getter is a getter and an intialisation method combined.
how to secure assest bundles from being used into other apps?
Answer by NestorAlgieri · Sep 26, 2018 at 04:41 AM
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.