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

How to test for empty AssetReference

Discussion in 'Addressables' started by yonson_chappers, Mar 25, 2019.

Thread Status:
Not open for further replies.
  1. yonson_chappers

    yonson_chappers

    Joined:
    Feb 6, 2017
    Posts:
    31
    I have a simple class that I've replaced prefabs with Asset References. Everything is fine except testing for an empty AssetReference (i.e. set to 'none').

    Before, I just test if the prefab == null, but with AssetReferences even when the toString() is []null, actually running the test

    if assetReference=null

    never works.

    So, how do we test for an empty Asset Reference?
    Many thanks
    John
     
    aka3eka likes this.
  2. yonson_chappers

    yonson_chappers

    Joined:
    Feb 6, 2017
    Posts:
    31
    Worked out a solution at the moment just for anyone who is having this problem. A null asset reference returns a Hash128 key of 0000000000000 so I'm just parsing any key and seeing if its zero. I'm sure there must be a better way but here is the code and this is at least working

    Hash128 key = thisAssetReference.RuntimeKey;
    int interiorKeyAsNumber = Utils.IntParseFast(key.ToString());

    if (interiorKeyAsNumber == 0) {
    // this is a crazy way of finding out that the aset reference is 'null'

    }
     
  3. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    958
    Bumping this thread: is this true? is this the only way to check AssetReference = null?

    I understand the lack of doc, I understand things will change with preview package.

    What I don't understand: we are hunting for these little pieces of infos, and it's not like I am writing a custom provider or anything grand, it's just checking for "null", which could happen by mistake or by design.

    I would love to be proven wrong! With these many people using Addressable I hope at least one person will know and be kind enough to enlightenment us...
     
    phobos2077 likes this.
  4. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    andreiagmu, aka3eka and bitinn like this.
  5. aka3eka

    aka3eka

    Joined:
    May 10, 2019
    Posts:
    32
    diesoftgames and andreiagmu like this.
  6. davidrochin

    davidrochin

    Joined:
    Dec 17, 2015
    Posts:
    72
    It seems that currently,
    RuntimeKeyIsValid()
    is returning false if the
    AssetReference
    is initialized with an address instead of a GUID. It can actually load the asset if I call
    LoadAssetAsync<>()
    but this doesn't allow me to do validation :/.
     
    FlightOfOne and RZGames_Jethro like this.
  7. aka3eka

    aka3eka

    Joined:
    May 10, 2019
    Posts:
    32
    What do you mean by "initialized with an address instead of a GUID"? I didn't find any mentions o address in AssetReference class in Unity docs (https://docs.unity3d.com/Packages/c...yEngine.AddressableAssets.AssetReference.html).
     
  8. phobos2077

    phobos2077

    Joined:
    Feb 10, 2018
    Posts:
    350
    It has a constructor that allows to pass in an address. Then you can call LoadAssetAsync and it will work. However, RuntimeKeyIsValid() will still return false because it will try to confirm it has valid GUID, while the underlying resource provider doesn't care, it works with either guid or an address.

    This class needs to be redesigned IMHO.
     
    davidrochin likes this.
  9. aka3eka

    aka3eka

    Joined:
    May 10, 2019
    Posts:
    32
    As far as I can see from both Addressables sources and VisualStudio code hint, AssetReference has only two overloads of constructor: the empty one and the one that accepts string as guid:

    Code (CSharp):
    1. public AssetReference ()
    2.     {
    3.     }
    4.  
    5.     public AssetReference (string guid)
    6.     {
    7.         m_AssetGUID = guid;
    8.     }
    What is it? Kind of undocumented feature? I don't think it's a good idea to use it at least in production builds (it might become unavailable in future releases).
     
  10. phobos2077

    phobos2077

    Joined:
    Feb 10, 2018
    Posts:
    350
    Yeah, kind of. I think you're right. AssetReference is a way to reference specific asset in a project, so using guid kinda makes sense. Cases where you need to instantiate AssetReference directly are very limited (probably related to custom inspector extensions etc.). If you just need to load asset by an address you already have, you don't use AssetReference for this.
     
    aka3eka likes this.
  11. Arthur-LVGameDev

    Arthur-LVGameDev

    Joined:
    Mar 14, 2016
    Posts:
    226
    This same thing tripped us up, too.

    Furthermore, even if the GUID is set (ie RuntimeKeyIsValid() returns true) that still provides no guarantee, at least AFAICT, that the underlying asset is actually marked as addressable -- so even if it returns true, it may not be loadable and will throw an error at runtime.

    We ran into this after writing editor code to transition stuff over to the Addressables system, which I'd assume is a pretty common use-case. It's all solve-able stuff but the dev UX definitely isn't very good, and the API/docs weren't real helpful.

    I know Unity developers care & are working on improvements, but I personally expected a bit more "refined" experience when initially switching over -- I generally expected it to all "just work" even for intricate or 'custom' setups. At least the source is visible, is the saving grace! ;)
     
    RaL, Brightori, aka3eka and 2 others like this.
  12. IAmChiagozie

    IAmChiagozie

    Joined:
    Jun 26, 2017
    Posts:
    23
    I use
    string.IsNullOrEmpty(theAssetRefernce.AssetGUID)
     
    EgoJacky likes this.
  13. Arthur-LVGameDev

    Arthur-LVGameDev

    Joined:
    Mar 14, 2016
    Posts:
    226
    The problem with that is, the GUID can be set but that still provides no guarantee that the underlying asset is marked as addressable. Nor does RuntimeKeyIsValid().

    So, to put it in code:
    Code (csharp):
    1. if(string.IsNullOrEmpty(guid) == false && assetRef.IsRuntimeKeyValid() == true) {
    2. // You STILL do NOT have any guarantees here!!
    3. }
    So despite both checks, it can still potentially NOT be a valid reference to an addressable asset.

    Note: The above is how it was at the time I wrote my prior posts. Maybe they've fixed this since then, or maybe there's a new method you can use to actually verify/validate that a reference is truly set to an addressable asset -- it's possible this has since been fixed or similar. But, as of the time of writing my initial posts on this topic, the above was true (aka no guarantees, even with both of those checks).
     
  14. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    677
    Here is the most reliable way that I found for it (you can pass the RuntimeKey as the key):
    Code (CSharp):
    1.         public static bool TryGetResourceLocator<T>(object key, out IResourceLocator result)
    2.         {
    3.             if (key != null)
    4.             {
    5.                 foreach (IResourceLocator resourceLocator in Addressables.ResourceLocators)
    6.                 {
    7.                     if (resourceLocator.Locate(key, typeof(T), out _))
    8.                     {
    9.                         result = resourceLocator;
    10.  
    11.                         return true;
    12.                     }
    13.                 }
    14.             }
    15.  
    16.             result = null;
    17.  
    18.             return false;
    19.         }
     
  15. Arthur-LVGameDev

    Arthur-LVGameDev

    Joined:
    Mar 14, 2016
    Posts:
    226
    I haven't read through the Addressables source in a while but, based off the last time I did, this code should indeed solve the issue & allow you to actually verify if the GUID actually resolves to a resource or not. Good stuff. :)
     
    brunocoimbra likes this.
  16. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,796
    Hey all. As this is a very old thread, I'm going to close it down. If you'd like to continue the conversation, please create a new thread. :)
     
Thread Status:
Not open for further replies.