Use an Addressable asset
To load and use an Addressable asset, you can:
- Use an AssetReference that references the asset
- Use its address string
- Use a label assigned to the asset
Refer to Loading assets for more detailed information about loading Addressable assets.
Loading Addressable assets uses asynchronous operations. Refer to Operations for information about the different ways to approach asynchronous programming in Unity scripts.
Tip
You can find more involved examples of how to use Addressable assets in the Addressables Sample repository.
Use AssetReferences
To use an AssetReference
, add an AssetReference
field to a MonoBehaviour
or ScriptableObject
. After you create an object of that type, you can assign an asset to the field in your object's Inspector window.
Note
If you assign a non-Addressable asset to an AssetReference field, Unity automatically makes that asset Addressable and adds it to your default Addressables group. AssetReferences also let you use Addressable assets in a Scene that isn't itself Addressable.
Unity doesn't load or release the referenced asset automatically; you must load and release the asset using the Addressables
API:
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
internal class LoadWithReference : MonoBehaviour
{
// Assign in Editor
public AssetReference reference;
// Start the load operation on start
void Start()
{
AsyncOperationHandle handle = reference.LoadAssetAsync<GameObject>();
handle.Completed += Handle_Completed;
}
// Instantiate the loaded prefab on complete
private void Handle_Completed(AsyncOperationHandle obj)
{
if (obj.Status == AsyncOperationStatus.Succeeded)
{
Instantiate(reference.Asset, transform);
}
else
{
Debug.LogError($"AssetReference {reference.RuntimeKey} failed to load.");
}
}
// Release asset when parent object is destroyed
private void OnDestroy()
{
reference.ReleaseAsset();
}
}
Refer to Loading an AssetReference for additional information about loading AssetReferences.
Load by address
You can use the address string to load an asset:
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
internal class LoadWithAddress : MonoBehaviour
{
// Assign in Editor or in code
public string address;
// Retain handle to release asset and operation
private AsyncOperationHandle<GameObject> handle;
// Start the load operation on start
void Start()
{
handle = Addressables.LoadAssetAsync<GameObject>(address);
handle.Completed += Handle_Completed;
}
// Instantiate the loaded prefab on complete
private void Handle_Completed(AsyncOperationHandle<GameObject> operation)
{
if (operation.Status == AsyncOperationStatus.Succeeded)
{
Instantiate(operation.Result, transform);
}
else
{
Debug.LogError($"Asset for {address} failed to load.");
}
}
// Release asset when parent object is destroyed
private void OnDestroy()
{
Addressables.Release(handle);
}
}
Remember that every time you load an asset, you must also release it.
Refer to Loading a single asset for more information.
Load by label
You can load sets of assets that have the same label in one operation:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
internal class LoadWithLabels : MonoBehaviour
{
// Label strings to load
public List<string> keys = new List<string>() {"characters", "animals"};
// Operation handle used to load and release assets
AsyncOperationHandle<IList<GameObject>> loadHandle;
// Load Addressables by Label
void Start()
{
float x = 0, z = 0;
loadHandle = Addressables.LoadAssetsAsync<GameObject>(
keys, // Either a single key or a List of keys
addressable =>
{
//Gets called for every loaded asset
if (addressable != null)
{
Instantiate<GameObject>(addressable,
new Vector3(x++ * 2.0f, 0, z * 2.0f),
Quaternion.identity,
transform);
if (x > 9)
{
x = 0;
z++;
}
}
}, Addressables.MergeMode.Union, // How to combine multiple labels
false); // Whether to fail if any asset fails to load
loadHandle.Completed += LoadHandle_Completed;
}
private void LoadHandle_Completed(AsyncOperationHandle<IList<GameObject>> operation)
{
if (operation.Status != AsyncOperationStatus.Succeeded)
Debug.LogWarning("Some assets did not load.");
}
private void OnDestroy()
{
// Release all the loaded assets associated with loadHandle
Addressables.Release(loadHandle);
}
}
Refer to Loading multiple assets for more information.