{!See https://docs.google.com/document/d/1takg_GmIBBKKTj-GHZCwzxohpQz7Bhekivkk72kYMtE/edit for reference implementation of OneTrust, dataLayer and GTM} {!OneTrust Cookies Consent} {!OneTrust Cookies Consent end} {!dataLayer initialization push} {!dataLayer initialization push end} {!Google Tag Manager} {!Google Tag Manager end} Load assets | Addressables | 1.21.19
docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Load assets

    You can use LoadAssetAsync or LoadAssetsAsync to load one, or multiple assets at runtime.

    Load a single asset

    Use the LoadAssetAsync method to load a single Addressable asset, typically with an address as the key:

    
    using System.Collections;
    using UnityEngine;
    using UnityEngine.AddressableAssets;
    using UnityEngine.ResourceManagement.AsyncOperations;
    
    internal class LoadAddress : MonoBehaviour
    {
        public string key;
        AsyncOperationHandle<GameObject> opHandle;
    
        public IEnumerator Start()
        {
            opHandle = Addressables.LoadAssetAsync<GameObject>(key);
            yield return opHandle;
    
            if (opHandle.Status == AsyncOperationStatus.Succeeded)
            {
                GameObject obj = opHandle.Result;
                Instantiate(obj, transform);
            }
        }
    
        void OnDestroy()
        {
            Addressables.Release(opHandle);
        }
    }
    
    

    You can use a label or other key type when you call LoadAssetAsync, not just an address. However, if the key resolves to more than one asset, only the first asset found is loaded. For example, if you call this method with a label applied to several assets, Addressables returns whichever one of those assets that happens to be located first.

    Load multiple assets

    Use the LoadAssetsAsync method to load more than one Addressable asset in a single operation. When using this method, you can specify a single key, such as a label, or a list of keys.

    When you specify multiple keys, you can specify a merge mode to set how the assets that match each key are combined:

    • Union: Include assets that match any key
    • Intersection: Include assets that match every key
    • UseFirst: Include assets only from the first key that resolves to a valid location
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AddressableAssets;
    using UnityEngine.ResourceManagement.AsyncOperations;
    
    internal class LoadMultiple : MonoBehaviour
    {
        // Label strings to load
        public List<string> keys = new List<string>() {"characters", "animals"};
    
        // Operation handle used to load and release assets
        AsyncOperationHandle<IList<GameObject>> loadHandle;
    
        // Load Addressables by Label
        public IEnumerator Start()
        {
            float x = 0, z = 0;
            loadHandle = Addressables.LoadAssetsAsync<GameObject>(
                keys,
                addressable =>
                {
                    //Gets called for every loaded asset
                    Instantiate<GameObject>(addressable,
                        new Vector3(x++ * 2.0f, 0, z * 2.0f),
                        Quaternion.identity,
                        transform);
    
                    if (x > 9)
                    {
                        x = 0;
                        z++;
                    }
                }, Addressables.MergeMode.Union, // How to combine multiple labels 
                false); // Whether to fail and release if any asset fails to load
    
            yield return loadHandle;
        }
    
        private void OnDestroy()
        {
            Addressables.Release(loadHandle);
            // Release all the loaded assets associated with loadHandle
            // Note that if you do not make loaded addressables a child of this object,
            // then you will need to devise another way of releasing the handle when
            // all the individual addressables are destroyed.
        }
    }
    
    

    To specify how to handle loading errors, use the releaseDependenciesOnFailure parameter. If true, then the operation fails if it encounters an error loading any single asset. The operation and any assets that loaded are released.

    If false, then the operation loads any objects that it can and doesn't release the operation. If it fails, the operation still completes with a status of Failed. Also, the list of assets returned has null values where the failed assets would otherwise appear.

    Set releaseDependenciesOnFailure to true when loading a group of assets that must be loaded as a set to be used. For example, if you load the assets for a game level, you might fail the operation as a whole rather than load only some of the required assets.

    In This Article
    Back to top
    Copyright © 2023 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)