using System; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; using UnityEngine.Networking; namespace TapTap.Common.Internal.Utils { public class ImageUtils { private static Dictionary> cachedTextures = new Dictionary>(); public static async Task LoadImage(string url) { if (cachedTextures.TryGetValue(url, out WeakReference refTex) && refTex.TryGetTarget(out Texture tex)) { return tex; } else { Texture newTex = await FetchImage(url); cachedTextures[url] = new WeakReference(newTex); return newTex; } } public static async Task FetchImage(string url) { UnityWebRequest www = UnityWebRequestTexture.GetTexture(url); UnityWebRequestAsyncOperation operation = www.SendWebRequest(); while (!operation.isDone) { await Task.Delay(50); } if (www.isNetworkError || www.isHttpError) { throw new Exception("Fetch image error."); } else { return ((DownloadHandlerTexture)www.downloadHandler).texture; } } } }