Code Snippets

Below are some code snippets that perform common Addressables tasks, which you can reference when developing your projects.

Warning

Copying the snippets without modification may cause memory leaks in your project. The Addressables.Release() calls are sometimes omitted for brevity. View Best Practices on how to release these assets from memory.

Loading an Addressable asset

// Namespace imports
using UnityEngine.AddressableAssets;

// Class fields
public AssetReference assetRef; // Assign Addressable asset in inspector

public IEnumerator Load()
{
// Using asset reference (recommended)
yield return Addressables.LoadAssetAsync<AudioClip>(assetRef);

// By string keys
yield return Addressables.LoadAssetAsync<AudioClip>("key_name");
yield return Addressables.LoadAssetAsync<Texture2D>("label_name");
yield return Addressables.LoadAssetAsync<GameObject>("4267d857dc01d095e06227cba21b401c"); // hardcoded asset GUID

// Instantiating the objects (creating instances of the asset).
yield return Addressables.InstantiateAsync("key_name", transform.position);
...
}
// Namespace imports
using UnityEngine.AddressableAssets;

// Class fields
public AssetReference assetRef; // Assign Addressable asset in inspector

public IEnumerator Load()
{
// Using asset reference (recommended)
yield return Addressables.LoadAssetAsync<AudioClip>(assetRef);

// By string keys
yield return Addressables.LoadAssetAsync<AudioClip>("key_name");
yield return Addressables.LoadAssetAsync<Texture2D>("label_name");
yield return Addressables.LoadAssetAsync<GameObject>("4267d857dc01d095e06227cba21b401c"); // hardcoded asset GUID

// Instantiating the objects (creating instances of the asset).
yield return Addressables.InstantiateAsync("key_name", transform.position);
...
}

See also: Quickstart Guide for a more detailed tutorial on loading Addressable assets in your project.

Downloading Addressable assets ahead of time

If you need to download an asset and its dependencies in the background, before it gets loaded (e.g. when an avatar gets close to the point of interest), you can use Addressables.DownloadDependenciesAsync() to achieve this.

This can cut the perceived latency that occurs when downloading an asset for the first time.

// Namespace imports
using UnityEngine.AddressableAssets;

// Class fields
public AssetReferenceGameObject skeletonEnemy; // Assign Addressable asset in inspector

public IEnumerator PreloadEnemy()
{
// Pre-download the bundles ahead of time (when avatar gets close).
yield return Addressables.DownloadDependenciesAsync(skeletonEnemy);
}

public IEnumerator LoadEnemy()
{
// If called after PreloadEnemy(), the asset load should complete much faster!
yield return Addressables.LoadAssetAsync<GameObject>(skeletonEnemy);
}
// Namespace imports
using UnityEngine.AddressableAssets;

// Class fields
public AssetReferenceGameObject skeletonEnemy; // Assign Addressable asset in inspector

public IEnumerator PreloadEnemy()
{
// Pre-download the bundles ahead of time (when avatar gets close).
yield return Addressables.DownloadDependenciesAsync(skeletonEnemy);
}

public IEnumerator LoadEnemy()
{
// If called after PreloadEnemy(), the asset load should complete much faster!
yield return Addressables.LoadAssetAsync<GameObject>(skeletonEnemy);
}

Getting the download progress

If you need to display download progress to the user for UX reasons, you can fetch the download status from the AsyncOperationHandle periodically and wait for DownloadStatus.IsDone to be true instead of waiting on the AsyncOperationHandle itself.

// Namespace imports
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

// Class fields
public AssetReference assetRef; // Assign Addressable asset in inspector

private IEnumerator DownloadStatusDemoCoroutine()
{
AsyncOperationHandle handle = Addressables.LoadAssetAsync(assetRef); // or use string key
DownloadStatus status = handle.GetDownloadStatus();
while (!status.IsDone)
{
UpdateProgressBar(status.Percent); // Update some UI. Percent returns value from 0 to 1.
yield return null;
status = handle.GetDownloadStatus(); // Check the status again after a frame.
}
// Done loading!

// This was a demo on how to fetch download progress, so we don't actually need the asset.
Addressables.Release(handle);
}
// Namespace imports
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

// Class fields
public AssetReference assetRef; // Assign Addressable asset in inspector

private IEnumerator DownloadStatusDemoCoroutine()
{
AsyncOperationHandle handle = Addressables.LoadAssetAsync(assetRef); // or use string key
DownloadStatus status = handle.GetDownloadStatus();
while (!status.IsDone)
{
UpdateProgressBar(status.Percent); // Update some UI. Percent returns value from 0 to 1.
yield return null;
status = handle.GetDownloadStatus(); // Check the status again after a frame.
}
// Done loading!

// This was a demo on how to fetch download progress, so we don't actually need the asset.
Addressables.Release(handle);
}

Fetch the download size of the asset bundle

If you need to know how big the bundle is before downloading it, then you can use Addressables.GetDownloadSizeAsync(). Keep in mind that it will return 0 if the Addressable was already cached.

// Namespace imports
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

// Class fields
public AssetReference assetRef; // Assign Addressable asset in inspector

private IEnumerator GetAddressableDownloadSizeCoroutine()
{
// Returns 0 if the Addressable was already cached.
AsyncOperationHandle<long> handle = Addressables.GetDownloadSizeAsync(assetRef); // or use string key
yield return handle;
Debug.Log($"The download size for this asset is: {handle.Result} bytes");
Addressables.Release(handle);
}
// Namespace imports
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

// Class fields
public AssetReference assetRef; // Assign Addressable asset in inspector

private IEnumerator GetAddressableDownloadSizeCoroutine()
{
// Returns 0 if the Addressable was already cached.
AsyncOperationHandle<long> handle = Addressables.GetDownloadSizeAsync(assetRef); // or use string key
yield return handle;
Debug.Log($"The download size for this asset is: {handle.Result} bytes");
Addressables.Release(handle);
}