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.
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).
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.
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