Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

How to check if an asset exists with given key

Discussion in 'Addressables' started by mnarimani, Jul 9, 2019.

  1. mnarimani

    mnarimani

    Joined:
    Mar 27, 2017
    Posts:
    216
    I want to check if an asset exists or not before calling Instantiate method.
     
  2. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    393
    Addressables.LoadResourceLocationsAsync(address, type), then check the if the location exists.
     
  3. mnarimani

    mnarimani

    Joined:
    Mar 27, 2017
    Posts:
    216
    This method also prints error when the address doesn't exist.
    I need a silent method that just returns a status of an asset.
     
  4. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    393
    Then it is a bug. Could submit a bug report to Unity?
     
  5. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    1,002
    This is definitely a bug. It would seem that we had a partial fix for this, and simply didn't deliver. We will get to to fixing this.
     
  6. RobbyZ

    RobbyZ

    Joined:
    Apr 17, 2015
    Posts:
    32
    I am currently doing this via the below technique, but it is inefficient and only works in editor. It seems like a simple 'hasAddress' method should exist for this somewhere.

    Code (CSharp):
    1. public static bool DoesAddressExist(AddressableAssetSettings settings, string testAddress)
    2. {
    3.     List<AddressableAssetEntry> allEntries = new List<AddressableAssetEntry>();
    4.     settings.GetAllAssets(allEntries);
    5.    
    6.     foreach (AddressableAssetEntry entry in allEntries)
    7.     {
    8.         if (entry.address == testAddress)
    9.             return true;
    10.     }
    11.  
    12.     return false;
    13. }
     
  7. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    157
    Here is how I do it.

    Code (CSharp):
    1.  
    2.         public static bool AssetExists(object key) {
    3.             if (Application.isPlaying) {
    4.                 foreach (var l in Addressables.ResourceLocators) {
    5.                     IList<IResourceLocation> locs;
    6.                     if (l.Locate(key, null, out locs))
    7.                         return true;
    8.                 }
    9.                 return false;
    10.             }
    11.             else if (Application.isEditor && !Application.isPlaying) {
    12. #if UNITY_EDITOR
    13.                 // note: my keys are always asset file paths
    14.                 return FileExists(System.IO.Path.Combine(Application.dataPath, (string)key));
    15. #endif
    16.             }
    17.             return false;
    18.         }
    19.  
    Addressables must have been initialized (Addressables.InitializeAsync() must have completed.)

    Then you can do a simple sync call of AssetExists(myAssetPathToCheck)

    This still works in 1.1.7
    It is very convenient because you can just await for Addressables.InitializeAsync() once when you start your game, and then can always do the sync AssetExists call.

    I don't know why Addressable devs don't provide something simple like this for such a basic and highly needed requirement.

    Just to check whether an asset exists they want you to do an async call.

    I use AssetExists for "convention over configuration" style code, where, for example, the game checks if an image exists or not and if it doesn't then uses the default image. Being able to do it in a sync call greatly reduces the code complexity and helps with perceived performance.
     
  8. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    1,002
    You answered your own question. We do that so it can be called at any time (even before init is done), and will give you a correct result when ready.

    fixed in 1.1.7
     
  9. DigitalContinueChar

    DigitalContinueChar

    Joined:
    May 21, 2019
    Posts:
    1
    For all those coming in the future, this method seems to be deprecated, as the ResourceLocators don't seem to support Locate anymore.

     
  10. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    157
    Hmm... I'm still using it just fine in 1.4.0.

    And to answer unity_bill .. the advantage of my method is you can await Addressables.InitializeAsync() once in the initial loading screen of your game. (Addressables Initialize only once anyways.) Then you can use my AssetExists method synchronously anywhere afterwards.

    While if you use the built-in method, then you will have to do an async call every time you want to just check if an asset exists .. which many times are in places that can't or you don't want to use async cause it will complicate your code a lot more.
     
  11. caochao88_unity

    caochao88_unity

    Joined:
    May 16, 2019
    Posts:
    26
    maybe you should try async/await:
    Code (CSharp):
    1. var list = await Addressables.LoadResourceLocationsAsync(assetId).Task;
    2.             if (list != null && list.Count > 0)
    3.             {
    4.             }
     
  12. caochao88_unity

    caochao88_unity

    Joined:
    May 16, 2019
    Posts:
    26
    do I need release the AsyncOperationHandle returned by LoadResourceLocationsAsync?
     
    Last edited: Mar 2, 2020
  13. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    157
    I know that async/await can be used. I use async/await throughout my code where needed. The point of my code above is that just to check whether an asset at a path exists I don't want to have to use async/await.
     
  14. caochao88_unity

    caochao88_unity

    Joined:
    May 16, 2019
    Posts:
    26
    Code (CSharp):
    1. var list = await Addressables.LoadResourceLocationsAsync(assetAddr).Task;        
    2. if (list != null && list.Count > 0)        
    3. {
    4. //assetAddr is existed in Addressable Groups
    5. }
    I think this codes is as easy as sync mode codes.
     
    Last edited: Mar 2, 2020
  15. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    157
    There many places in code where it isn't convenient to use async, such as property getters/setters.

    Anyways, we can agree to disagree. I am just explaining my reasons.
     
    Julien-Lynge likes this.
unityunity