Search Unity

UnityWebRequestAssetBundle.GetAssetBundle should load from cache if no internet?

Discussion in 'Asset Bundles' started by kolmichcreations, Jun 10, 2021.

  1. kolmichcreations

    kolmichcreations

    Joined:
    Sep 12, 2014
    Posts:
    10
    Hi,

    I'm using UnityWebRequestAssetBundle.GetAssetBundle with the hash parameter (uses caching) so it compares the remote and cached hashes and only downloads the assetbundles if the hash changes. This is working fine.

    Now i'm trying to make this work correctly when my device is not connected to the internet.
    I assume the correct behaviour would be that if i already have a locally cached assetbundle and there is no internet connection UnityWebRequestAssetBundle.GetAssetBundle should load the available assetbundle from cache.

    Unfortunately this is not working. I also cannot load it directly from the cache folder cause the cache folder does not contain the assetbundles but some kind of extracted version of them.

    I cant find a solution for this in the documentation. Any suggestions?

    Not having this functionality would mean that apps/games that are updating content by using assetbundles can't be used offline?

    Thank you!
     
  2. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Yes, if you pass a hash into a getassetbundle call, and that version is available, it should load just fine even without internet.

    If this doesn't work for you, can you post your code?
     
  3. kolmichcreations

    kolmichcreations

    Joined:
    Sep 12, 2014
    Posts:
    10
    Thank you very much nilsdr!

    After checking my code again i figured out that the first assetbundle i'm loading is the assetbundle containing the assetbundlemanifest which contains all the hashes for the assetbundles on the server. I'm loading this without passing a hash code so this is the moment i get the error. Makes sense.

    So i will have to change my question:

    1) OK, I can skip downloading the assetbundle with the manifest if there is no internet connection.
    2) When i pass the hashes of the cached assetbundles to GetAssetBundle it should not try to download over the internet but load them from cache.

    But how do i get the hashes of the cached assetbundles so i can pass them to GetAssetBundle to force load from cache?
     
    nilsdr likes this.
  4. kolmichcreations

    kolmichcreations

    Joined:
    Sep 12, 2014
    Posts:
    10
    OK, i found a solution.

    I'm saving the hashes of all assetbundles i'm getting from the manifest file during the download to playerprefs. If no internet available i simply use those hash values, and indeed GetAssetbundle is loading correctly from cache now if no internet available.

    Thank you nilsdr!
     
    nilsdr likes this.