2024-12-04 02:09:23 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
using System.Threading.Tasks;
|
2024-12-04 10:26:19 +08:00
|
|
|
|
using System;
|
2024-12-04 02:09:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class ImageLoader : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>
|
|
|
|
|
private Dictionary<string, Sprite> imageCache = new Dictionary<string, Sprite>();
|
2024-12-04 10:26:19 +08:00
|
|
|
|
public Sprite sprite;
|
2024-12-04 02:09:23 +08:00
|
|
|
|
// <20>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD>ͼƬ
|
|
|
|
|
public async Task<Sprite> LoadImageAsync(string url)
|
|
|
|
|
{
|
2024-12-04 10:26:19 +08:00
|
|
|
|
if (!IsValidUrl(url))
|
|
|
|
|
{
|
|
|
|
|
return sprite;
|
|
|
|
|
}
|
2024-12-04 02:09:23 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD>ͼƬ<CDBC><C6AC><EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7>ػ<EFBFBD><D8BB><EFBFBD><EFBFBD><EFBFBD>ͼƬ
|
|
|
|
|
if (imageCache.ContainsKey(url))
|
|
|
|
|
{
|
|
|
|
|
return imageCache[url];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB>ͼƬ<CDBC><C6AC><EFBFBD><EFBFBD><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD>
|
|
|
|
|
using (UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(url))
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<F3B2A2B5><C8B4><EFBFBD>Ӧ
|
|
|
|
|
var operation = webRequest.SendWebRequest();
|
|
|
|
|
while (!operation.isDone)
|
|
|
|
|
await Task.Yield(); // <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD>ʹ<EFBFBD><CAB9>await<69>Է<EFBFBD><D4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD>
|
|
|
|
|
if (webRequest.result == UnityWebRequest.Result.Success)
|
|
|
|
|
{
|
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
Texture2D texture = ((DownloadHandlerTexture)webRequest.downloadHandler).texture;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
imageCache[url] = TextureToSprite(texture);
|
|
|
|
|
return imageCache[url];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-12-04 10:26:19 +08:00
|
|
|
|
//Debug.LogError("Failed to load image: " + webRequest.error);
|
|
|
|
|
return sprite;
|
2024-12-04 02:09:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// <20><>Texture2Dת<44><D7AA>ΪSprite
|
|
|
|
|
Sprite TextureToSprite(Texture2D texture)
|
|
|
|
|
{
|
|
|
|
|
// ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ijߴ紴<DFB4><E7B4B4>һ<EFBFBD><D2BB>Sprite
|
|
|
|
|
return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
|
|
|
|
|
}
|
2024-12-04 10:26:19 +08:00
|
|
|
|
|
|
|
|
|
public static bool IsValidUrl(string url)
|
|
|
|
|
{
|
|
|
|
|
Uri uriResult;
|
|
|
|
|
return Uri.TryCreate(url, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
|
|
|
|
|
}
|
2024-12-04 02:09:23 +08:00
|
|
|
|
}
|