Version: Unity 6.1 (6000.1)
Language : English
AssetBundle file format reference
Handling dependencies between AssetBundles

Loading assets from AssetBundles

To load assets from an AssetBundle, you must first load the AssetBundle itself and then load the assets from it.

Loading AssetBundles

You can use the following APIs to load AssetBundles:

Refer to the API references for these classes for the full range of AssetBundle loading methods available and usage examples.

Loading assets

Once an AssetBundle is loaded, you can use the AssetBundle class to load individual assets from it as follows.

Use LoadAsset to load a single asset, for example the root GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary
of a prefabAn asset type that allows you to store a GameObject complete with components and properties. The prefab acts as a template from which you can create new object instances in the scene. More info
See in Glossary
, synchronously:

GameObject gameObject = loadedAssetBundle.LoadAsset<GameObject>(assetName);

Use LoadAllAssets to load all assets as follows:

Unity.Object[] objectArray = loadedAssetBundle.LoadAllAssets();

This returns an array with all the root Object of each asset.

The methods in the previous snippets return either the type of object to be loaded or an array of objects. The asynchronous counterparts of these methods return an AssetBundleRequest instead. You must wait for this operation to complete before accessing the asset.

To load a single asset asynchronously:

AssetBundleRequest request = loadedAssetBundleObject.LoadAssetAsync<GameObject>(assetName);
yield return request; // or await request;
var loadedAsset = request.asset;

To load all assets asynchronously:

AssetBundleRequest request = loadedAssetBundle.LoadAllAssetsAsync();
yield return request; // or await request;
var loadedAssets = request.allAssets;

For more information and a full code example, refer to the AssetBundle API reference.

Loading AssetBundle manifests

You can load an AssetBundle manifest into an instance of the AssetBundleManifest class to get information including dependency data, hash data, and variant data for built AssetBundles.

This is especially useful when managing dependencies between AssetBundles. The manifest object makes dynamically finding and loading dependencies possible, so you don’t have to hardcode all the AssetBundle names and their relationships explicitly in your code.

For more information and a code example, refer to Handling dependencies between AssetBundles.

Managing loaded AssetBundles

Unity recommends using the Addressables package for managing AssetBundles, dependencies, and assets, as it simplifies the process. For manual management, understanding proper AssetBundle loading and unloading is critical to avoid memory duplication or missing objects.

Recommended unload strategies

The AssetBundle.Unload function removes the AssetBundle header and associated structures from memory, with the boolean argument determining whether loaded objects are also unloaded.

Most projects should use AssetBundle.Unload(true) to prevent object duplication. Common strategies include:

  • Defined unload points: Unload transient AssetBundles at specific times, such as during level transitions or loading screens.
  • Reference-counting: Track object usage and unload AssetBundles only when all constituent objects are unused. For an example implementation, refer to Example unload strategy: reference counting.

If you must use Unload(false), unwanted objects can only be unloaded in one of the following ways:

  • References elimination: Remove all references to an object and call Resources.UnloadUnusedAssets.
  • Non-additive sceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
    See in Glossary
    loading: Load a new scene non-additively to destroy current scene objects, automatically invoking Resources.UnloadUnusedAssets.

Additional resources

AssetBundle file format reference
Handling dependencies between AssetBundles