Search Unity

Mapping addresses to assets

Discussion in 'Addressables' started by pdinklag, Apr 17, 2020.

  1. pdinklag

    pdinklag

    Joined:
    Jan 24, 2017
    Posts:
    154
    It's nice to see the Addressables system is coming along. :) I am porting my old system now, and I have one question related to Addressables.LoadAssetsAsync given a list of resource locations.

    Is the list of loaded assets guaranteed to be in the same order as the passed list of locations? Specifically, will the following code always produce a correct mapping, assuming everything loads successfully?

    Code (CSharp):
    1. var mapping = new Dictionary<string, T>();
    2. Addressables.LoadResourceLocationsAsync(assetLabel).Completed += locationsLoader =>
    3. {
    4.     Addressables.LoadAssetsAsync<T>(locationsLoader.Result, null /* no callback */).Completed += assetsLoader =>
    5.     {
    6.         for (var i = 0; i < assetsLoader.Result.Count; i++)
    7.         {
    8.             mapping.Add(locationsLoader.Result[i].PrimaryKey, assetsLoader.Result[i]);
    9.         }
    10.     };
    11. };
    12.  
    I do this to cache assets with a certain label and store them in a map with their addresses being the keys, because I need synchronous access to them throughout runtime with absolutely no frame delays. Of course I tried this, and it does indeed appear to work. However, the documentation doesn't say much and I'd like to be certain about it.

    Is this the way to go, or is there any other way to enumerate assets along with their addresses, or even a preferred technique to pre-load assets and grant synchronous access?
    Thanks!
     
  2. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    What you have done is the way to go.