I want to cache images in unity for android platform. I use WWW to downloading images but it will download them again each time. I searched in unity documentation and web and found nothing useful. Any help will be appreciated.
You could do one of two things
Asset bundles are an inbuilt feature, and it's really easy to integrate and use. Further, once any asset in downloaded with an AssetBundle, it's automatically cached as well (by default), and the next time, you won't need to re-download.
This method is more convoluted, but if you don't have access to Asset Bundles (It's a Pro-only feature in 4.x) for any reason, this is one of the only ways to do it.
Partial example of Method #2
public class TestMyDownload : Monobehaviour {
public string url = "http://www.example.com/bar.png";
IEnumerator Start () {
WWW www = new WWW(url);
yield return www;
if(www.bytes != null) {
System.IO.File.WriteAllBytes(Application.persistentDataPath + "/myfile.png", www.bytes);
Debug.Log("Writing Success");
}
}
}
EDIT : Just an FYI, method two can handle ALL types of data, not just images. If you're 100% sure you need only images, you can also access WWW.image
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With