Search Unity

Feature Request: GetAddresses(string label) and/or GetAddress(UnityEngine.Object o)

Discussion in 'Addressables' started by AlkisFortuneFish, Sep 27, 2018.

  1. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    588
    I cannot currently find any way to request all addresses for a label or find a way to find the address of something loaded via the addressables system.

    The use-case is the following:

    Want to present the user with a list of assets to pick from, profile icons for example.
    I should be able to mark them all with a label.
    I should be able to retrieve their addresses using that label to be able to load them, present them, store the selected address and unload them.
    Failing that, I should be able to load all the assets through the existing LoadAssets(string label) API, have the user select one, get and store its address and unload them.

    The former is more flexible, as the developer gets to pick whether to actually load all the assets at the same time or load them and unload them on demand.
     
    taptpci likes this.
  2. Elhimp

    Elhimp

    Joined:
    Jan 6, 2013
    Posts:
    20
    Well, right now you could work around by grouping your assets with kinda same address. Yet (maybe I'm just not looking good enough) there is no such thing as Addressables.Keys collection, which would be also nice to have, 'cause Addressables basically key-value storage thing.

    So my way to dealing with almost same task is
    Code (CSharp):
    1. IEnumerable<string> profileIconKeys = Addressables.ResourceLocators
    2.     .OfType<ResourceLocationMap>()
    3.     .SelectMany(locationMap => locationMap.Locations.Keys.Select(key => key.ToString()))
    4.     .Where(keyString => keyString.StartsWith("profileIcons/"));
     
    Last edited: Sep 28, 2018
    Paul_H23 likes this.
  3. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    588
    Thanks for the snippet, I'll see if it fits the particular use case we have! :)
    I will look into implementing the actual API myself when I have the time, it's just ideally this is something the system should have from the get go, rather than something to manually implement.

    PS: Use OfType<ResourceLocationMap>(), saves you having to .Where().Cast(). :)
     
  4. Elhimp

    Elhimp

    Joined:
    Jan 6, 2013
    Posts:
    20
    Edited for future generations...
    And ofcourse idea is keyString should StartsWith something, not fully equals it.
     
  5. dri_richard

    dri_richard

    Joined:
    Mar 10, 2017
    Posts:
    56
    This is a surprising omission, given that addressables is all about orthogonality & separation of concerns.
    In our case, some of our assets will be local and others will be remote. I really don't want to load them all!
     
  6. Dunk86

    Dunk86

    Joined:
    May 10, 2015
    Posts:
    50
    Oh no...
    I just assumed that you could get a list of AssetReferences by label - what's the point if you need to completely load them all in one go?
    I thought my use case was the most obvious one:
    1) I have a list of hats, all addressables and labelled as "Hat".
    2) I query to know which hats I have, I present a menu to scroll through the hats one at a time.
    3) I load each hat prefab when I switch to it, caching it for later use.

    For a system like this to be flexible, you need to be able to query the list of addresses without loading the whole set...
    I've only just started with addressables, so maybe I'm just missing something. :/

    EDIT: I managed to workaround the issue by having a ScriptableObject that holds the list of AssetReferences that I'd want to fetch/load at runtime. Not much fun to have to do this manually, but at least I can attach useful meta-data to each element that I can use before the full load.
     
    Last edited: Feb 19, 2019
  7. sschoener

    sschoener

    Joined:
    Aug 18, 2014
    Posts:
    20
    I'd like to second this request. This is exactly what I had hoped to use addressables for. My initial expectation was that I could somehow iterate over all addressables and look at their metadata, but that does not seem to be the case (this would help here as well since I could simply check for the presence of a specific label).
     
  8. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    849
    under the hood, an address and a label are actually the same thing. They are buckets for locations. The motivation for this was twofold. For one, a given address can actually point to more than one asset (assets can share addresses). Second, is that maintaining this backwards lookup from location to address significantly bloats the data for large projects. Ways to solve the problem

    Short term (0.6.7) - you can call LoadAssets<IResourceLocation>() to get the locations associated with a label. Populate your menu with items loaded via these returned locations. I think that will accomplish the initial ask of this thread.
    Short term (0.7.x) - we plan to add a dedicated API to loading locatinos, as LoadAssets<IResourceLocation> is fairly non-intuitive.

    Long term: there's a good chance we'll add this backwards lookup as an option. Due to bloat, it would not be on by default, but perhaps the use case is common enough to add as an option.

    -Bill
     
    MNNoxMortem likes this.
  9. DRI-Ross

    DRI-Ross

    Joined:
    Sep 24, 2018
    Posts:
    5
    This sounds very useful.
    I have a related but slightly different use case: if I wanted to download some assets from the address (say a sprite to be populated into the UI) but only download the 'meat' of the asset (textures, mesh, animations etc.) at a later point (when the user has actually pressed the button), is this doable in the planned API?
     
  10. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    588
    We do this by downloading a ScriptableObject that contains the address of the "meat" of the asset rather than a direct reference.
     
  11. DRI-Ross

    DRI-Ross

    Joined:
    Sep 24, 2018
    Posts:
    5
    Ah, that's what I'm doing at the moment - I was hoping there was a cleaner way. Thanks, though.