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
  3. Dismiss Notice

Check existance of asset before loading?

Discussion in 'Addressables' started by Domarius, Mar 29, 2020.

  1. Domarius

    Domarius

    Joined:
    Jan 5, 2013
    Posts:
    99
    I'm porting my game to Unity, and previously I had optional files that some levels had. Eg. custom animations for certain types of objects. If this custom file didn't exist, a default fallback was used.

    Using addressables, it seems you have to hard code the existance of everything's address in advance. How can I test for the existance of an asset?
     
  2. Cfirzi

    Cfirzi

    Joined:
    Jul 30, 2018
    Posts:
    24
    you need to use Addressables.GetDownloadSizeAsync()

    if (obj.Result == 0) it means it is already downloaded
    anything else means you need to download it
     
  3. Domarius

    Domarius

    Joined:
    Jan 5, 2013
    Posts:
    99
    That is to check if something has already been loaded, which is different than checking if it exists in the first place.

    With some searching I found the right code. It's an async operation...
    https://forum.unity.com/threads/uni...dkeyexception-was-thrown.796908/#post-5301735

    Code (CSharp):
    1. var validateAddress = Addressables.LoadResourceLocationsAsync(address);
    2. await validateAddress.Task;
    3. if (validateAddress.Status == UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationStatus.Succeeded) {
    4.         if (validateAddress.Result.Count > 0) {
    5.                 // asset exists go ahead and load
    6.         }
    7. }
     
    Last edited: Apr 1, 2020