Method InitializeAsync
InitializeAsync()
Initialize the Addressables system, if needed.
Declaration
public static AsyncOperationHandle<IResourceLocator> InitializeAsync()
Returns
Type | Description |
---|---|
Async |
AsyncOperationHandle that is used to check when the operation has completed. The result of the operation is an IResource |
Remarks
The Addressables system initializes itself at runtime the first time you call an Addressables API function. You can call this function explicitly to initialize Addressables earlier. This function does nothing if initialization has already occurred.
Other Addressables API functions such as Load
The initialization process loads configuration data and the local content catalog. Custom initialization tasks can also be included in this process, for example loading additional remote catalogs. See Customizing Addressables Initialization for more information.
The initialization process:
- Sets up the Resource
Manager and ResourceLocators - Loads the Resource
Manager object, which is created by the Addressables buildRuntime Data - Executes IInitializable
Object operations - Optionally, checks for an updated content catalog (
true
by default) - Loads the content catalog
The Result
object contained in the AsyncResult
object in a Completed
event handler. To access the handle in a coroutine or Task-based function, pass false
to the
Initialize
Examples
The following script loads all Addressable assets referenced in the catalog.
void UsingInitializeAsyncSampleCallback()
{
Addressables.InitializeAsync().Completed += OnInitializeComplete;
}
void OnInitializeComplete(AsyncOperationHandle<IResourceLocator> handle)
{
if (handle.Status == AsyncOperationStatus.Succeeded)
{
Debug.Log("Addressables initialization succeeded");
IResourceLocator locator = handle.Result;
// locator.Keys includes bundle names and other identifiers from the local catalog.
foreach (object locatorKey in locator.Keys)
{
locator.Locate(locatorKey, typeof(UnityEngine.Object), out IList<IResourceLocation> locations);
if (locations == null)
{
continue;
}
foreach (IResourceLocation location in locations)
{
// The key representing the location of an Addressable asset.
string locationKey = location.InternalId;
Addressables.LoadAssetAsync<UnityEngine.Object>(locationKey).Completed += OnLoadAssetComplete;
}
}
}
}
void OnLoadAssetComplete(AsyncOperationHandle<UnityEngine.Object> handle)
{
if (handle.Status == AsyncOperationStatus.Succeeded)
{
Debug.Log($"Successfully loaded ({handle.Result.GetType()})");
}
}
See Also
InitializeAsync(bool)
Initialize the Addressables system, if needed.
Declaration
public static AsyncOperationHandle<IResourceLocator> InitializeAsync(bool autoReleaseHandle)
Parameters
Type | Name | Description |
---|---|---|
bool | autoReleaseHandle | Determines if the handle should be released on completion, or manually released. |
Returns
Type | Description |
---|---|
Async |
AsyncOperationHandle that is used to check when the operation has completed. The result of the operation is an IResource |