Class AddressableExtensions
Provides extension methods for Unity Addressables types that enable a fluent, readable API
for chaining completion callbacks, converting handles to Unity's Awaitable pattern,
and loading assets directly from AssetReference or string address values.
Inherited Members
Namespace: Scylla.Core.Util.Addressables
Assembly: ScyllaCore.dll
Syntax
public static class AddressableExtensions
Remarks
All members of this class are only compiled when the SCYLLA_HAS_ADDRESSABLES
scripting define is present (i.e. the Unity Addressables package is installed). Without the
package, the entire extension class body is stripped by the preprocessor and none of the
methods are available.
Extension groups. Methods are organized into three functional groups:
- AsyncOperationHandle extensions - AutoRelease<T>(AsyncOperationHandle<T>), OnComplete<T>(AsyncOperationHandle<T>, Action<T>, Action<AddressableException>), ToAwaitable<T>(AsyncOperationHandle<T>, CancellationToken), ToResult<T>(AsyncOperationHandle<T>, CancellationToken). These allow chaining and conversion of raw operation handles.
-
AssetReference extensions -
LoadAssetAsync,LoadAssetAsyncAwaitable,InstantiateAsync,ReleaseAsset. Thin wrappers delegating to ScyllaAddressables for use on Inspector-assigned asset references. -
String address extensions -
LoadAddressable,LoadAddressableAwaitable,AddressableExists,InstantiateAddressable. Allow calling Addressables operations directly on address strings for concise call sites.
Methods
AddressableExists(string, CancellationToken)
Checks whether this address string resolves to at least one resource location in the Addressables catalog. Delegates to ExistsAsync(string, CancellationToken).
Declaration
public static Awaitable<bool> AddressableExists(this string address, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | address | The Addressables key string to look up. Returns |
| CancellationToken | cancellationToken | An optional token that can cancel the in-flight lookup. |
Returns
| Type | Description |
|---|---|
| Awaitable<bool> |
|
AddressableExists<T>(string, CancellationToken)
Checks whether this address string resolves to at least one resource location of the given asset type in the Addressables catalog. Delegates to ExistsAsync<T>(string, CancellationToken).
Declaration
public static Awaitable<bool> AddressableExists<T>(this string address, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | address | The Addressables key string to look up. Returns |
| CancellationToken | cancellationToken | An optional token that can cancel the in-flight lookup. |
Returns
| Type | Description |
|---|---|
| Awaitable<bool> |
|
Type Parameters
| Name | Description |
|---|---|
| T | The asset type to filter the location query to. |
AutoRelease<T>(AsyncOperationHandle<T>)
Registers a completion callback on the handle that automatically releases it via
Addressables.Release as soon as the operation finishes, preventing the need for
manual handle management when only the loaded result (not the handle) needs to be kept.
Declaration
public static AsyncOperationHandle<T> AutoRelease<T>(this AsyncOperationHandle<T> handle)
Parameters
| Type | Name | Description |
|---|---|---|
| AsyncOperationHandle<T> | handle | The handle to configure for automatic release on completion. |
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<T> | The same |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the handle result. |
Remarks
The release only happens if the handle is still valid at completion time. This guards against double-release in edge cases where the handle was released externally.
Be aware that once a handle is released, its Result may be garbage collected
if there are no other references. Only use AutoRelease when you plan to retrieve
the result from within the same Completed callback or immediately after awaiting,
before the handle is released.
ScyllaAddressables.LoadAsync<Sprite>("ui/icon")
.AutoRelease()
.OnComplete(sprite => icon.sprite = sprite);
InstantiateAddressable(string)
Starts the asynchronous instantiation of the prefab at this address string at the world origin,
returning a raw AsyncOperationHandle. Delegates to
InstantiateAsync(string).
Declaration
public static AsyncOperationHandle<GameObject> InstantiateAddressable(this string address)
Parameters
| Type | Name | Description |
|---|---|---|
| string | address | The Addressables key string identifying the prefab. Must not be |
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<GameObject> | An |
Exceptions
| Type | Condition |
|---|---|
| AddressableException | Thrown synchronously with NullAddress when this address
is |
InstantiateAddressable(string, Transform)
Starts the asynchronous instantiation of the prefab at this address string, parenting the result to the specified transform. Delegates to InstantiateAsync(string, Transform).
Declaration
public static AsyncOperationHandle<GameObject> InstantiateAddressable(this string address, Transform parent)
Parameters
| Type | Name | Description |
|---|---|---|
| string | address | The Addressables key string identifying the prefab. Must not be |
| Transform | parent | The |
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<GameObject> | An |
Exceptions
| Type | Condition |
|---|---|
| AddressableException | Thrown synchronously with NullAddress when this address
is |
InstantiateAddressable(string, Vector3, Quaternion)
Starts the asynchronous instantiation of the prefab at this address string at a specific world position and rotation. Delegates to InstantiateAsync(string, Vector3, Quaternion).
Declaration
public static AsyncOperationHandle<GameObject> InstantiateAddressable(this string address, Vector3 position, Quaternion rotation)
Parameters
| Type | Name | Description |
|---|---|---|
| string | address | The Addressables key string identifying the prefab. Must not be |
| Vector3 | position | The world-space position at which to place the instantiated object. |
| Quaternion | rotation | The world-space rotation to apply to the instantiated object. |
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<GameObject> | An |
Exceptions
| Type | Condition |
|---|---|
| AddressableException | Thrown synchronously with NullAddress when this address
is |
InstantiateAsync(AssetReference)
Starts the asynchronous instantiation of the prefab identified by this AssetReference
at the world origin and returns a raw AsyncOperationHandle that the caller manages.
Declaration
public static AsyncOperationHandle<GameObject> InstantiateAsync(this AssetReference reference)
Parameters
| Type | Name | Description |
|---|---|---|
| AssetReference | reference | The |
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<GameObject> | An |
InstantiateAsync(AssetReference, Transform)
Starts the asynchronous instantiation of the prefab identified by this AssetReference
as a child of the specified parent transform, returning a raw AsyncOperationHandle.
Declaration
public static AsyncOperationHandle<GameObject> InstantiateAsync(this AssetReference reference, Transform parent)
Parameters
| Type | Name | Description |
|---|---|---|
| AssetReference | reference | The |
| Transform | parent | The |
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<GameObject> | An |
InstantiateAsync(AssetReference, Vector3, Quaternion)
Starts the asynchronous instantiation of the prefab identified by this AssetReference
at a specific world position and rotation, returning a raw AsyncOperationHandle.
Declaration
public static AsyncOperationHandle<GameObject> InstantiateAsync(this AssetReference reference, Vector3 position, Quaternion rotation)
Parameters
| Type | Name | Description |
|---|---|---|
| AssetReference | reference | The |
| Vector3 | position | The world-space position at which to place the instantiated object. |
| Quaternion | rotation | The world-space rotation to apply to the instantiated object. |
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<GameObject> | An |
LoadAddressableAwaitable<T>(string, CancellationToken)
Loads the asset at this address string using Unity's Awaitable pattern, suspending
execution until the load completes. Delegates to
LoadAsyncAwaitable<T>(string, CancellationToken).
Declaration
public static Awaitable<T> LoadAddressableAwaitable<T>(this string address, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | address | The Addressables key string. Must not be |
| CancellationToken | cancellationToken | An optional token that can cancel the in-flight load. |
Returns
| Type | Description |
|---|---|
| Awaitable<T> | The loaded asset of type |
Type Parameters
| Name | Description |
|---|---|
| T | The type of asset to load. |
Exceptions
| Type | Condition |
|---|---|
| AddressableException | Thrown with NullAddress, LoadFailed, or OperationCancelled depending on the failure mode. |
LoadAddressable<T>(string)
Starts an asynchronous load of the asset at this address string and returns a raw
AsyncOperationHandle. Shorthand for LoadAsync<T>(string),
enabling concise call sites such as "ui/icon".LoadAddressable<Sprite>().
Declaration
public static AsyncOperationHandle<T> LoadAddressable<T>(this string address)
Parameters
| Type | Name | Description |
|---|---|---|
| string | address | The Addressables key string. Must not be |
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<T> | An |
Type Parameters
| Name | Description |
|---|---|
| T | The type of asset to load. |
Exceptions
| Type | Condition |
|---|---|
| AddressableException | Thrown synchronously with NullAddress when this address
is |
LoadAssetAsyncAwaitable<T>(AssetReference, CancellationToken)
Loads the asset referenced by this AssetReference and suspends execution using Unity's
Awaitable pattern, returning the asset directly upon completion. Delegates to
LoadAsyncAwaitable<T>(AssetReference, CancellationToken).
Declaration
public static Awaitable<T> LoadAssetAsyncAwaitable<T>(this AssetReference reference, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| AssetReference | reference | The |
| CancellationToken | cancellationToken | An optional token that can cancel the in-flight load. When cancelled, the handle is released before the exception propagates. |
Returns
| Type | Description |
|---|---|
| Awaitable<T> | The loaded asset of type |
Type Parameters
| Name | Description |
|---|---|
| T | The type of asset to load. |
Exceptions
| Type | Condition |
|---|---|
| AddressableException | Thrown with NullAddress when |
LoadAssetAsync<T>(AssetReference)
Starts an asynchronous load of the asset referenced by this AssetReference and returns
a raw AsyncOperationHandle that the caller manages. Delegates to
AssetReference.LoadAssetAsync<T>().
Declaration
public static AsyncOperationHandle<T> LoadAssetAsync<T>(this AssetReference reference)
Parameters
| Type | Name | Description |
|---|---|---|
| AssetReference | reference | The |
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<T> | An |
Type Parameters
| Name | Description |
|---|---|
| T | The type of asset to load. Must be compatible with the asset type assigned to the reference. |
Remarks
This extension provides a consistent call surface for AssetReference loads alongside the
other members of this class. For async/await usage, prefer
LoadAssetAsyncAwaitable<T>(AssetReference, CancellationToken).
OnComplete<T>(AsyncOperationHandle<T>, Action<T>, Action<AddressableException>)
Registers success and error completion callbacks on an AsyncOperationHandle, providing
a cleaner alternative to accessing the raw handle.Completed event directly.
Declaration
public static AsyncOperationHandle<T> OnComplete<T>(this AsyncOperationHandle<T> handle, Action<T> onSuccess, Action<AddressableException> onError = null)
Parameters
| Type | Name | Description |
|---|---|---|
| AsyncOperationHandle<T> | handle | The handle to attach callbacks to. |
| Action<T> | onSuccess | The callback invoked when |
| Action<AddressableException> | onError | An optional callback invoked when the operation status is anything other than
|
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<T> | The same |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the handle result. |
Remarks
The callback is registered on handle.Completed. Both callbacks are invoked on the
Unity main thread when the operation finishes.
ScyllaAddressables.LoadAsync<AudioClip>("sfx/jump")
.OnComplete(
clip => audioSource.PlayOneShot(clip),
ex => Debug.LogError($"Load failed: {ex.Message}")
)
.AutoRelease();
ReleaseAsset(AssetReference)
Releases the asset that was loaded from this AssetReference, decrementing its
Addressables reference count and allowing the underlying bundle to be unloaded when no
other references remain.
Declaration
public static void ReleaseAsset(this AssetReference reference)
Parameters
| Type | Name | Description |
|---|---|---|
| AssetReference | reference | The |
Remarks
Only call this once per load. Calling it when no asset is loaded via this reference is handled internally by the Addressables system (no-op or harmless).
ToAwaitable<T>(AsyncOperationHandle<T>, CancellationToken)
Suspends execution until the AsyncOperationHandle completes and returns the result,
bridging the Unity Addressables callback model to Unity's Awaitable async pattern.
Declaration
public static Awaitable<T> ToAwaitable<T>(this AsyncOperationHandle<T> handle, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| AsyncOperationHandle<T> | handle | The in-flight or already-completed handle to await. The caller retains ownership and must release this handle separately when done using the result. |
| CancellationToken | cancellationToken | An optional token that can cancel the wait. When cancelled, the suspension ends and an AddressableException with OperationCancelled is thrown. The handle itself is NOT released on cancellation - the caller must release it. |
Returns
| Type | Description |
|---|---|
| Awaitable<T> | The result of the completed operation of type |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the handle result. |
Remarks
This overload does NOT release the handle on cancellation, unlike the equivalent methods in ScyllaAddressables. Prefer ToResult<T>(AsyncOperationHandle<T>, CancellationToken) when exception-free error handling is preferred.
Exceptions
| Type | Condition |
|---|---|
| AddressableException | Thrown with LoadFailed when the handle completes with
a non-succeeded status; or with OperationCancelled when
the |
ToResult<T>(AsyncOperationHandle<T>, CancellationToken)
Suspends execution until the AsyncOperationHandle completes and wraps the outcome in an
AddressableResult<T>, providing exception-free error handling for awaitable workflows.
Declaration
public static Awaitable<AddressableResult<T>> ToResult<T>(this AsyncOperationHandle<T> handle, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| AsyncOperationHandle<T> | handle | The in-flight or already-completed handle to await. The caller retains ownership and must release this handle separately when done. |
| CancellationToken | cancellationToken | An optional token that can cancel the wait. When cancelled, the returned result contains OperationCancelled rather than throwing an exception. |
Returns
| Type | Description |
|---|---|
| Awaitable<AddressableResult<T>> | An |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the handle result. |
Remarks
Unlike ToAwaitable<T>(AsyncOperationHandle<T>, CancellationToken), this overload never throws. Use it when the caller prefers to inspect a result object rather than handle exceptions.