Cache UI Sprite Image

UGUIÀÇ ½ºÇÁ¶óÀÌÆ® À̹ÌÁö¸¦ À¥ÀÇ À̹ÌÁö·Î ¹Ù¤Ô¤¢¤Í¤¤ ÈÄ Ä³½ÌÇغ»´Ù.

1. À¥ÀÇ À̹ÌÁö·Î UI  ½ºÇÁ¶óÀÌÆ® À̹ÌÁö ¹Ù²Ù±â

MyTest.cs ÆÄÀÏÀ» Image ÄÄÆ÷´ÂÆ®°¡ ÀÖ´Â °ÔÀÓ¿ÀºêÁ§Æ®ÀÇ ÄÄÆ÷³ÍÆ®·Î Ãß°¡ÇÑ´Ù.

MyTest.cs ÆÄÀÏ
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class MyTest : MonoBehaviour
{
    WWWCache wwwCache = new WWWCache();
    void Start()
    {
        StartCoroutine(LoadImage("image/default01"));
    }

    IEnumerator LoadImage(string textureName)
    {
        Image image = this.GetComponent<Image>();
        if (image == null)
            yield break;

        WWW www = new WWW("http://www.sisakorea.kr/imgdata/sisakorea_kr/201308/2013082122101407.jpg");
        yield return www;

        if (string.IsNullOrEmpty(www.error) == false)
        {
            image.sprite = Resources.Load<Sprite>(textureName);
            yield break;
        }

        if (www.isDone)
        {
            //Texture2D texture = new Texture2D(www.texture.width, www.texture.height, www.texture.format, false);
            Texture2D texture = new Texture2D(512, 512, TextureFormat.ETC_RGB4, false);
            www.LoadImageIntoTexture(texture);

            Rect rec = new Rect(0, 0, texture.width, texture.height);
            image.sprite = Sprite.Create(texture, rec, new Vector2(0.5f, 0.5f), 100);
        }

        www.Dispose();
        www = null;
    }
}

À̹ÌÁö ÄÄÆ÷³ÍÆ®ÀÇ ¹Þ¾Æ¼­ ½ºÇÁ¶óÀÌÆ®¸¦ ¹Ù²Ù´Â ÄÚµå´Â ´ÙÀ½°ú °°´Ù.

//Texture2D texture = new Texture2D(www.texture.width, www.texture.height, www.texture.format, false);
Texture2D texture = new Texture2D(512, 512, TextureFormat.ETC_RGB4, false);
www.LoadImageIntoTexture(texture);

Rect rec = new Rect(0, 0, texture.width, texture.height);
image.sprite = Sprite.Create(texture, rec, new Vector2(0.5f, 0.5f), 100);

ÅؽºÃĸ¦ ¿øº» ±×´ë·Î Ãâ·ÂÇÒ¼ö ÀÖÁö¸¸ À̹ÌÁö Å©±â¿Í Æ÷¸ËÀ» º¯°æµµ °¡´ÉÇÏ´Ù.

2. À̹ÌÁö ij½ÌÇϱâ

https://gist.github.com/asus4/2301758  ¼Ò½º¸¦ ÇÑÁÙ ¼öÁ¤ÇÏ¿´´Ù.
¿¡µðÅÍ¿Í ¾Èµå·ÎÀ̵忡¼­ Å×½ºÆ® ÇÏ¿´´Ù.

WWWCahche.cs ÆÄÀÏ
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) {
#if !UNITY_IOS
        File.WriteAllBytes(urlToCachePath(www.url), www.bytes);
#endif
    }
   
    public string CacheUrl(string url) {
        string path = urlToCachePath(url);
        if(File.Exists(path)) {
            return "file:///" + path;
        }
        Debug.LogWarning("dont have cache url:"+url);
        return "";
    }
}

IOS´Â Documents Æú´õ¿¡ ÇÊ¿ä¾ø´Â ¾²±â¸¦ ÇÏ¸é °Ë¼ö¸¦ Åë°ú ¸øÇÑ´Ù´Â Á¤º¸°¡ À־ ÆÄÀÏ ¾²±â¸¦ ¾ÈÇß´Ù. (È®½ÇÇÑ Á¤º¸ÀÎÁö ¸ð¸£°Ú´Ù)

·ÎÄÿ¡¼­ ÆÄÀÏÀ» ÀÐÀ»¶§ "file:///"·Î Àоî¾ß Á¦´ë·Î ÀÐÀ»¼ö ÀÖ´Ù.

MyTest.cs ÆÄÀÏ
using UnityEngine;
using System.Collections;
using UnityEngine.UI;


public class MyTest : MonoBehaviour
{
    WWWCache wwwCache = new WWWCache();
    void Start()
    {
        StartCoroutine(LoadCacheImage());
    }

    IEnumerator LoadCacheImage()
    {
        string url = "http://www.sisakorea.kr/imgdata/sisakorea_kr/201308/2013082122101407.jpg";

        Image image = this.GetComponent<Image>();
        if (image == null)
            yield break;

        string cacheUrl = wwwCache.CacheUrl(url);
        if (string.IsNullOrEmpty(cacheUrl) == false)
            url = cacheUrl;

        WWW www = new WWW(url);
        yield return www;

        if (string.IsNullOrEmpty(www.error) == false)
            yield break;

        if (www.isDone)
        {
            if (string.IsNullOrEmpty(cacheUrl) == true)
                wwwCache.WriteCache(www);

            Debug.Log("<<< Cache image Show!!!");
            Texture2D texture = new Texture2D(512, 512, TextureFormat.ETC_RGB4, false);
            www.LoadImageIntoTexture(texture);

            Rect rec = new Rect(0, 0, texture.width, texture.height);
            image.sprite = Sprite.Create(texture, rec, new Vector2(0.5f, 0.5f), 100);
        }

        www.Dispose();
        www = null;
    }
}

cacheUrlÀÌ ÀÖÀ¸¸é ·ÎÄà ÆÄÀϸíÀ» www·Î ³Ñ°ÜÁÖ°í ¾øÀ¸¸é wwwCache.WriteCache·Î ÀúÀåÇÑ´Ù.

Âü°í)
https://gist.github.com/asus4/2301758
http://gods2000.tistory.com/m/entry/Unity3D-¿ÜºÎ-À̹ÌÁö-´Ù¿î·Îµå-ÈÄ-png-ÆÄÀÏ-ÀúÀå-ÇÏ´Â-¹æ¹ý
https://docs.unity3d.com/ScriptReference/Texture2D.LoadImage.html
http://iw90.tistory.com/74