Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.IO;
- public class TextureWWWCacher : MonoBehaviour {
- WWW web;
- public string imgUrl;
- public string cacheFile = "superbanner.png";
- public Texture2D resultTexture;
- string bannerCache_key = "nt_current_banner_cache";
- string imageFile = "";
- // Use this for initialization
- public IEnumerator fetch () {
- yield return fetch(imgUrl,-1,-1);
- }
- //this one uses bannerName instead of url to see if we have cached it
- public IEnumerator fetch (string bannerName,int resX,int resY) {
- imageFile = Application.persistentDataPath + "/supperbanner.png";
- resultTexture = new Texture2D(0,0);
- string hasCachedKey = PlayerPrefs.GetString(bannerCache_key,"");
- if( bannerName != hasCachedKey ){
- Debug.Log("downloading image file " + imgUrl);
- //we should download this new version..
- WWW web = new WWW(imgUrl);
- yield return web;
- if(web.error == null)
- {
- /*if(resX>0){
- web.texture.Resize(resX,resY);
- web.texture.Apply();
- }*/
- byte[] image = web.texture.EncodeToPNG();
- File.WriteAllBytes(imageFile, image);
- PlayerPrefs.SetString(bannerCache_key,bannerName);
- }
- else{
- Debug.Log("error downloading image file:" + web.error);
- resultTexture = null;
- }
- }
- else{
- Debug.Log("loading directly from cache");
- }
- if(resultTexture!= null){
- byte[] loadedImage = File.ReadAllBytes(imageFile);
- resultTexture.LoadImage(loadedImage);
- /*resultTexture.Resize(512,256);
- resultTexture.Apply();//*/
- }
- }
- // Update is called once per frame
- void Update () {
- }
- }
Add Comment
Please, Sign In to add comment