AssetBundle
API for accessing the content of AssetBundle files.
Read time 5 minutesLast updated 2 days ago
Definition
- Type: Class
- Namespace: UnityEngine
- Assembly: UnityEngine.AssetBundleModule
- Inherits from: Object
public class AssetBundle : Object
Remarks
This class exposes an API, via static methods, for loading and managing AssetBundles.
This same class offers non-static methods and properties that expose the contents of a specific loaded AssetBundle, including the ability to load an Asset from within an AssetBundle.
Create AssetBundles by calling BuildPipeline.BuildAssetBundles or using the Addressables package. The build process generates one or more AssetBundle files, and each AssetBundle file contains a serialized instance of this class.
Note: AssetBundle.LoadAsset, and the other Load methods, do not support loading Scenes from AssetBundles. Instead, in runtime you load the streamed scene AssetBundle and then call SceneManager.LoadScene or SceneManager.LoadSceneAsync with the Scene name. In the Editor it is not supported to load Scenes from AssetBundles so calls to EditorSceneManager.OpenScene will fail with an error that the Scene file is not found.
Additional Resources: Intro to AssetBundles, UnityWebRequestAssetBundle.GetAssetBundle, BuildPipeline.BuildAssetBundles.
Examples
using System.Collections;using UnityEngine;using UnityEngine.Networking;public class SampleBehaviour : MonoBehaviour{ IEnumerator Start() { var uwr = UnityWebRequestAssetBundle.GetAssetBundle("https://myserver/myBundle.unity3d"); yield return uwr.SendWebRequest(); // Get an asset from the bundle and instantiate it. AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr); var loadAsset = bundle.LoadAssetAsync<GameObject>("Assets/Players/MainPlayer.prefab"); yield return loadAsset; Instantiate(loadAsset.asset); bundle.Unload(true); }}
Properties
Property | Description |
|---|---|
| isStreamedSceneAssetBundle | Return true if the AssetBundle contains Unity Scene files |
Static Properties
Property | Description |
|---|---|
| memoryBudgetKB | Controls the size of the shared AssetBundle loading cache. Default value is 1MB. |
Methods
Method | Description |
|---|---|
| Contains | Check if an AssetBundle contains a specific object. |
| GetAllAssetNames | Return all Asset names in the AssetBundle. |
| GetAllScenePaths | Return all the names of Scenes in the AssetBundle. |
| LoadAllAssets | Loads all Assets contained in the AssetBundle synchronously. |
| LoadAllAssetsAsync | Loads all Assets contained in the AssetBundle asynchronously. |
| LoadAsset | Synchronously loads an Asset from the AssetBundle. |
| LoadAssetAsync | Asynchronously loads an Asset from the bundle. |
| LoadAssetWithSubAssets | Loads Asset and sub Assets from the AssetBundle synchronously. |
| LoadAssetWithSubAssetsAsync | Loads Asset and sub Assets from the AssetBundle asynchronously. |
| Unload | Unloads an AssetBundle freeing its data. |
| UnloadAsync | Unloads assets in the bundle. |
Static Methods
Method | Description |
|---|---|
| GetAllLoadedAssetBundles | Get an enumeration of all the currently loaded AssetBundles. |
| LoadFromFile | Synchronously loads an AssetBundle from a file on disk. |
| LoadFromFileAsync | Asynchronously loads an AssetBundle from a file on disk. |
| LoadFromMemory | Synchronously load an AssetBundle from a memory region. |
| LoadFromMemoryAsync | Asynchronously load an AssetBundle from a memory region. |
| LoadFromStream | Synchronously loads an AssetBundle from a managed Stream. |
| LoadFromStreamAsync | Asynchronously loads an AssetBundle from a managed Stream. |
| RecompressAssetBundleAsync | Asynchronously recompress a downloaded/stored AssetBundle from one BuildCompression to another. |
| UnloadAllAssetBundles | Unloads all currently loaded AssetBundles. |
Inheritance
Operators
Operator | Description |
|---|---|
| operator == | Compares two object references to see if they refer to the same object. |
| bool | Determines whether the object exists. |
| operator != | Compares if two objects refer to a different object. |