Version: 1.3
LanguageEnglish
  • C#

AssetBundle

class in UnityEngine

/

Inherits from:Object

/

Implemented in:UnityEngine.AssetBundleModule

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

AssetBundles let you stream additional assets via the UnityWebRequest class and instantiate them at runtime.

Create AssetBundles by calling BuildPipeline.BuildAssetBundles or using the Scriptable Build Pipeline package.

A bundle built for any of the standalone platforms can only be loaded on that platform, for example a bundle build on iOS is not compatible with Android. This is because shaders, textures and other types of data are built into platform-specific formats based on the BuildTarget.

Additional resources: UnityWebRequestAssetBundle.GetAssetBundle, Loading Resources at Runtime, 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); } }

Static Properties

memoryBudgetKBControls the size of the shared AssetBundle loading cache. Default value is 1MB.

Properties

isStreamedSceneAssetBundleReturn true if the AssetBundle is a streamed Scene AssetBundle.

Public Methods

ContainsCheck if an AssetBundle contains a specific object.
GetAllAssetNamesReturn all asset names in the AssetBundle.
GetAllScenePathsReturn all the Scene asset paths (paths to *.unity assets) in the AssetBundle.
InstantiateAsyncAllow the user to implement asynchronous instantiation.
LoadAllAssetsLoads all assets contained in the asset bundle that inherit from type.
LoadAllAssetsAsyncLoads all assets contained in the asset bundle asynchronously.
LoadAssetLoads asset with name from the bundle.
LoadAssetAsyncAsynchronously loads asset with name from the bundle.
LoadAssetWithSubAssetsLoads asset and sub assets with name from the bundle.
LoadAssetWithSubAssetsAsyncLoads asset with sub assets with name from the bundle asynchronously.
PreDownloadAsAssetAsyncAsynchronously predownloads autostreaming assets in obj from the bundle and checks if loaded .
UnloadUnloads an AssetBundle freeing its data.
UnloadAsyncUnloads assets in the bundle.

Static Methods

GetAllLoadedAssetBundlesTo use when you need to get a list of all the currently loaded Asset Bundles.
LoadFromFileSynchronously loads an AssetBundle from a file on disk.
LoadFromFileAsyncAsynchronously loads an AssetBundle from a file on disk.
LoadFromMemorySynchronously create an AssetBundle from a memory region.
LoadFromMemoryAsyncAsynchronously create an AssetBundle from a memory region.
LoadFromStreamSynchronously loads an AssetBundle from a managed Stream.
LoadFromStreamAsyncAsynchronously loads an AssetBundle from a managed Stream.
RecompressAssetBundleAsyncAsynchronously recompress a downloaded/stored AssetBundle from one BuildCompression to another.
SetAssetBundleDecryptKeySet DecryptKey before loading assetbundes which were built with a given EncryptKey.
UnloadAllAssetBundlesUnloads all currently loaded AssetBundles.

Inherited Members

Properties

hideFlagsShould the object be hidden, saved with the Scene or modifiable by the user?
nameThe name of the object.

Public Methods

GetInstanceIDGets the instance ID of the object.
ToStringReturns the name of the object.

Static Methods

DestroyRemoves a GameObject, component or asset.
DestroyImmediateDestroys the object obj immediately. You are strongly recommended to use Destroy instead.
DontDestroyOnLoadDo not destroy the target Object when loading a new Scene.
FindAnyObjectByTypeRetrieves any active loaded object of Type type.
FindFirstObjectByTypeRetrieves the first active loaded object of Type type.
FindObjectOfTypeReturns the first active loaded object of Type type.
FindObjectsByTypeRetrieves a list of all loaded objects of Type type.
FindObjectsOfTypeGets a list of all loaded objects of Type type.
InstantiateClones the object original and returns the clone.
InstantiateAsyncCaptures a snapshot of the original object (that must be related to some GameObject) and returns the AsyncInstantiateOperation.

Operators

boolDoes the object exist?
operator !=Compares if two objects refer to a different object.
operator ==Compares two object references to see if they refer to the same object.