Hi,
I know there are similar questions already, but the answer I found so far are not clear.
My question is: if I download jpg files with www, will they be cached or not?
Hi,
I know there are similar questions already, but the answer I found so far are not clear.
My question is: if I download jpg files with www, will they be cached or not?
WebPlayer will cache - or more correctly, the browser it is running in will cache the results.
All other platforms need you to do a little extra work. however that can be quite minimal due to WWW being able to load from the disk as well. THIS IS ON AT LEAST UNITY 5.2.X. or if it caches it checks for changed on the web server and in most cases you wouldn’t actually want this unless you implicitly request it - what you want is instant disk load if you already have the texture.
so suppose you are already using WWW in many places, you can fairly easily create an utility class that you use instead of the WWW constructor and everything else remains same how you interact with the WWW object.
you can use the following as a base for your solution. or you can go on asset store and buy some other premade solution. how it works? quite simply it waits on the WWW and saves the file to persistentDataPath once it is complete. subsequent loads will then load that file directly. this has been tested on windows and android(unity 5.2.1). the place where you used the WWW before you can keep as is, with the exception of the www constructor getting replaced with this, all that happens is that if the url is on disk already then it will be loaded from there. the place where you use your www will just yield return www same as normal. HOWEVER you should keep in mind that this can change the .url of the WWW object, so if you have some in-memory cache of those WWW objects that you’re sorting by the url then you need to do a little extra to keep them correctly working after moving to something like this.
static public WWW getCachedWWW(string url)
{
string filePath = Application.persistentDataPath;
filePath += "/" + GetInt64HashCode(url);
string loadFilepath = filePath;
bool web = false;
WWW www;
bool useCached = false;
useCached = System.IO.File.Exists(filePath);
if (useCached)
{
//check how old
System.DateTime written = File.GetLastWriteTimeUtc(filePath);
System.DateTime now = System.DateTime.UtcNow;
double totalHours = now.Subtract(written).TotalHours;
if (totalHours > 300)
useCached = false;
}
if (System.IO.File.Exists(filePath))
{
string pathforwww = "file://" + loadFilepath;
Debug.Log("TRYING FROM CACHE " + url + " file " + pathforwww);
www = new WWW(pathforwww);
}
else
{
web = true;
www = new WWW(url);
}
yoursomesclass.instance.StartCoroutine(doLoad(www, filePath, web));
return www;
}
static IEnumerator doLoad(WWW www, string filePath, bool web)
{
yield return www;
if (www.error == null)
{
if (web)
{
//System.IO.Directory.GetFiles
Debug.Log("SAVING DOWNLOAD " + www.url + " to " + filePath);
// string fullPath = filePath;
File.WriteAllBytes(filePath, www.bytes);
Debug.Log("SAVING DONE " + www.url + " to " + filePath);
//Debug.Log("FILE ATTRIBUTES " + File.GetAttributes(filePath));
//if (File.Exists(fullPath))
// {
// Debug.Log("File.Exists " + fullPath);
// }
}
else
{
Debug.Log("SUCCESS CACHE LOAD OF " + www.url);
}
}
else
{
if (!web)
{
File.Delete(filePath);
}
Debug.Log("WWW ERROR " + www.error);
}
}
If you are building for the web player, then yes, the images will be cached. On the web player builds WWW is implement via the web browser, so anything the web browser does happens for the content you pull into the game. On other platforms no caching is implemented.