To load assets from an AssetBundle, you must first load the AssetBundle itself and then load the assets from it.
You can use the following APIs to load AssetBundles:
AssetBundle
class, for example AssetBundle.LoadFromFile
. This class has a range of loading methods depending on the AssetBundle location and whether you want to load it synchronously or asynchronously.UnityWebRequestAssetBundle.GetAssetBundle
.Refer to the API references for these classes for the full range of AssetBundle loading methods available and usage examples.
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.
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.
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.
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:
If you must use Unload(false)
, unwanted objects can only be unloaded in one of the following ways:
Resources.UnloadUnusedAssets
.Resources.UnloadUnusedAssets
.