using System.Collections; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using UnityEngine; using UnityEngine.Events; using UnityEngine.Networking; using XFramework; public static class OutSpriteLoader { public static Dictionary loadedSpriteDic = new Dictionary(); public static Dictionary loadedTextureDic = new Dictionary(); //public async Task LoadTexture(string path) //{ // //if (OutTextureManager.loadedTexture.ContainsKey(path)) // // loadedAction?.Invoke(OutTextureManager.loadedTexture[path]); // //else // //{ // var request = UnityWebRequestTexture.GetTexture(path); // await request; // Texture texture=null; // if (DownloadHandlerTexture.GetContent(request) != null) // { // texture = DownloadHandlerTexture.GetContent(request); // OutTextureManager.loadedTexture.Add(path, texture); // } // else // { // Debug.Log("图片加载失败,请确认名称是否正确"); // } // //loadedAction?.Invoke(texture); // //} //} /// /// 加载多张图片 /// /// /// public static async Task LoadTexture(string [] paths) { Texture2D[] textures = new Texture2D[paths.Length]; Dictionary> allTasksDic = new Dictionary>(); for (int i = 0; i < paths.Length; i++) { if (string.IsNullOrEmpty(paths[i])) { textures[i] = default(Texture2D); } else if (loadedTextureDic.ContainsKey(paths[i])) { textures[i] = loadedTextureDic[paths[i]]; } else if(!allTasksDic.ContainsKey(paths[i])) { allTasksDic.Add(paths[i], LoadAsyncTexture(paths[i])); } } await Task.WhenAll(allTasksDic.Values);//等待所有需要加载的图片完成加载 for (int i = 0; i < paths.Length; i++) { if (!string.IsNullOrEmpty(paths[i]) && allTasksDic.ContainsKey(paths[i])) textures[i] = allTasksDic[paths[i]].Result; } return textures; } /// /// 加载一张图片 /// /// /// public static async Task LoadTexture(string path) { if (string.IsNullOrEmpty(path)) return default(Texture2D); if (loadedTextureDic.ContainsKey(path)) return loadedTextureDic[path]; var task = LoadAsyncTexture(path); Task t = await Task.WhenAny(task); return t.Result; } public static async Task LoadSprites(string[] paths) { Sprite[] sprites = new Sprite[paths.Length]; Dictionary> allTasksDic = new Dictionary>(); for (int i = 0; i < paths.Length; i++) { if (string.IsNullOrEmpty(paths[i])) { sprites[i] = GetDefultSprite(); } else if (loadedSpriteDic.ContainsKey(paths[i])) { sprites[i] = loadedSpriteDic[paths[i]]; } else if (!allTasksDic.ContainsKey(paths[i])) { allTasksDic.Add(paths[i], LoadAsyncSprite(paths[i])); } } await Task.WhenAll(allTasksDic.Values);//等待所有需要加载的图片完成加载 for (int i = 0; i < paths.Length; i++) { if (!string.IsNullOrEmpty(paths[i]) && allTasksDic.ContainsKey(paths[i])) sprites[i] = allTasksDic[paths[i]].Result; } return sprites; } public static async Task LoadSprite(string path) { if (string.IsNullOrEmpty(path))//无效路径返回一个默认的图标 { return GetDefultSprite(); } if (loadedSpriteDic.ContainsKey(path)) return loadedSpriteDic[path]; var task = LoadAsyncSprite(path); Task t = await Task.WhenAny(task); return t.Result; } /// /// 获取一个默认的Sprite"?" 避免出现空图标 /// /// static Sprite GetDefultSprite() { if (!loadedSpriteDic.ContainsKey("defaultSprite")) { Texture2D tex = new Texture2D(2, 2); tex.LoadImage(null); Vector2 pivot = new Vector2(0.5f, 0.5f); Sprite sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), pivot, 100.0f); loadedSpriteDic.Add("defaultSprite", sprite); } return loadedSpriteDic["defaultSprite"]; } static async Task LoadAsyncSprite(string url) { //string path = Path.Combine(Application.streamingAssetsPath, url + end); var getRequest = UnityWebRequest.Get(url); await getRequest.SendWebRequest(); byte[] imgData = getRequest.downloadHandler.data; Texture2D tex = new Texture2D(2, 2); tex.LoadImage(imgData); Vector2 pivot = new Vector2(0.5f, 0.5f); Sprite sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), pivot, 100.0f); loadedSpriteDic.Add(url, sprite); return sprite; } static async Task LoadAsyncTexture(string url) { //string path = Path.Combine(Application.streamingAssetsPath, url + end); var getRequest = UnityWebRequestTexture.GetTexture(url); await getRequest.SendWebRequest(); Texture2D tex = DownloadHandlerTexture.GetContent(getRequest); loadedTextureDic.Add(url, tex); return tex; } }