4
\$\begingroup\$

Loading assets from an asset bundle is done explicitly. You give the bundle the name of the asset to load, then it returns it( either now or later. ) However, loading dependencies is done completely automatically.

How is this done? Is the asset bundle completely removed from memory after the required objects are loaded? Are all the bundle's assets loaded, or just the necessary ones? How can these assets be accessed outside of the reference that caused the dependency in the first place?

I ask this because I am trying to collect some type information about the assets loaded in asset bundles. I need to be aware of all assets of a certain type that are loaded into the game.

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

The asset bundle system does not automatically remove anything nor does it load dependencies automatically. Dependencies are included in asset bundles automatically, but actually exist in each asset bundle that depends on them, unless you manually removed them. For example, if you have a 2 different player models, Hero1 and Hero2. They both share a Gun texture. If you put hero1 into an asset bundle, that asset bundle will contain Hero1 and the Gun Texture. If you put hero2 into an asset bundle, it will contain Hero2 and the Gun Texture. If you load both asset bundles simultaneously you will have 2 copies of the gun texture loaded. The way to get around this is with the asset bundle dependencies. The way these work is that at build time, you would create an asset bundle with the Gun texture, and then call AssetBundle.PushAssetDependencies(), passing it the gun texture. You can then build the Hero1 and Hero2 asset bundles, and each of them will not include the Gun texture. At runtime, it will be up to you to load the Gun texture bundle along with the Hero1 and/or Hero2 asset bundles, but it will be only loaded a single time.

The memory management of asset bundles gets a bit more complex than this too, as far as memory usage is concerned. If you have a compressed asset bundle, what will happen when you load it is the entire compressed asset bundle is loaded into memory. From there, individual assets are decompressed and loaded. Once all the assets have been loaded, you can unload the asset bundle and you'll still have the assets inside in memory (assuming something is referencing them). The downside of this approach is that at some point, you'll have both the entire compressed asset bundle in memory, and simultaneously the uncompressed asset bundles in memory as well. With the new parameter to AssetBundle.Unload, you can specify whether you want it to destroy only the compressed asset bundle or whether you want it to free the uncompressed copy as well.

The other way to generate asset bundles is to generate them as uncompressed asset bundles. If you generate uncompressed asset bundles, they will be larger, which will negatively affect your download time in getting the assets, but your loads will be much faster. When you load an uncompressed asset bundle, there isn't a second copy of all of your data, the objects that reference data in your asset bundle are pointed to the actual asset bundle in memory. When you unload the asset bundle, anything pointing to things inside the asset bundle are unloaded, and so you can't unload the asset bundle while still referencing things inside it. The upside is that you avoid the extra memory of having both loaded, and you don't have the performance hit that is incurred by the decompression, since its loaded in place.

\$\endgroup\$
3
  • \$\begingroup\$ With uncompressed asset bundles, when you load the asset bundle, are all assets in the bundle loaded? Is there any way to load only select assets from the bundle? \$\endgroup\$
    – Ben
    Commented Jan 7, 2016 at 4:01
  • \$\begingroup\$ Not for the purposes of saving memory, no, the entire bundle gets loaded into memory. This information may be out of date, as I did this investigation a couple years ago, but in the case of a compressed bundle, the entire compressed bundle gets loaded into memory. It then allocates memory for the uncompressed version of it, and decompresses it into that memory, for a total memory usage that is greater than the size of the compressed bundle. If you use an uncompressed bundle, it loads it into memory directly, so you only have the size of the uncompressed bundle to deal with. \$\endgroup\$
    – Tom K
    Commented Feb 22, 2016 at 21:50
  • \$\begingroup\$ It turns out if you open an uncompressed asset bundle without using WWW, it streams it as you ask for assets. \$\endgroup\$
    – Ben
    Commented Feb 23, 2016 at 3:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .