Search Unity

Getting IResourceLocations from an IResourceLocator?

Discussion in 'Addressables' started by ecurtz, Oct 12, 2019 at 11:04 PM.

  1. ecurtz

    ecurtz

    Joined:
    May 13, 2009
    Posts:
    612
    I'm trying to come up with some method of simply cataloging what addressables are available at runtime. One way to do this would be if there's a way to simply get the list of IResourceLocation objects that an IResourceLocator knows about, but that doesn't seem to be possible without doing some nasty gymnastics about looking through all the keys and building a list. Is there some way I'm missing to do this, or some reason it isn't a good idea?

    Another option that would work for my case but is less general would be if resourceLocator.Locate(null, Type, out locations)) would give all the locations for the specified Type, but that throws an exception.
     
  2. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    302
    (locator as ResourceLocationMap).Locations
    seems what you're asking for? The key is an address, and the value is
    IList<IResourceLocation>
    . You can just get all values and flatten into a list, or iterate using generator (yield return).
     
    ecurtz likes this.
  3. ecurtz

    ecurtz

    Joined:
    May 13, 2009
    Posts:
    612
    Thanks for the reply. That's a good idea, but ResourceLocationMap is just one implementation of IResourceLocator, so there's no guarantee that the Addressables.ResourceLocators list will actually allow that cast in the general case.

    It might work in practice to just test the class and put in a case for each implementation that actually exists in the package but that would always be kind of a hack (still better than nothing though...)
     
  4. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    302
    Currently only two classes implemented
    IResourceLocator
    ,
    ResourceLocationMap 
    for the addressable managed assets, and
    LegacyResourcesLocator
    for the assets loaded from resources directories. The later is half-baked without the ability to get all keys. So I guess the approach just works in practice. And of course you can raise exception for other cases.