Method InitializeAsync
InitializeAsync()
Initialize the Addressables system, if needed.
Declaration
public static AsyncOperationHandle<IResourceLocator> InitializeAsync()
Returns
Type | Description |
---|---|
AsyncOperationHandle<IResourceLocator> | AsyncOperationHandle that is used to check when the operation has completed. The result of the operation is an IResourceLocator object. |
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 LoadAssetAsync also automatically initializes the system if not already initialized. However in some cases you may wish to explicitly initalize Addressables, for example to check if the initialization process completed successfully before proceeding to load Addressables assets. Initializing explicitly shortens the execution time of the subsequent Addressables API function call because the initialization process is already completed.
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 ResourceManager and ResourceLocators
- Loads the ResourceManagerRuntimeData object, which is created by the Addressables build
- Executes IInitializableObject operations
- Optionally, checks for an updated content catalog (
true
by default) - Loads the content catalog
The Result
object contained in the AsyncOperationHandle<TObject> returned by this function
contains a list of Addressable keys and a method that can be used to gather the IResourceLocation
instances for a given key and asset type. You must access the Result
object in a Completed
event handler. To access the handle in a coroutine or Task-based function, pass false
to the
InitializeAsync(bool) overload of this function. Otherwise, the Addressables system
releases the AsyncOperationHandle<TObject> object before control returns to your code.
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.name} ({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 |
---|---|
AsyncOperationHandle<IResourceLocator> | AsyncOperationHandle that is used to check when the operation has completed. The result of the operation is an IResourceLocator object. |