Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need some help on LoadFromCacheOrDownload()

Discussion in 'Editor & General Support' started by ajketan, Mar 7, 2014.

  1. ajketan

    ajketan

    Joined:
    Nov 27, 2013
    Posts:
    20
    Hi

    Our game requires a system that downloads any new content from the net and uses it in game. I am using LoadFromCacheOrDownload() for it.

    It works fine ... it downloads my content and all is good till here.

    Now ideally when once the content is downloaded it should not download again. But that is happening with me. On my PC I can see the cached assetBundle getting deleted from the cache folder when I run the game again and then it downloads again. This also happens on my iOS Devices.

    How do I restrict that ?

    here is the code that I am using.....
    Code (csharp):
    1.  
    2.  
    3.     private IEnumerator load(string url, int version)
    4.     {
    5.         // wait for the caching system to be ready
    6.         while (!Caching.ready)
    7.           yield return null;
    8.  
    9.         // load AssetBundle file from Cache if it exists with the same version or download and store it in the cache
    10.         if(Caching.IsVersionCached(url, 1) == false)
    11.         {
    12.             www = WWW.LoadFromCacheOrDownload(url, version);
    13.             yield return www;
    14.             Debug.Log("Downloading again");
    15.         }
    16.         else
    17.         {
    18.             Debug.Log("Downloading again is not required....already in cache");
    19.         }
    20.  
    21.         Debug.Log(" Loaded ");
    22.  
    23.         if (www.error != null)
    24.           throw new Exception("WWW download had an error: " + www.error);
    25.    
    26.         AssetBundle assetBundle = www.assetBundle;
    27.         AssetBundleRequest request = assetBundle.LoadAsync ("myObject", typeof(GameObject));
    28.         yield return request;
    29.  
    30.         if(request.isDone == false)
    31.         {
    32.             //request.allowSceneActivation = true;
    33.         }
    34.         else
    35.         {
    36.             GameObject myObj = assetBundle.mainAsset as GameObject;
    37.             Instantiate(myObj);
    38.             //request.allowSceneActivation = false;
    39.         }
    40.    
    41.         // Unload the AssetBundles compressed contents to conserve memory
    42.         assetBundle.Unload(false);
    43.     }
    44.  
    45.  
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    The cache is pretty small, so I imagine that you're filling the cache up and it's deleting old assets to make space for the new ones. You can buy a larger cache.
     
  3. ajketan

    ajketan

    Joined:
    Nov 27, 2013
    Posts:
    20
    Sorry but I didn't get what you are trying to say...

    My point is if I have downloaded an assetBundle from my server to the device, it should be there with me for further use.
    And Can I not save it?

    Consider this assetbundle as an Upgrade to a weapon used in my game.
     
  4. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Asset bundles are compressed when they are transferred to your computer, but stored uncompressed in the cache. If the cache fills up, then Unity will delete the oldest asset bundle to make space for new bundles. And doesn't Unload() remove the asset bundle from the cache?