Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Asset bundles not being cached.

Discussion in 'Asset Bundles' started by Tramplex, May 22, 2020.

  1. Tramplex

    Tramplex

    Joined:
    Dec 10, 2016
    Posts:
    6
    Hello.
    Ive encountered problem where bundles that i download to device are not saved to cache. I tested this with isVersionCached(). I doublechecked url, crc and hash they are same as bundle was downloaded with, but method always returns false. And i dont even need to relaunch application. I tried to load bundles twice and some specific bundles are not getting loaded from cache just download over again.
    There are similar bundles that ARE cached. For example i have bundle maps0 that contains 2 scenes and it is not cached and try to download every time. Same way i have maps1 that has 5 scenes and it is cached.
    Is there something that can prevent bundles from caching?
    Also this behaviour is not present in editor. I though about directives but i removed them all from bundles download part.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,528
    Can you show the code how you are downloading them?
     
  3. Tramplex

    Tramplex

    Joined:
    Dec 10, 2016
    Posts:
    6
    Code (CSharp):
    1. private IEnumerator LoadRemote(string bundleName, CoReciever reciever)
    2.     {
    3.         reciever.Data = null;
    4.         var url = string.Format("{0}{1}", RemoteSourceUrl, bundleName);
    5.         Hash128 hash = new Hash128();
    6.         if (EnableCache)
    7.         {
    8.             if (manifest == null)
    9.             {
    10.                 yield return GetBundlesInfo();
    11.             }
    12.  
    13.             if (manifest != null)
    14.             {
    15.                 hash = manifest.GetAssetBundleHash(bundleName);
    16.             }
    17.         }
    18.  
    19.         float timeout = repeatDealy;
    20.         bool needToRepeat = true;
    21.         int repeatCount = 0;
    22.         var initialByteCount = bytesLoaded;
    23.         string cashBundleName;
    24.         int slashIndex = bundleName.LastIndexOf('/');
    25.         cashBundleName = bundleName.Remove(0, slashIndex + 1);
    26.  
    27.         while (needToRepeat)
    28.         {
    29.             UnityWebRequest request = null;
    30.             if (EnableCache) {
    31.                 if (manifest != null)
    32.                 {
    33.                     request = UnityWebRequestAssetBundle.GetAssetBundle(url, hash, 0);
    34.                     Debug.Log($"Loading bundle with cache URL: {url} Hash: {hash}");
    35.                 }
    36.                 else
    37.                 {
    38.                     request = UnityWebRequestAssetBundle.GetAssetBundle(url, 0);
    39.                     Debug.Log($"Loading bundle no cache URL: {url}");
    40.                 }
    41.             }
    42.  
    43.             request.SendWebRequest();
    44.  
    45.             while (!request.isDone)
    46.             {
    47.                 yield return null;
    48.                 bytesLoaded = initialByteCount + request.downloadedBytes;
    49.             }
    50.  
    51.             if (!string.IsNullOrEmpty(request.error) || request.isNetworkError || request.isHttpError)
    52.             {
    53.                 string errorString = !string.IsNullOrEmpty(request.error) ? request.error : Constants.EMPTY_STRING;
    54.                 Debug.Log($"<color=red>ERROR.</color> AssetBundle {bundleName} load error: {errorString}");
    55.                 timeout = repeatDealy;
    56.                 while (timeout > 0f)
    57.                 {
    58.                     timeout -= Time.unscaledDeltaTime;
    59.                     yield return null;
    60.                 }
    61.  
    62.                 repeatCount++;
    63.                 Debug.Log($"Trying to repeat load {bundleName}. Attempt: {repeatCount}");
    64.  
    65.                 if (!InternetReachability.IsInternetConnected)
    66.                 {
    67.                     SystemMessage.Instance.NoInternetConnectionWarning();
    68.                 }
    69.             }
    70.             else
    71.             {
    72.                 needToRepeat = false;
    73.                 reciever.Data = DownloadHandlerAssetBundle.GetContent(request);
    74.                 RequestLog(bundleName, request.responseCode);
    75.  
    76.                 if (EnableCache && IsOK(request.responseCode))
    77.                 {
    78.  
    79.                     if (Caching.ClearOtherCachedVersions(cashBundleName, hash))
    80.                     {
    81.                         Debug.Log($"Any other <color=yellow>{cashBundleName}</color> bundles is cleared from cash.");
    82.                     }
    83.                 }
    84.  
    85.             }
    86.         }
    87.  
    88.     }
    And this is used for all bundles, ones that cache and ones that dont
     
  4. Tramplex

    Tramplex

    Joined:
    Dec 10, 2016
    Posts:
    6
    So it seems that i have solved my issue. My bundles were named maps/league0, maps/league1 etc. I changed it to not have digits in the end and it is being cached now. If only it was stated somewhere that digits in the end of bundles names are restricted.
    And this doesnt explain why only two bundles with names maps/league0 and leagues/league1 were not cached but others with similar names were cached.
     
unityunity