API for accessing the content of AssetBundle files.
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.
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); } }
memoryBudgetKB | Controls the size of the shared AssetBundle loading cache. Default value is 1MB. |
isStreamedSceneAssetBundle | Return true if the AssetBundle contains Unity Scene files |
Contains | 检查 AssetBundle 是否包含特定对象。 |
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 | 卸载 AssetBundle 释放其数据。 |
UnloadAsync | 卸载捆绑包中的资源。 |
GetAllLoadedAssetBundles | Get an enumeration of all the currently loaded AssetBundles. |
LoadFromFile | 从磁盘上的文件同步加载 AssetBundle。 |
LoadFromFileAsync | 从磁盘上的文件异步加载 AssetBundle。 |
LoadFromMemory | Synchronously load an AssetBundle from a memory region. |
LoadFromMemoryAsync | Asynchronously load an AssetBundle from a memory region. |
LoadFromStream | 从托管 Stream 同步加载 AssetBundle。 |
LoadFromStreamAsync | 从托管 Stream 异步加载 AssetBundle。 |
RecompressAssetBundleAsync | 异步将下载/存储的 AssetBundle 从一个 BuildCompression 再压缩为另一个。 |
UnloadAllAssetBundles | 卸载当前已加载的所有 AssetBundle。 |
GetInstanceID | Gets the instance ID of the object. |
ToString | 返回对象的名称。 |
Destroy | 移除 GameObject、组件或资源。 |
DestroyImmediate | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 |
DontDestroyOnLoad | 在加载新的 Scene 时,请勿销毁 Object。 |
FindAnyObjectByType | Retrieves any active loaded object of Type type. |
FindFirstObjectByType | Retrieves the first active loaded object of Type type. |
FindObjectsByType | Retrieves a list of all loaded objects of Type type. |
Instantiate | 克隆 original 对象并返回克隆对象。 |
bool | 该对象是否存在? |
operator != | 比较两个对象是否引用不同的对象。 |
operator == | 比较两个对象引用,判断它们是否引用同一个对象。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.