Skip to content

Instantly share code, notes, and snippets.

@asus4
Created April 4, 2012 14:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save asus4/2301758 to your computer and use it in GitHub Desktop.
Save asus4/2301758 to your computer and use it in GitHub Desktop.
another WWW.LoadFromCacheOrDownload for Unity
using UnityEngine;
using System;
using System.Collections;
using System.IO;
/// <summary>
/// Defalult WWW.LoadFromCacheOrDownload() can use only AssetBundle
/// this eneble chaching any file.
/// </summary>
public class WWWCache {
string strToBase64(string str) {
byte[] byt = System.Text.Encoding.UTF8.GetBytes(str);
return Convert.ToBase64String(byt);
}
string base64ToStr(string base64) {
byte[] b = Convert.FromBase64String(base64);
return System.Text.Encoding.UTF8.GetString(b);
}
string urlToCachePath(string url) {
return Application.persistentDataPath +"/"+ strToBase64(url);
}
public bool HasCache(string url) {
return File.Exists(urlToCachePath(url));
}
public void WriteCache(WWW www) {
File.WriteAllBytes(urlToCachePath(www.url), www.bytes);
}
public string CacheUrl(string url) {
string path = urlToCachePath(url);
if(File.Exists(path)) {
return "file://" + path;
}
Debug.LogWarning("dont have cache url:"+url);
return "";
}
}
@aeldron
Copy link

aeldron commented Aug 26, 2015

WWW.LoadFromCacheOrDownload does not store files at Application.persistentDataPath. It stores it at separate local cache which is managed by the OS. The WebPlayer shared cache allows up to 50 MB of cached AssetBundles. PC/Mac Standalone applications and iOS/Android applications have a limit of 4 GB. When the cache is full, the OS will delete the oldest files to make room for new ones (like any proper cache system should do). What would happen when your cache hits the limit? Also, files saved to Application.persistentData path are automatically backed up to iCloud. Consider using Application.temporaryCachePath instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment