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.