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

Get a list of all catalogs and/or addressables runtime?

Discussion in 'Addressables' started by Digika, Sep 5, 2019.

  1. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    225
    I need to fetch all the catalogs and addressables in it (or just addressables flat) that are shipped/built in with player. LIke, with Asset Bundles I can load bundle and the get all assets names so I basically want to get similar data runtime (except it will be format specific to addressable system ofc) and iterate.
     
  2. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    wlwl2 likes this.
  3. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    225
    My scenario does not matter but let me make a example one. Let's say I have an Enemy Manager that instantiates all enemies labeled as "commonEnemy". So its all cool, it asks for all assets/objects under that label and loads them. That means EnemyManager at least has a reference to the label name stored. Now, lets say we have a special extra enemy, unique one, it has no label so it has to be referenced via AddressableReference and so EnemyManager now also has or know about that one specific reference to an object in some catalogue data.

    In summary, we now know that the player (as in unity player) is aware about that label and that reference. This applies to the rest of the assets in projects. So there has to be some way to get that info runtime, some info on the structure of the addressable assets - catalogues, labels, specific references. How do I fetch it and examine it runtime, in a final build?
     
  4. HugoClip

    HugoClip

    Joined:
    Feb 28, 2018
    Posts:
    52
    By default there is only one catalog shipped with the player, basically each catalog is transformed into ResourceMapLocator. You can grab it with the following code:

    Code (CSharp):
    1. ResourceLocationMap map = Addressables.ResourceLocators[0] as ResourceLocationMap;
    2. map.Locate("special", typeof(GameObject), out IList<IResourceLocation> locs);
    I'm still confused on why you can't just grab the key and just load it with the normal addressables flow.
     
  5. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    Well, the label is currently the prefered way to organize a group of assets, which defined at the build time. You can use label for specific game logic or with the help of the importer to a more generic and automated usage, like applying label to folder or group.

    If this does not satisfy your requirement, you're looking for a generic way to find assets by some dimensions, mostly the address parsing. If you follow the second link in #2, which direct you to this thread, shows how to iterate through the loaded catalog at the runtime. However since the system flatten all types of references when building a catalog, you lose informations like group or label for an address. The catalog seems treating no difference of address, label, guid, and turns them into a key-value kind mapping.
     
    unity_bill likes this.
  6. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    225
    As I said before multiple times I just need to get a list of all addressables player knows about in build.
     
    rusteze and ina like this.
  7. alchemyincarnate

    alchemyincarnate

    Joined:
    Feb 25, 2014
    Posts:
    10
    This gives me an empty list.
    Can anyone help me get all labels that the internal build (AddressablesMainContentCatalog) is aware of?
     
  8. rosen_rusinov

    rosen_rusinov

    Joined:
    Apr 6, 2018
    Posts:
    10
    Code (CSharp):
    1.            
    2.             var allLocations = new List<IResourceLocation>();
    3.             foreach (var resourceLocator in Addressables.ResourceLocators)
    4.             {
    5.                 if (resourceLocator is ResourceLocationMap map)
    6.                 {
    7.                     foreach (var locations in map.Locations.Values)
    8.                     {
    9.                         allLocations.AddRange(locations);
    10.                     }
    11.                 }
    12.             }
    13.  
    Maybe this is what you need?
     
  9. alchemyincarnate

    alchemyincarnate

    Joined:
    Feb 25, 2014
    Posts:
    10
    Hi @rosen_rusinov
    thanks for the reply. this seems to add everything and not just the valid labels i.e. if I call Addressables.GetDownloadSizeAsync(allLocations), I get a KeyNotFoundException on an address string
     
  10. rosen_rusinov

    rosen_rusinov

    Joined:
    Apr 6, 2018
    Posts:
    10
    Interesting thats exactly what I'm using it for I have this call right after the snippet and it works for me (I can download all the games addressable content at once with this)

    Code (CSharp):
    1.  
    2.             var preloadSizeOp = Addressables.GetDownloadSizeAsync(allLocations);
    3.             var preloadSize = await preloadSizeOp.Task;
    4.             Addressables.Release(preloadSizeOp);
    5.  
    6.             if (preloadSize > 0f)
    7.             {
    8.                 Log($"Starting download of {preloadSize / (1024f * 1024f):n2}mbs");
    9.  
    10.                 var oneSecond = System.TimeSpan.FromSeconds(1f);
    11.                 var preloadOp = Addressables.DownloadDependenciesAsync(allLocations);
    12.                 while (!preloadOp.IsDone)
    13.                 {
    14.                     var status = preloadOp.GetDownloadStatus();
    15.                     Log($"Downloading in progress: {status.DownloadedBytes / (1024f * 1024f):n2}mbs/{status.TotalBytes / (1024f * 1024f):n2}mbs ({status.Percent * 100f:n0}%)");
    16.                     await UnityAwaiters.WaitForTime(oneSecond);
    17.                 }
    18.  
    19.                 Addressables.Release(preloadOp);
    20.                 Log($"Download of all assets complete!");
    21.             }
    22.             else
    23.             {
    24.                 Log($"All assets up to date!");
    25.             }
    26.  
    Using Unity 2020.3.20f1, Addressables 1.18.19 & 1.19.11 both versions this has worked for me
     
  11. alchemyincarnate

    alchemyincarnate

    Joined:
    Feb 25, 2014
    Posts:
    10
    hi again @rosen_rusinov
    thank you for the 2nd snippet, finally solved my problem after cross checking with your code.
    for anyone finding this thread via google, the above method worked for me on Addressables@1.19.11
     
    rosen_rusinov likes this.