docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Wait for asynchronous loads with coroutines

    AsyncOperationHandle implements the IEnumerator interface and continues iteration until the operation is complete.

    In a coroutine, you can yield the operation handle to wait for the next iteration. When complete, the execution flow continues to the following statements. You can implement the MonoBehaviour.Start method as a coroutine, which is a good way to have a GameObject load and instantiate the assets it needs.

    The following script loads a prefab as a child of its GameObject using a Start method in a coroutine. It yields the AsyncOperationHandle until the operation finishes and then uses the same handle to instantiate the prefab.

    
    using System.Collections;
    using UnityEngine;
    using UnityEngine.AddressableAssets;
    using UnityEngine.ResourceManagement.AsyncOperations;
    
    internal class LoadWithIEnumerator : MonoBehaviour
    {
        public string address;
        AsyncOperationHandle<GameObject> opHandle;
    
        public IEnumerator Start()
        {
            opHandle = Addressables.LoadAssetAsync<GameObject>(address);
    
            // yielding when already done still waits until the next frame
            // so don't yield if done.
            if (!opHandle.IsDone)
                yield return opHandle;
    
            if (opHandle.Status == AsyncOperationStatus.Succeeded)
            {
                Instantiate(opHandle.Result, transform);
            }
            else
            {
                opHandle.Release();
            }
        }
    
        void OnDestroy()
        {
            opHandle.Release();
        }
    }
    
    

    You can't cancel Addressables.LoadAssetsAsync once started. However, releasing the handle before it has finished decrements the handle reference count and automatically releases it when the load is complete.

    For more information, refer to Write and run coroutines.

    Group operations in a coroutine

    To perform several operations before moving on to the next step in your game logic, such as loading prefabs and other assets before you start a level, you can combine them with a single call to the Addressables.LoadAssetsAsync method, if all the operations load assets.

    The AsyncOperationHandle for this method works the same as LoadAssetAsync. You can yield the handle in a coroutine to wait until all the assets in the operation load. You can also pass a callback method to LoadAssetsAsync and the operation calls that method when it finishes loading a specific asset. Refer to Loading multiple assets for an example.

    You can also use the ResourceManager.CreateGenericGroupOperation to create a group operation that completes when all its members finish.

    Additional resources

    • Wait for asynchronous loads with events
    • Wait for asynchronous loads with async and await
    • Write and run coroutines
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)