67 lines
2.2 KiB
C#
67 lines
2.2 KiB
C#
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.Networking;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
|
|||
|
public class ImageLoader : MonoBehaviour
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>
|
|||
|
private Dictionary<string, Sprite> imageCache = new Dictionary<string, Sprite>();
|
|||
|
//private async void Start()
|
|||
|
//{
|
|||
|
// GetComponent<Image>().sprite = await LoadImageAsync("https://fantasymonster-app.oss-cn-hangzhou.aliyuncs.com/goods/mall/c7860d8909194d479b6f27ccb922e863.png");
|
|||
|
//}
|
|||
|
// <20>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD>ͼƬ
|
|||
|
public async Task<Sprite> LoadImageAsync(string url)
|
|||
|
{
|
|||
|
// <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))
|
|||
|
{
|
|||
|
Debug.Log("Image found in cache.");
|
|||
|
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><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//await webRequest.SendWebRequest().ToTask();
|
|||
|
// <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
|
|||
|
{
|
|||
|
Debug.LogError("Failed to load image: " + webRequest.error);
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
// <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));
|
|||
|
}
|
|||
|
// <20><><EFBFBD>ز<EFBFBD><D8B2><EFBFBD>ʾͼƬ
|
|||
|
//public async void LoadAndDisplayImage(string url, Renderer renderer)
|
|||
|
//{
|
|||
|
// Texture2D texture = await LoadImageAsync(url);
|
|||
|
// if (texture != null)
|
|||
|
// {
|
|||
|
// renderer.material.mainTexture = texture;
|
|||
|
// }
|
|||
|
//}
|
|||
|
}
|