Search Unity

Asset bundle saved in cache but allways download again on restart

Discussion in 'Asset Bundles' started by marek1990, Jan 24, 2019.

  1. marek1990

    marek1990

    Joined:
    Dec 9, 2015
    Posts:
    8
    Hello,
    I have an issue with Asset Bundles on Android and in Editor. My asset bundle is a simple pack of txt files (about 2,000) with level settings. For test purposes i put there (not used) graphics to make asset bundle larger (to debug network usage, txt only will not cause any spike in wifi debugger).
    What I want to do is:
    1. Put default Asset Bundle in StreamingAssets to support offline players.
    2. Allow game to download updated Asset Bundle and use updated version with new level settings.

    And now I've tried to download bundle from StreamingAssets directory to cache using:
    Code (CSharp):
    1. IEnumerator GetLocalData()
    2. {
    3.     string uri = Path.Combine(Application.streamingAssetsPath, "Android");
    4.     var request = UnityWebRequestAssetBundle.GetAssetBundle(uri, 0);
    5.     yield return request.SendWebRequest();
    6.     AssetBundle manifestBundle = DownloadHandlerAssetBundle.GetContent(request);
    7.     AssetBundleManifest manifest = manifestBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
    8.     Hash128 hash = manifest.GetAssetBundleHash("levels");
    9.     manifestBundle.Unload(true);
    10.     CachedAssetBundle cachedAssetBundle = new CachedAssetBundle("levels", hash);
    11.     uri = Path.Combine(Application.streamingAssetsPath, "levels");
    12.     request = UnityWebRequestAssetBundle.GetAssetBundle(uri,cachedAssetBundle, 0);
    13.     yield return request.SendWebRequest();
    14. }
    Then compare local Hash128 to remote Hash128. If they are different I'am checking if remote version is already cached with:

    Caching.IsVersionCached("levels", remoteBundleHash128);


    And if there is no cache for remote version I'am starting the download

    Code (CSharp):
    1. IEnumerator DownloadFromRemote(Hash128 remoteHash)
    2. {
    3.     string uri = "http://myhosting.domain.com/levels";
    4.     var request = UnityWebRequestAssetBundle.GetAssetBundle(uri, remoteHash, 0);
    5.     yield return request.SendWebRequest();
    6.     AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
    7. }
    And until now everything is ok. I can check cache if Asset Bundles are here.

    Code (CSharp):
    1. List<Hash128> cachedVersions = new List<Hash128>();
    2. Caching.GetCachedVersions("levels", cachedVersions);
    3. for (int i = 0; i < cachedVersions.Count; i++)
    4. {
    5.     Debug.Log(cachedVersions[i] + " is cached: " + Caching.IsVersionCached("levels", cachedVersions[i]));
    6. }
    When I stop and run game again I can call above cache checking again and it says that both local and remote bundles are cached. But after restart when I try to get content from Asset Bundle they are downloaded again from remote. My get data code:
    Code (CSharp):
    1. IEnumerator GetDataFromAssetBundle()
    2. {
    3.     string uri = "http://myhosting.domain.com/levels";
    4.     var request = UnityWebRequestAssetBundle.GetAssetBundle(uri,remoteHash128, 0);
    5.     yield return request.SendWebRequest();
    6.     var bundle = DownloadHandlerAssetBundle.GetContent(request
    7.     //get needed data here
    8.     bundle.Unload(true);
    9. }
    10.  
    How to avoid downloading this data again or how to read from cache? My Unity3D version is 2018.3.2f1.
    Thank you for help,
    Mark
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    How do you determine that it is downloaded? It doesn't work if you are offline?
     
  3. marek1990

    marek1990

    Joined:
    Dec 9, 2015
    Posts:
    8
    I forgot to close this topic, sorry. My issue is now fixed. I found what is wrong. I download AssetBundle from remote and save it's Hash128 to PlayerPrefs string. Then on restart I try to

    Hash128.Compute(PlayerPrefs.GetString("Hash"))


    instead of using parse method everything works great.

    Hash128.Parse(PlayerPrefs.GetString("Hash")


    Now Hash128 match version in Cache and everything is working.

    Thank you for replay.
     
  4. openixdev

    openixdev

    Joined:
    Jan 2, 2019
    Posts:
    1