Search Unity

Downloading Asset Bundles From a Server Doesn't Save Bundles To Cache

Discussion in 'Asset Bundles' started by danieltimmsRFD, Mar 17, 2022.

  1. danieltimmsRFD

    danieltimmsRFD

    Joined:
    Mar 25, 2021
    Posts:
    5
    Hi, I seem to be having trouble saving caches when downloading Asset Bundles from a Google Cloud server. From what I can see in the documentation, it seems like the caching is supposed to happen automatically? Can anyone confirm please? And if this is not the case, what is the solution please?

    Code (CSharp):
    1. IEnumerator Start()
    2.     {
    3.         mainBundle = UnityWebRequestAssetBundle.GetAssetBundle("https://storage.googleapis.com/assetbundles_rfd/StreamingAssets");
    4.         yield return mainBundle.SendWebRequest();
    5.  
    6.         Debug.Log(mainBundle.downloadProgress);
    7.  
    8.         if (mainBundle.result != UnityWebRequest.Result.Success)
    9.         {
    10.             Debug.Log(mainBundle.error);
    11.         }
    12.         else
    13.         {
    14.             AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(mainBundle);
    15.  
    16.             AssetBundleRequest asset = bundle.LoadAssetAsync<AssetBundleManifest>("assetbundlemanifest");
    17.             yield return asset;
    18.  
    19.             //Get the AssetBundleManifest
    20.             AssetBundleManifest loadedAssetMf = asset.asset as AssetBundleManifest;
    21.  
    22.             while (Caching.ready == false)
    23.                 yield return null;
    24.  
    25.             //Get Hash128 from the AssetBundleManifest
    26.             var bundles = loadedAssetMf.GetAllAssetBundles();
    27.  
    28.             foreach (var v in bundles)
    29.             {
    30.                 Hash128 tempHash128 = loadedAssetMf.GetAssetBundleHash(v);
    31.                 bundleHashes.Add(new BundleWrapper
    32.                 {
    33.                     bundleHash = tempHash128,
    34.                     bundleName = v
    35.                 });
    36.  
    37.                 Debug.Log("Is " + v + " cached: " + Caching.IsVersionCached("https://storage.googleapis.com/assetbundles_rfd/" + v, tempHash128));
    38.                 Logger.Instance.AddLog("Is " + v + " cached: " + Caching.IsVersionCached("https://storage.googleapis.com/assetbundles_rfd/" + v, bundleHashes.Where(item => item.bundleName == v).FirstOrDefault().bundleHash));
    39.  
    40.                 if (Caching.IsVersionCached("https://storage.googleapis.com/assetbundles_rfd/" + v, bundleHashes.Where(item => item.bundleName == v).FirstOrDefault().bundleHash) == false)
    41.                 {
    42.                     otherBundles = UnityWebRequestAssetBundle.GetAssetBundle("https://storage.googleapis.com/assetbundles_rfd/" + v);
    43.  
    44.                     Debug.Log("https://storage.googleapis.com/assetbundles_rfd/" + v);
    45.                     Logger.Instance.AddLog("https://storage.googleapis.com/assetbundles_rfd/" + v);
    46.  
    47.                     yield return otherBundles.SendWebRequest();
    48.  
    49.                     if (otherBundles.result != UnityWebRequest.Result.Success)
    50.                     {
    51.                         Debug.Log(otherBundles.error);
    52.                         Logger.Instance.AddError(otherBundles.error);
    53.                     }
    54.                     else
    55.                     {
    56.                         Debug.Log("Success!");
    57.                         Logger.Instance.AddSuccess("Success!");
    58.  
    59.                         AssetBundle otherBundle = DownloadHandlerAssetBundle.GetContent(otherBundles);
    60.                         Instantiate(otherBundle.LoadAsset("Shrek_Model"));
    61.                         bundle.Unload(false);
    62.                     }
    63.                 }
    64.             }
    65.         }
    66.     }
    The full stack of the code is executed, and the "success!" message is called at the end once the uncached bundle has been downloaded, but then it seems that the newly downloaded bundle doesn't ever get saved.

    Any insight would be greatly appreciated, thanks!
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    See documentation for UnityWebRequestAssetBundle.GetAssetBundle.
    You need to provide additional argument for caching to work. The overload that you use is the one that doesn't cache.
     
  3. danieltimmsRFD

    danieltimmsRFD

    Joined:
    Mar 25, 2021
    Posts:
    5
    Which argument is it I need to add please?
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
  5. danieltimmsRFD

    danieltimmsRFD

    Joined:
    Mar 25, 2021
    Posts:
    5
    Thanks, got it.

    Is there a way I can get the Hash of the MAIN AssetBundle which contains the manifest for all the other bundles please? So if the device is offline, it can check if it already has the other asset bundles. Is this possible?
     
  6. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    The documentation is deficient here. It isn't explained how to get the correct hash128 from a downloaded AssetBundle to be saved and used in later queries. And the wording for how to use the version number argument is rather ambiguous as well and poorly explained.