- Home /
Can I access previously downloaded textures?
Hi guys, didn't found anything related. I'll be using www to get textures but once the player opens the game again I don't want to download the previously downloaded textures, is there a way to access these downloaded textures? Are they stored in some folder?
Answer by Dreamora · Apr 12, 2013 at 06:54 AM
No, things you download through WWW are dropped unless you store them yourself (exception: www.LoadFromCacheOrDownload for assetbundles)
If you store the texture after a download then you can access it through WWW and the file:// protocol again (if its standalone or mobile) If it is the webplayer, just do the request again and the browser will provide it out of the cache.
Edit, re Dreamora's incredibly useful comment below, here's some tested code version of that! Hope it helps someone!
Call it with a gameObject which has a flat plane the size you want with an example image on it, and the URL in question of some jpg.
private IEnumerator Paint(GameObject go, string url)
{
go.renderer.material.mainTexture =
new Texture2D(512,512, TextureFormat.DXT1, false);
var www = new WWW(url);
yield return www;
www.LoadImageIntoTexture(
go.renderer.material.mainTexture as Texture2D);
}
private IEnumerator PaintCachewise(GameObject go, string url)
{
go.renderer.material.mainTexture =
new Texture2D(512,512, TextureFormat.DXT1, false);
Debug.Log("url "+url);
string justFilename = System.IO.Path.GetFileName(url);
Debug.Log("justFilename " +justFilename);
string localName =
Application.persistentDataPath + "/" + justFilename;
Debug.Log("localName " + localName);
if ( System.IO.File.Exists( localName ) )
Debug.Log(" It Exists");
else
Debug.Log(" -- does not exist yet");
if ( System.IO.File.Exists( localName ) )
{
// proudly load from cache with confidence
var ccc = new WWW( "file://" + localName);
yield return ccc;
ccc.LoadImageIntoTexture(
go.renderer.material.mainTexture as Texture2D);
Debug.Log("loaded from speedy local cache.");
}
else
{
var www = new WWW(url);
yield return www;
www.LoadImageIntoTexture(
go.renderer.material.mainTexture as Texture2D);
System.IO.File.WriteAllBytes( localName, www.bytes );
Debug.Log("loaded from planetary cloud data system - "
+" and saved to device SSD");
}
// per Dreamora http://answers.unity3d.com/answers/436748 !!
}
So yes? I can store and access them using WWW and file protocol?
Yes. Another way could to store it permanently could be to write it to a file in the Resources folder and then check it before downloading it again. This is for the standalone build. I dont know if you can read and write files using the browser plugin.
No problem with the webplayer, I'm looking for a solution on iOs/Android :)
Its absolutely no problem on iOS and Android Use www and then store www.bytes onto the device using System.IO.File.WriteAllBytes(Application.persistentDataPath + "/someImageName.xxx",www.bytes); with xxx being the same filetype your requested file from the web has.
later on you can load that image again using new WWW("file://"+Application.persistentDataPath + "/someImageName.xxx")
Answer by lassiiwa · Dec 17, 2015 at 02:40 PM
here's a little more generic cached WWW utility function. you will need to point the hash function to something that you have and the StartCoroutine come from some monobehaviour. just use this instead of your normal WWW constructor.
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 (useCached)
{
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);
}
}
I think you have a bug in there. Should line 19 not be: if (useCached) ins$$anonymous$$d of if (System.IO.File.Exists(filePath)) ?
And loadFilepath is actually not needed since you can use filePath ins$$anonymous$$d, right?
yes, good catch. had fixed it on my local version but not here. was mainly just posting as inspiration, that a simple disk based cache that will do the job in a pinch is not too complex to make and that you don't necessarily need to rewrite all the parts of your app that use www to use it, since www will load from disk quite easily(and at least for android and win32 apps you don't need to write any special cases for the path either.)
also what I'm doing in my actual app is caching some of the the www's in memory.
it's too bad unity doesn't do this itself though, since it would really be a rather simple addition to unity and very useful. also www doesn't support retries and such, so mostly you will end up writing your own wrapper for www.
Thanks alot for your solution!
I had problem with function GetInt64HashCode
so I changed
filePath += "/" + GetInt64HashCode(url);
to
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(url);
filePath += "/" + System.Convert.ToBase64String(plainTextBytes);
Your answer
Follow this Question
Related Questions
www.texture textureType to GUI 0 Answers
Ios www.texture returns question mark 1 Answer
www.texture memory leak 1 Answer
Where is downloaded things from editor script being saved? 0 Answers