Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Is it possible to retrieve a guid from the Catalog at runtime?

Discussion in 'Addressables' started by Laiken, May 28, 2022.

  1. Laiken

    Laiken

    Joined:
    Nov 18, 2015
    Posts:
    50
    By default the guids of the addressable files are included in the catalog. But is there a way to retrieve it?
    For example, if I have a texture on the address "myTexture", is there a way to get the guid of that texture?
     
    Last edited: May 28, 2022
  2. Laiken

    Laiken

    Joined:
    Nov 18, 2015
    Posts:
    50
    Code (CSharp):
    1. AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;
    2. List<AddressableAssetEntry> allEntries = new List<AddressableAssetEntry>(settings.groups.SelectMany(g => g.entries));
    That allEntries has the address and guid but can only be accessed in editor. Any way to access the guids in the catalog during runtime?
     
  3. pillakirsten

    pillakirsten

    Unity Technologies

    Joined:
    May 22, 2019
    Posts:
    346
    Hi @Laiken it's a little tricky to do at runtime. You can use Addressables.ResourceLocators to retrieve a mapping of all keys to their resource locations, but keys could refer to a guid, address, or label. So what you can do is reverse the elements of the dictionary. The guid associated with a particular address will reference the same location.

    Code (CSharp):
    1.  
    2. Addressables.InitializeAsync().WaitForCompletion(); // populate ResourceLocators
    3. var locToKeys = new Dictionary<string, List<object>>();
    4. foreach (IResourceLocator locator in Addressables.ResourceLocators)
    5. {
    6.     ResourceLocationMap map = locator as ResourceLocationMap;
    7.     if (map == null)
    8.         continue;
    9.     foreach (KeyValuePair<object, IList<IResourceLocation>> keyToLocs in map.Locations)
    10.     {
    11.         foreach (IResourceLocation loc in keyToLocs.Value)
    12.         {
    13.             if (!locToKeys.ContainsKey(loc.InternalId))
    14.                 locToKeys.Add(loc.InternalId, new List<object>(){ keyToLocs.Key });
    15.             else
    16.                 locToKeys[loc.InternalId].Add(keyToLocs.Key);
    17.         }
    18.     }
    19. }
    20.  
     
    Laiken likes this.
  4. Laiken

    Laiken

    Joined:
    Nov 18, 2015
    Posts:
    50
    Thank you pillakirsten!
    That worked perfectly.