docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Asynchronous loading

    The Addressables system API is asynchronous and returns an AsyncOperationHandle to use to manage operation progress and completion.

    Addressables is designed to be content location agnostic. The content might need to be downloaded first or use other methods that can take a long time. To force synchronous execution, refer to Synchronous Addressables for more information.

    When loading an asset for the first time, the handle is complete after a minimum of one frame. If the content has already loaded, execution times might differ between the various asynchronous loading options shown below. You can wait until the load has completed as follows:

    • Coroutine: Always delayed at a minimum of one frame before execution continues.
    • Completed callback: A minimum of one frame if the content hasn't already loaded, otherwise the callback is invoked in the same frame.
    • Awaiting AsyncOperationHandle.Task: A minimum of one frame if the content hasn't already loaded, otherwise the execution continues in the same frame.
    
    using System.Collections;
    using UnityEngine;
    using UnityEngine.AddressableAssets;
    using UnityEngine.ResourceManagement.AsyncOperations;
    
    internal class AsynchronousLoading : MonoBehaviour
    {
        private string address = "tree";
        private AsyncOperationHandle loadHandle;
    
        // always minimum of 1 frame
        IEnumerator LoadAssetCoroutine()
        {
            loadHandle = Addressables.LoadAssetAsync<GameObject>(address);
            yield return loadHandle;
        }
    
        // minimum of 1 frame for new asset loads
        // callback called in current frame for already loaded assets
        void LoadAssetCallback()
        {
            loadHandle = Addressables.LoadAssetAsync<GameObject>(address);
            loadHandle.Completed += h =>
            {
                // Loaded here
            };
        }
    
        // minimum of 1 frame for new asset loads
        // await completes in current frame for already loaded assets
        async void LoadAssetWait()
        {
            loadHandle = Addressables.LoadAssetAsync<GameObject>(address);
            await loadHandle.Task;
        }
    
        private void OnDestroy()
        {
            Addressables.Release(loadHandle);
        }
    }
    
    
    In This Article
    Back to top
    Copyright © 2024 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)