Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Addressables don't load remote catalog twice

Discussion in 'Addressables' started by lbaptista95, Feb 20, 2020.

  1. lbaptista95

    lbaptista95

    Joined:
    Jan 4, 2019
    Posts:
    4
    I'm trying to download assets from my AWS S3 bucket. So I'm using this script:
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.IO;
    3. using UnityEngine;
    4. using UnityEngine.AddressableAssets;
    5. using UnityEngine.AddressableAssets.ResourceLocators;
    6. using UnityEngine.ResourceManagement.AsyncOperations;
    7. using UnityEngine.ResourceManagement.ResourceLocations;
    8.  
    9. public class AssetLoader : MonoBehaviour
    10. {
    11.     public string catalogPath;
    12.     public string myLabel;
    13.     public List<IResourceLocation> locations;      
    14.  
    15.     public void initAddressables()
    16.     {
    17.         uiManager.loading.SetActive(true);
    18.         AsyncOperationHandle<IResourceLocator> handle = Addressables.InitializeAsync();
    19.         handle.Completed += initDone;
    20.     }
    21.  
    22.  
    23.     private void initDone(AsyncOperationHandle<IResourceLocator> obj)
    24.     {
    25.         if (obj.Status == AsyncOperationStatus.Succeeded)
    26.         {
    27.             LoadCatalog();
    28.         }
    29.     }
    30.  
    31.  
    32.     void LoadCatalog()
    33.     {
    34.         AsyncOperationHandle<IResourceLocator> handle;
    35.      
    36.         handle = new AsyncOperationHandle<IResourceLocator>();      
    37.         handle = Addressables.LoadContentCatalogAsync(catalogPath);      
    38.         handle.Completed += LoadCatalogsCompleted;    
    39.      
    40.     }
    41.     void LoadCatalogsCompleted(AsyncOperationHandle<IResourceLocator> obj)
    42.     {
    43.         if (obj.Status == AsyncOperationStatus.Succeeded)
    44.         {
    45.             LoadResourceLocation();
    46.         }
    47.         else
    48.         {
    49.             Debug.LogError("LoadCatalogsCompleted is failed");
    50.         }
    51.     }
    52.  
    53.     void LoadResourceLocation()
    54.     {
    55.         AsyncOperationHandle<IList<IResourceLocation>> handle = Addressables.LoadResourceLocationsAsync(myLabel);
    56.         handle.Completed += LocationsLoaded;
    57.     }
    58.     void LocationsLoaded(AsyncOperationHandle<IList<IResourceLocation>> obj)
    59.     {
    60.         if (obj.Status == AsyncOperationStatus.Succeeded)
    61.         {
    62.             locations = new List<IResourceLocation>(obj.Result);
    63.             LoadDependency();
    64.         }
    65.         else
    66.         {
    67.             Debug.LogError("locationsLoaded is failed");
    68.         }
    69.     }
    70.  
    71.     void LoadDependency()
    72.     {
    73.         AsyncOperationHandle handle = Addressables.DownloadDependenciesAsync(myLabel);
    74.         handle.Completed += DependencyLoaded;
    75.     }
    76.     void DependencyLoaded(AsyncOperationHandle obj)
    77.     {
    78.         if (obj.Status == AsyncOperationStatus.Succeeded)
    79.         {
    80.             LoadAssets();
    81.         }
    82.         else
    83.         {
    84.             Debug.LogError("dependencyLoaded is Failed");
    85.         }
    86.     }
    87.  
    88.     private void LoadAssets()
    89.     {
    90.         AsyncOperationHandle<IList<GameObject>> handle = Addressables.LoadAssetsAsync<GameObject>(locations, OnAssetsCategoryLoaded);
    91.         handle.Completed += OnAssetsLoaded;
    92.     }
    93.     private void OnAssetsCategoryLoaded(GameObject obj)
    94.     {
    95.         SpawnItem(obj.name);
    96.     }
    97.     private void OnAssetsLoaded(AsyncOperationHandle<IList<GameObject>> obj)
    98.     {
    99.     }
    100.  
    101.     void SpawnItem(string addressableKey)
    102.     {
    103.         if (!instantiated)
    104.         {
    105.             AsyncOperationHandle<GameObject> asyncLoad = Addressables.InstantiateAsync(myLabel, Vector3.zero, Quaternion.identity);
    106.             StartCoroutine(progressAsync(asyncLoad));
    107.             asyncLoad.Completed += AssetSpawned;
    108.             instantiated = true;
    109.         }
    110.      
    111.     }
    112.  
    113.     private System.Collections.IEnumerator progressAsync(AsyncOperationHandle<GameObject> asyncOperation)
    114.     {
    115.         float percentLoaded = asyncOperation.PercentComplete;
    116.         while (!asyncOperation.IsDone)
    117.         {
    118.             Debug.Log("Progress = " + percentLoaded + "%");
    119.             yield return 0;
    120.         }
    121.     }
    122.     void AssetSpawned(AsyncOperationHandle<GameObject> obj)
    123.     {
    124.          Debug.Log("Asset Spawned");
    125. }
    The public method "initAddressables()" is called when I press a button.

    Everything works fine at the first time. But when I try to load the same asset another time (without closing the application) it returns me this error:

    ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: startIndex

    And it happens when the application runs this line:

    handle = Addressables.LoadContentCatalogAsync(catalogPath);

    Is there a way to "reset" the addressables? Or something like that?
     
  2. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,816
    We'll pass this along to the team for you. Could you tell us which version of the Editor you're using, and which Package Manager version?
     
  3. lbaptista95

    lbaptista95

    Joined:
    Jan 4, 2019
    Posts:
    4
    Unity 2019.2.6f1
    Package Manager 2.2.0
    Addressables 1.5.1
     
  4. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    762
    @lbaptista95 currently there's not any supported way to do what you're trying to do I don't think. There is some discussion around changing how the catalogs are loaded and starting to track them so they can be unloaded. I can't give any timeline, even a rough one, for something like that as I'm not sure when/if it'll get picked up.

    Go ahead and file a bug with Unity. This will make sure that we take things like this into account when sorting through our priorities.