Search Unity

Associating an AssetReference with its Asset Address

Discussion in 'Addressables' started by dgoyette, Nov 6, 2019.

  1. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    I just discovered the AssetReference class, which seemed like a good way to allow me to assign references to Addressables in the inspector, like so:

    upload_2019-11-6_14-15-26.png

    The issue I'm having, though, is that when I have a handle on that Asset Reference, I don't seem to correlate it to the "Asset Address" of the asset as set on the Addressables window:

    upload_2019-11-6_14-16-28.png

    Is there a simple thing I'm missing that allows me to extract the Asset Address from a AssetReference? I'm mainly using Asset Address in this one config, to make it easy to pick addressables, but in my code I'm almost always referring to addressables by their Address using constants.
     
  2. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    763
    Hey @dgoyette yeah there isn't a direct way to get that information from something like an asset reference. I think it mainly has to do with the fact that we could have multiple ResourceLocations associated with a particular Addressable Asset, one of which is the Addressable Address you see in the GUI. We could look into adding something onto AssetReference that returned all of these locations...

    I threw this together real quick as a potential for you to use modify it to make it work for your situation. Of course, if you end up finding multiple locations in the locator for your reference it might not be the first location like I have assumed in the quick example code below.

    Code (CSharp):
    1.  
    2. public AssetReference reference;
    3.  
    4.     IEnumerator Start()
    5.     {
    6.         yield return Addressables.InitializeAsync();
    7.  
    8.         foreach (var locator in Addressables.ResourceLocators)
    9.         {
    10.             IList<IResourceLocation> locations;
    11.             if (locator.Locate(reference.RuntimeKey, null, out locations))
    12.             {
    13.                 Debug.Log(locations[0].InternalId);
    14.             }
    15.         }
    16.     }
    Hope this helps
     
    Reijii and dgoyette like this.
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Thanks very much. I'll take a look.
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Thanks. It looks like this gives me what I want. The "InternalId" gives me the "Path" of the addressable, while "PrimaryKey" gives the short name or "Asset Address". Hopefully this all still works in a build. :)

    upload_2019-11-6_20-25-44.png
     
  5. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    Yes, It works at runtime.
     
  6. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    763
    Awesome, glad that helped