• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Dizis · Apr 06, 2016 at 06:16 PM · assetbundlewwwassetbundlescaching

AssetBundles caching bug?

What happened

Asset bundles cac$$anonymous$$ng does not work properly via WWW.LoadFromCacheOrDownload() method. When cache is almost full and there is not enough space for new asset bundle, previously downloaded asset bundles are not removed to free occupied space in spite of the fact that they are already unloaded with AssetBundle.Unload() method. Actually they are not removed until you relaunch the application. The bug can be reproduced in last versions of Unity since v5.3.3 on mobile devices and in UnityEditor too. Unity v5.3.0-v5.3.2 works fine. Note: asset bundle files are created for Android platform;

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 public class AssetBundlesLoader : MonoBehaviour 
 {
     const int KB = 1024;
     const int Version = 0;
 
     List<string> urls;
     Dictionary<WWW, string> wwwList = new Dictionary<WWW, string>();
 
     List<KeyValuePair<string, LogType>> log = new List<KeyValuePair<string, LogType>>();
     float logWidth = 0;
     float logHeight = 0;
     Vector2 scrollVector = Vector2.zero;
     System.Object addMessageLock = new System.Object();
 
     void Start () 
     {
         Application.logMessageReceivedThreaded += AddMessage;
 
         Cac$$anonymous$$ng.maximumAvailableDiskSpace = 80 * KB * KB;
 
         urls = new List<string>()
         {
             "http://hprc.azurewebsites.net/temp/android/1.bin",
             "http://hprc.azurewebsites.net/temp/android/2.bin",
             "http://hprc.azurewebsites.net/temp/android/3.bin",
             "http://hprc.azurewebsites.net/temp/android/4.bin",
             "http://hprc.azurewebsites.net/temp/android/5.bin",
             "http://hprc.azurewebsites.net/temp/android/6.bin",
             "http://hprc.azurewebsites.net/temp/android/7.bin"
         };
     }
     
     void LoadAssetBundle(string url)
     {
         StartCoroutine(DownloadAssetBundle(url, Version));
     }
 
     IEnumerator DownloadAssetBundle (string url, int version)
     {            
         w$$anonymous$$le (!Cac$$anonymous$$ng.ready)
             yield return null;
 
         using(WWW www = WWW.LoadFromCacheOrDownload (url, version))
         {
             wwwList.Add(www, url);
 
             Debug.Log("Loading AssetBundle: " + url);
             yield return www;
             Debug.Log("Loaded AssetBundle: " + url);
 
             if (www.error == null) 
             {        
                 var ab = www.assetBundle;
                 if (ab != null) 
                 {
                     ab.Unload(true);
                     Debug.Log("Unloaded AssetBundle: " + url);
                 }
             }
             else
                 Debug.LogErrorFormat("Download AssetBundle '{0}' error: {1}", url, www.error);
 
             if (wwwList.ContainsKey(www))
                 wwwList.Remove(www);
 
             www.Dispose();
         }
     }
 
     void OnGUI()
     {
         float y = 5;
         const float textHeight = 20;
         const int buttonHeight = 25;
         const int margin = 5;
 
         GUI.Label(new Rect(0, y, Screen.width, textHeight), string.Format("IsCac$$anonymous$$ngEnabled = '{0}'", Cac$$anonymous$$ng.enabled));
         y += textHeight;
 
         GUI.Label(new Rect(0, y, Screen.width, textHeight), string.Format("IsCac$$anonymous$$ngReady = '{0}'", Cac$$anonymous$$ng.ready));
         y += textHeight;
 
         GUI.Label(new Rect(0, y, Screen.width, textHeight), string.Format("MaxDiskSpace = {0:0.0 MB}", Cac$$anonymous$$ng.maximumAvailableDiskSpace/(KB*KB*1f)));
         y += textHeight;
 
         GUI.Label(new Rect(0, y, Screen.width, textHeight), string.Format("SpaceFree = {0:0.0 MB}", Cac$$anonymous$$ng.spaceFree/(KB*KB*1f)));
         y += textHeight;
 
         GUI.Label(new Rect(0, y, Screen.width, textHeight), string.Format("SpaceOccupied = {0:0.0 MB} ({1:0.0%})",
             Cac$$anonymous$$ng.spaceOccupied/(KB*KB*1f),
             Cac$$anonymous$$ng.maximumAvailableDiskSpace > 0
             ? (float)Cac$$anonymous$$ng.spaceOccupied / Cac$$anonymous$$ng.maximumAvailableDiskSpace
             : 0));
         y += textHeight;
 
         if (GUI.Button(new Rect(0, y, 150, buttonHeight), "Clean Cache"))
             Cac$$anonymous$$ng.CleanCache();
         y += buttonHeight;
 
         GUI.Label(new Rect(0, y, Screen.width, textHeight), string.Format("-------- AssetBundles[{0}] --------", urls.Count()));
         y += textHeight;
 
         int n = 1;
         foreach (var u in urls) 
         {
             if (GUI.Button(new Rect(0, y, buttonHeight * 1.5f, buttonHeight), "D" + (n++)))
                 LoadAssetBundle(u);
 
             var c = GUI.color;
             if (Cac$$anonymous$$ng.IsVersionCached(u, Version))
                 GUI.color = Color.yellow;
             
             GUI.Label(new Rect(buttonHeight * 1.5f + margin, y, Screen.width, textHeight), u);
             y += buttonHeight + margin;
 
             GUI.color = c;
         }
 
         GUI.Label(new Rect(0, y, Screen.width, textHeight), string.Format("------------ Loading[{0}] ------------", wwwList.Count()));
         y += textHeight;
 
         foreach (var p in wwwList) 
         {
             GUI.HorizontalScrollbar(new Rect(0, y + 3, Screen.width, buttonHeight), 0, p.Key.progress, 0, 1);
 
             GUI.Label(new Rect(margin, y, Screen.width, textHeight), p.Value);
             y += textHeight;
         }
 
         if (!wwwList.Any())
             y += textHeight;
 
         GUI.Label(new Rect(0, y, Screen.width, textHeight), string.Format("-------------- Log[{0}] --------------", log.Count()));
         y += textHeight;
 
         logWidth = Mathf.Max(logWidth, Screen.width);
         logHeight = Mathf.Max(logHeight, Screen.height - y - margin);
 
         scrollVector =
             GUI.BeginScrollView(
                 new Rect(0, y + margin, Screen.width, Screen.height - y - margin), scrollVector, new Rect(0, 0, logWidth, logHeight));
 
         GUI.Box(new Rect(0, 0, logWidth, logHeight), string.Empty);
 
         logHeight = 0;
 
         lock (addMessageLock) 
         {
             int i = 1;
             var content = new GUIContent ();
             foreach (var p in log)
             {
                 content.text = p.Key;
                 logWidth = Mathf.Max (logWidth, GUI.skin.label.CalcSize (content).x);
 
                 var c = GUI.color;
                 if (p.Value != LogType.Log)
                     GUI.color = p.Value == LogType.Warning ? Color.yellow : Color.red;
 
                 GUI.Label (new Rect (margin, logHeight, Screen.width, textHeight), string.Format ("{0}) {1}", i++, p.Key));
                 logHeight += textHeight;
 
                 GUI.color = c;
             }
         }
 
         GUI.EndScrollView ();
     }
 
     void AddMessage(string text, string stackTrace, LogType type) 
     {
         lock (addMessageLock) 
         {
             log.Add (new KeyValuePair<string, LogType> (text, type));
         }
     }
 }
 

You can reproduce it using the example we attached

  • Open project in Unity v5.3.3-v5.4.0b;

  • Switch platform to Android (not nessesary);

  • Run the project in UnityEditor or on Android - you should see the UI like in the attached image: alt text

  • If "SpaceOccupied" value is not 0 then press "ClearCache" button;

  • Press "D1" button and wait for downloading of asset bundle to finish (after downloading script calls "AssetBundle.Unload(bool)" method);

  • In "AssetBundles" section cached asset bundles are marked in yellow color;

  • Press "D2" button and wait for downloading of asset bundle to finish;

  • Press "D3" button and wait for downloading of asset bundle to finish. After t$$anonymous$$s step cache is almost full and there is not enough space for another one asset bundle;

  • Press "D4" button and look at result:

ACTUAL RESULT: When you press "D4" button the warning message appeares in Unity Console: "Not enough space in cache to write file". The asset bundle is being downloaded but not cached.

EXPECTED RESULT: New asset bundle should be downloaded and cached, w$$anonymous$$le the oldest one should be removed from cache (they all are UNLOADED - AssetBundle.Unload(bool) method is called on each of them).

Bug report link: https://fogbugz.unity3d.com/default.asp?786259_96f53dq8ue9j26o0

Project arc$$anonymous$$ve link: link text

assetbundlescachingbug.png (171.9 kB)
assetbundlescachingbug.zip (30.4 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gguonextfloor · May 10, 2016 at 02:19 PM 0
Share

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Managing assetbundles (caching vs non-caching) 0 Answers

Assetbundle memory leaks 0 Answers

How Unity Generates Cache Hash For AssetBundles 1 Answer

How do you package/retrieve Asset Bundle Dependencies 0 Answers

LoadFromCacheOrDownload non asset bundles 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges