Search Unity

How to check if an addressable asset has been downloaded already?

Discussion in 'Addressables' started by faolad, Jul 23, 2019.

  1. faolad

    faolad

    Joined:
    Jan 27, 2013
    Posts:
    118
    There are some UI elements that display depending on if there is something to download or not.
     
  2. faolad

    faolad

    Joined:
    Jan 27, 2013
    Posts:
    118
    Using Addressables.GetDownloadSizeAsync might work, but I'm looking for an alternative
     
  3. Milfeulle9

    Milfeulle9

    Joined:
    Aug 2, 2015
    Posts:
    6
    Bump, I'd also like to know this.
     
  4. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    @faolad, does GetDownloadSizeAsync work?

    Docstring of GetDownloadSizeAsync:
    /// Determines the required download size, dependencies included, for the specified <paramref name="key"/>.
    /// Cached assets require no download and thus their download size will be 0. The Result of the operation
    /// is the download size in bytes.

    If you check the implementation of the API, it collects all dependencies of the address, then go through each
    IResourceLocation, and do the math. It sounds a right way to go.
     
    Last edited: Aug 1, 2019
    faolad and danilonishimura like this.
  5. faolad

    faolad

    Joined:
    Jan 27, 2013
    Posts:
    118
    GetDownloadSizeAsync seems like the way to go, but at the moment it doesn't work. It always returns 0.
     
  6. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    What situation are you in? Windows stand alone builds? Play mode? some other platform?
    If it's always returning 0 that doesn't seem very helpful, so we'd like to figure out what's going on there.
     
  7. faolad

    faolad

    Joined:
    Jan 27, 2013
    Posts:
    118
    It happens on Windows Stand-alone Build, IOS and in Play Mode. We only have tested on those platforms.
     
  8. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    There was a bug related to this that we recently fixed. Please ensure you have new, never before downloaded content, (or turn off bundle caching in your group settings) and try this again with 1.1.7.
    If it's still giving you 0, we'd love to know.
     
    faolad likes this.
  9. faolad

    faolad

    Joined:
    Jan 27, 2013
    Posts:
    118
    Still giving 0. I ensure everything its new, never before downloaded content.
     
  10. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    Well, two ways we could proceed..
    1. you make a Unity bug report with a repro project and we try to get to the bottom of it.
    2. you put some breakpoints and/or debug logging in our code and let us know where it's getting lost.


    If you choose 2, the place to look is ComputeSize inside AssetBundleProvider.cs. Most likely adding a log at the start would be enough to point us in the right direction

    Code (CSharp):
    1.         public virtual long ComputeSize(IResourceLocation loc)
    2.         {
    3.             Debug.Log("CompSize: " + loc.InternalId + ", " + BundleSize);
    4.             if (!loc.InternalId.Contains("://"))
    5.                 return 0;

    • If InternalId has"://" and BundleSize > 0, then the step would be to use logs or breakpoints to work out why it's returning 0.
    • If BundleSize == 0, then you need to look for "BundleSize = GetFileSize(info.FileName)" inside BuildScriptPackedMode.cs and work out why it's 0
    • If InternalId doesn't have "://" then I don't know how it'll download anyway.
     
    faolad likes this.
  11. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    Just clarify that GetDownloadSizeAsync returns the bundle size required to download (haven't downloaded yet). And once the bundle get downloaded, it will returns 0 (nothing need to download).

    If it always 0, then
    1) The bundle may be downloaded already. Try Caching.ClearCache(); before any AAS code, to clean the cache.
    2) The bundle is a local one. Check Library/com.unity.addressables/StreamingAssetsCopy/aa/Windows/catalog.json search your bundle name, see if it starts with http.

    If still not work, you may want to debug the AddressableImpl.GetDownloadSizeAsync, see what happens internally.
     
    phobos2077, unity_bill and faolad like this.
  12. Keyserjaya99

    Keyserjaya99

    Joined:
    Nov 17, 2016
    Posts:
    10
    Try change Play Mode Script to "Simulate Groups (Advanced)" it works for me
    My Addressable version is 1.3.3

    upload_2019-11-6_19-50-42.png
     
    mertbsn likes this.
  13. better_walk_away

    better_walk_away

    Joined:
    Jul 12, 2016
    Posts:
    291
    Bump this question now. In Addressables 1.11.2,
    GetDownloadSizeAsync 
    always returns the actual file size of the bundle, so this method doesn't work now.

    Edit:
    Yeah, I am pretty sure things have changed in Addressables 1.11.2. I still can find the sentence
    Cached assets require no download and thus their download size will be 0. 
    in the doc of the Addressables 1.10.0. But the doc of Addressables 1.11.2 isn't out yet. And I have tested Addressables 1.10.0, the
    GetDownloadSizeAsync 
    method did return zero if the bundle has been downloaded. However, in Addressables 1.11.2, they seem to have change the way
    GetDownloadSizeAsync 
    works, now it always returns the actual file size no matter what.
     
    Last edited: Jul 6, 2020
    aka3eka and phobos2077 like this.
  14. Jhaire

    Jhaire

    Joined:
    Nov 30, 2012
    Posts:
    7
    Yeah I've noticed this behaviour after upgrading to1.11.2. The 1.11.2 documentation still has the line: "Cached assets require no download and thus their download size will be 0."

    Is this a bug with the latest version or has the documentation not been updated? Can someone from Unity let us know what the correct way to tell if a remote asset is cached?
     
    aka3eka likes this.
  15. aka3eka

    aka3eka

    Joined:
    May 10, 2019
    Posts:
    32
    oivchenko likes this.
  16. CodeBombQuinn

    CodeBombQuinn

    Joined:
    Apr 17, 2018
    Posts:
    23
    Just wanted to confirm our team is in the same boat here. And having to comply with Apple's requirement of "show an approval dialog to download assets" is going to show every time now even if things are cached. Would love a hotfix for this if possible.
     
  17. aka3eka

    aka3eka

    Joined:
    May 10, 2019
    Posts:
    32
    Addressables 1.13.1 is out and it's changelog says "Fixed bug where GetDownloadSizeAsync was returning non-zero values for cached AssetBundles.".
    Have anyone tested it already?
     
  18. PascalZieglerAzuryLiving

    PascalZieglerAzuryLiving

    Joined:
    Aug 3, 2020
    Posts:
    11
    I did. It's fixed.
     
    better_walk_away and oivchenko like this.
  19. Kultie

    Kultie

    Joined:
    Nov 14, 2018
    Posts:
    48
    Noted that in 1.13.1 the GetDownloadSizeAsync works fine.
    But in my app i'm having a storage management feature. And since Addressable.ClearDependancy is useless i'm using Directory.Delete with path and bundle name from Addressable.
    The solution work fine since it only delete what I want to delete but it seem conflict with the GetDownloadeSizeAsync that the function will return non-zero bundle size for bundle that has been downloaded and I checked that only 1 bundle delete from cache.

    TLDR: GetDownloadSizeSync work fine until you use Directory.Delete to remove bundle from cache (Addressable 1.13.1)
     
  20. dsavickij

    dsavickij

    Joined:
    May 17, 2019
    Posts:
    12
    @unity_bill Addressables 1.16.10 GetDownloadSizeAsync for bundle returns 0, but for asset inside bundle result is non 0, any suggestions? as far as I understand this isn't expected behavior
     
    Last edited: Mar 3, 2021
  21. Nikhil_creaxt

    Nikhil_creaxt

    Joined:
    Mar 2, 2021
    Posts:
    6
    @unity_bill I am facing the same issue related to download size 0 in editor ( all modes ) and on android build.
    here is the code one of developer written :

    Code (CSharp):
    1. private IEnumerator Process()
    2.         {
    3.             // Collect all the resource locations.
    4.             var loadResourceLocationHandle = Addressables
    5.                 .LoadResourceLocationsAsync(assetLabelReference);
    6.             yield return loadResourceLocationHandle;
    7.             // Get the size of the recources that are required to be downloaded
    8.             // as dependencies of the loaded resource locations above
    9.             var getDownloadSizeHandle = Addressables
    10.                 .GetDownloadSizeAsync(loadResourceLocationHandle.Result);
    11.             yield return getDownloadSizeHandle;
    12.             // If the download size if above zero, i.e. the resources haven't already
    13.             // been downloaded, proceed to downloading the resources, else stop
    14.             // further execution.
    15.             /* foreach (var item in assetLabelReference)
    16.             Debug.Log(item.labelString + " ******************************** " + getDownloadSizeHandle.Result + " *******************************"); */
    17.             if (getDownloadSizeHandle.Result == 0)
    18.             {
    19. // #if UNITY_EDITOR
    20. //                 foreach (var item in assetLabelReference)
    21. //                     PlayerPrefs.SetInt(item.labelString, 1);
    22. // #endif
    23.                 onDependenciesDownloaded.Invoke();
    24.                 yield break;
    25.             }
    26.             var downloadDependenciesHandle = Addressables
    27.                 .DownloadDependenciesAsync(loadResourceLocationHandle.Result);
    28.             while (downloadDependenciesHandle.IsDone == false)
    29.             {
    30.            
    31.                 Debug.Log("Download progress " + downloadDependenciesHandle.PercentComplete + " *******************************");
    32.                 onDownloadingDependencies.Invoke(downloadDependenciesHandle.PercentComplete);
    33.                 yield return null;
    34.             }
    35.             foreach (var item in assetLabelReference)
    36.             Debug.Log(item.labelString + " ******************************** " + getDownloadSizeHandle.Result + " *******************************");
    37.             // Set the player prefs so that thay can be checked to know the status
    38.             // of the dependencies, i.e. if they have been downloaded or not.
    39.             foreach (var item in assetLabelReference)
    40.                 PlayerPrefs.SetInt(item.labelString, 1);
    41.             onDependenciesDownloaded.Invoke();
    42.         }
    43.  


    please help me out here as i am stuck on this bug.
     
  22. KwahuNashoba

    KwahuNashoba

    Joined:
    Mar 30, 2015
    Posts:
    110
    Have you tried adding
    yield return Addressables.ClearDependencyCacheAsync(assetLabelReference, false);
    at the beggining of that method? It might happen that you have them cached already for some reason.
     
  23. Nikhil_creaxt

    Nikhil_creaxt

    Joined:
    Mar 2, 2021
    Posts:
    6
    @KwahuNashoba I don't think so it might be cached because on first run it should not be cached until downloaded first.
     
  24. IvanIvanovP3

    IvanIvanovP3

    Joined:
    Jan 24, 2021
    Posts:
    6
    I have Addressables.GetDownloadSizeAsync returns 0, but Status = Failed.
    In addition to checking Result, you need to check Status.
     
  25. giulychuu

    giulychuu

    Joined:
    Apr 1, 2021
    Posts:
    7
    i have this problem. pls help me
    this is my code:
    Code (CSharp):
    1.   IEnumerator DownloadAsset()
    2.     {
    3.         var loadResourceLocationsAsync = Addressables.LoadResourceLocationsAsync(required);
    4.  
    5.         yield return loadResourceLocationsAsync;
    6.         resourceLocations = loadResourceLocationsAsync.Result;
    7.         var downloadDependenciesAsync = Addressables.DownloadDependenciesAsync(resourceLocations);
    8.         var totalBytes = downloadDependenciesAsync.GetDownloadStatus().TotalBytes;
    9.         Debug.Log(Addressables.GetDownloadSizeAsync(resourceLocations).GetDownloadStatus().TotalBytes + " "
    10.             + downloadDependenciesAsync.GetDownloadStatus().TotalBytes);
    11.  
    12.         do
    13.         {
    14.             Debug.Log("" + $"Downloading update {downloadDependenciesAsync.GetDownloadStatus().DownloadedBytes}B/{totalBytes}B");
    15.             Debug.Log(downloadDependenciesAsync.Status + "  " + downloadDependenciesAsync.IsDone);
    16.             yield return null;
    17.         } while (!downloadDependenciesAsync.IsDone && downloadDependenciesAsync.Status != AsyncOperationStatus.Failed);
    18.  
    19.  
    20.         Debug.Log(downloadDependenciesAsync.Status + "  " + downloadDependenciesAsync.IsDone);
    21.  
    22.         yield return downloadDependenciesAsync;
    23.         StartCoroutine(LoadAssets());
    24.     }
    25.  
    26.  
    27.  
    28.     IEnumerator LoadAssets()
    29.     {
    30.         // text.text = "Loading";
    31.         AsyncOperationHandle<IList<GameObject>> loadArena = Addressables.LoadAssetsAsync<GameObject>("arena", obj =>
    32.         {
    33.             //Gets called for every loaded asset
    34.             Debug.Log(obj.name);
    35.             Instantiate(obj);
    36.         });
    37.         yield return loadArena;
    38.         //&   text.text = "finished";
    39.  
    40.     }
     
  26. Shubham_rgupta

    Shubham_rgupta

    Joined:
    Sep 15, 2021
    Posts:
    1
    @unity_bill
    Still i'm facing this issue while downloading the assets, the result is always 0 Screenshot 2023-04-27 112022.png
     
  27. highpockets

    highpockets

    Joined:
    Jan 17, 2013
    Posts:
    71
    It seems odd that you can't provide the Addressables.MergeMode when trying to get the download time. Is this is a missing feature or did I miss something in the docs?? If I want to use MergeMode.Intersection and the download time always returns the Union, then the number of assets will never match in my case. Is there a way around this?