更新拉取图片的缓存代码
This commit is contained in:
1 parent
696fd256a8
commit
6645310601
22 files changed
+149
-119
No files matched your search
@@ -205,16 +205,44 @@ namespace Assets.Scripts
|
||||
/*删除父亲节点下的所有孩子*/
|
||||
|
||||
/*显示网络图片*/
|
||||
private static Dictionary<string, Texture> propTextureCache = new Dictionary<string, Texture>();
|
||||
public delegate Coroutine StartCoroutine(IEnumerator routine);
|
||||
public static void DisplayImage(StartCoroutine startCoroutine,RawImage img, string url, Action<Texture> action=null)
|
||||
public static void DisplayImage(RawImage img, string url, bool cache = false)
|
||||
{
|
||||
startCoroutine(DownloadImage(img, url,action));
|
||||
//可以考虑缓存到硬盘里
|
||||
//Texture2D tex = null;
|
||||
//byte[] fileData;
|
||||
//
|
||||
//if (File.Exists(filePath))
|
||||
//{
|
||||
// fileData = File.ReadAllBytes(filePath);
|
||||
// tex = new Texture2D(2, 2);
|
||||
// tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
|
||||
//}
|
||||
if (cache)
|
||||
{
|
||||
if (propTextureCache.ContainsKey(url))
|
||||
{
|
||||
img.texture = propTextureCache[url];
|
||||
return;
|
||||
}
|
||||
}
|
||||
UIManager.Instance.StartCoroutine(DownloadImage(img, url,(texture)=> {
|
||||
if (cache)
|
||||
{
|
||||
propTextureCache.Add(url, texture);
|
||||
}
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
public static void DisplayImageAysnc(StartCoroutine startCoroutine, RawImage img, string url,Action<string> action)
|
||||
{
|
||||
startCoroutine(DownloadImageCallBack(img, url, action));
|
||||
}
|
||||
|
||||
|
||||
|
||||
static IEnumerator DownloadImage(RawImage img,string MediaUrl, Action<Texture> action = null)
|
||||
{
|
||||
UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
|
||||
|
||||
Reference in new issue
Block a user