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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Create AssetReference at runtime from key?

Discussion in 'Addressables' started by cg-adam, Apr 24, 2020.

  1. cg-adam

    cg-adam

    Joined:
    Jul 30, 2019
    Posts:
    44
    Is there any way we can make an AssetReference at runtime with just a key? I have a third party solution which uses AssetReferences for pooling, but I need to inject the pooled items at runtime based on some data that comes back from my server.

    EDIT:
    I am aware there is a constructor but it requires an asset GUID to be passed through, I need to create one using an "address/key".
     
    Last edited: Apr 27, 2020
  2. phobos2077

    phobos2077

    Joined:
    Feb 10, 2018
    Posts:
    350
    Code (CSharp):
    1. /// <summary>
    2.         /// Construct a new AssetReference object.
    3.         /// </summary>
    4.         /// <param name="guid">The guid of the asset.</param>
    5.         public AssetReferenceT(string guid) : base(guid)
    6.         {
    7.         }
     
  3. cg-adam

    cg-adam

    Joined:
    Jul 30, 2019
    Posts:
    44
    Sorry, yes I know there is a constructor but passing through a "key" into an AssetReference does not seem possible, so I am asking if there is a solution for that right now. The current constructor requires an asset GUID to passed through, which is not what I need.
     
  4. phobos2077

    phobos2077

    Joined:
    Feb 10, 2018
    Posts:
    350
    Have you actually tried that? Don't mind the "guid" name, in Addressables all assets can be addressed by either Guid or address interchangeably, by default. So just pass in the key, it should work.
     
  5. cg-adam

    cg-adam

    Joined:
    Jul 30, 2019
    Posts:
    44
    To be honest I have not tried it recently. I think I did a while ago, but will give it another go. Would be good to have an official response around this, but I will try it tomorrow. Thanks.
     
  6. davidrochin

    davidrochin

    Joined:
    Dec 17, 2015
    Posts:
    72
    Not working for me. I have to pass the GUID or else the AssetReference won't work.
     
  7. davidrochin

    davidrochin

    Joined:
    Dec 17, 2015
    Posts:
    72
    Actually It seems I can initialize and load an
    AssetReference
    with an asset's address instead of GUID, but
    RuntimeKeyIsValid()
    returns false, even though I can actually load the asset if I call
    LoadAssetAsync<>()
     
  8. phobos2077

    phobos2077

    Joined:
    Feb 10, 2018
    Posts:
    350
    Yeah, I've been digging through the source code a lot bug haven't tried this with
    RuntimeKeyIsValid()
    . Seems like this class in unfinished. Anyway, why do you really need AssetReference for if you initialize it at runtime? Why not just call Addressables.LoadAssetAsync directly?

    The only reason for AssetReference to exist is the custom drawer.
     
  9. davidrochin

    davidrochin

    Joined:
    Dec 17, 2015
    Posts:
    72
    Because a lot of my methods take
    AssetReference
    's as parameters. Too bad I'll have to make versions that take strings
     
  10. cg-adam

    cg-adam

    Joined:
    Jul 30, 2019
    Posts:
    44
    I mainly want to use this because a third party tool we are using (PoolBoss) now supports addressables but only for AssetReferences.