两个列表进骑行释放内存

This commit is contained in:
Caiyanpeng committed 2021-11-16 18:41:18 +08:00
1 parent 3295314b7e
commit 6a4390f1b9
7 files changed
+88 -32

No files matched your search

+36 -8
View File
@@ -309,7 +309,7 @@ namespace Assets.Scripts
/*显示网络图片*/
public static Dictionary<string, Texture> propTextureCache = new Dictionary<string, Texture>();
public delegate Coroutine StartCoroutine(IEnumerator routine);
public static void DisplayImage(RawImage img, string url, bool cache = false)
public static void DisplayImage(RawImage img, string url, bool cache = false,Dictionary<string,Texture> caches = null)
{
if (string.IsNullOrEmpty(url))
return;
@@ -332,12 +332,38 @@ namespace Assets.Scripts
}
}
UIManager.Instance.StartCoroutine(DownloadImage(img, url,(texture)=> {
if (cache && !propTextureCache.ContainsKey(url))
{
propTextureCache.Add(url, texture);
}
//if (cache && !propTextureCache.ContainsKey(url))
//{
// propTextureCache.Add(url, texture);
//}
}));
}
/// <summary>
/// 使用临时字典存放图片,切换页面后清除
/// </summary>
/// <param name="img"></param>
/// <param name="url"></param>
/// <param name="caches"></param>
public static void DisplayImageTempDict(RawImage img, string url, Dictionary<string, Texture> caches = null)
{
if (string.IsNullOrEmpty(url))
return;
if (caches!=null)
{
if (caches.ContainsKey(url))
{
img.texture = caches[url];
return;
}
}
UIManager.Instance.StartCoroutine(DownloadImage(img, url, (texture) => {
if (caches != null && !caches.ContainsKey(url))
{
caches.Add(url, texture);
}
}));
}
public static void DisplayImageAysnc(StartCoroutine startCoroutine, RawImage img, string url,Action<string> action)
@@ -349,13 +375,15 @@ namespace Assets.Scripts
static IEnumerator DownloadImage(RawImage img,string MediaUrl, Action<Texture> action = null)
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl,true);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
Debug.Log(request.error);
else if(img!=null)
{
img.texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
var _t = ((DownloadHandlerTexture)request.downloadHandler).texture;
_t.name = MediaUrl;
img.texture = _t;
if (action != null)
{
action.Invoke(img.texture);
@@ -365,7 +393,7 @@ namespace Assets.Scripts
static IEnumerator DownloadImageCallBack(RawImage img, string MediaUrl,Action<string> action)
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl, true);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
Debug.Log(request.error);