- Home /
AssetBundles caching bug?
What happened
Asset bundles caching 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;
Caching.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)
{
while (!Caching.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("IsCachingEnabled = '{0}'", Caching.enabled));
y += textHeight;
GUI.Label(new Rect(0, y, Screen.width, textHeight), string.Format("IsCachingReady = '{0}'", Caching.ready));
y += textHeight;
GUI.Label(new Rect(0, y, Screen.width, textHeight), string.Format("MaxDiskSpace = {0:0.0 MB}", Caching.maximumAvailableDiskSpace/(KB*KB*1f)));
y += textHeight;
GUI.Label(new Rect(0, y, Screen.width, textHeight), string.Format("SpaceFree = {0:0.0 MB}", Caching.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%})",
Caching.spaceOccupied/(KB*KB*1f),
Caching.maximumAvailableDiskSpace > 0
? (float)Caching.spaceOccupied / Caching.maximumAvailableDiskSpace
: 0));
y += textHeight;
if (GUI.Button(new Rect(0, y, 150, buttonHeight), "Clean Cache"))
Caching.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 (Caching.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:
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 this 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, while 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 archive link: link text
This code sample is really well written.
Can I use this code to submit another asset bundle issue?