Files
TaiWan/Assets/Roaming/Scripts/Loader/ImageLoader.cs

219 lines
6.8 KiB
C#
Raw Normal View History

2025-10-31 15:20:38 +08:00
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using DG.Tweening;
using static UnityEngine.Rendering.DebugUI;
/// <summary>
/// ͼƬ<CDBC><C6AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public class ImageLoader<T> : MonoBehaviour
{
public static Dictionary<string, Texture2D> dicTextures;
static List<string> removeKeys;
static Image progress;
static ImageLoader()
{
dicTextures = new Dictionary<string, Texture2D>();
removeKeys = new List<string>();
if(progress==null) progress = GameObject.Find("progress").GetComponent<Image>();
}
public static void RemoveTexture()
{
if (removeKeys.Count > 0)
{
if (dicTextures.ContainsKey(removeKeys[0]))
{
Destroy(dicTextures[removeKeys[0]]);
dicTextures.Remove(removeKeys[0]);
Resources.UnloadUnusedAssets();
}
Debug.Log("Removed:" + removeKeys[0]);
removeKeys.RemoveAt(0);
}
}
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
///// <summary>
///// <20><><EFBFBD>ⲿָ<E2B2BF><D6B8><EFBFBD>ļ<EFBFBD><C4BC>м<EFBFBD><D0BC><EFBFBD>ͼƬ
///// </summary>
///// <param name="imgFullName"><3E>ļ<EFBFBD>·<EFBFBD><C2B7></param>
///// <returns></returns>
//public static Texture2D LoadTextureByIO(string imgFullName)
//{
// FileStream fs = new FileStream(imgFullName, FileMode.Open, FileAccess.Read);
// fs.Seek(0, SeekOrigin.Begin);//<2F>α<EFBFBD><CEB1>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><D0BF><EFBFBD>
// byte[] bytes = new byte[fs.Length];//<2F><><EFBFBD><EFBFBD><EFBFBD>ֽڣ<D6BD><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6B4A2>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ͼƬ<CDBC>ֽ<EFBFBD>
// try
// {
// fs.Read(bytes, 0, bytes.Length);//<2F><>ʼ<EFBFBD><CABC>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>trycatch<63><68><EFBFBD><EFBFBD><E4A3AC>ֹ<EFBFBD><D6B9>ȡʧ<C8A1>ܱ<EFBFBD><DCB1><EFBFBD>
// }
// catch (System.Exception e)
// {
// Debug.Log(e);
// }
// fs.Close();//<2F>мǹر<C7B9>
// int width = 2048;//ͼƬ<CDBC>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E1B5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>
// int height = 2048;//ͼƬ<CDBC>ĸߣ<C4B8><DFA3><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2BBB0>pico<63><6F><EFBFBD>صĿ<D8B5><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD>4k<34><6B>4k<34><6B>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><ECB3A3><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>pico<63><6F>ʱ<EFBFBD><CAB1>ӦΪ<D3A6><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˴<EFBFBD><CBB4><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>6000*3600<30><30><EFBFBD><EFBFBD><EFBFBD>³<EFBFBD><C2B3><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD>ͺ<EFBFBD><CDBA><EFBFBD><EFBFBD>ˡ<EFBFBD><CBA1><EFBFBD><EFBFBD><EFBFBD>
// Texture2D texture = new Texture2D(width, height);
// if (texture.LoadImage(bytes))
// {
// //print("ͼƬ<CDBC><C6AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ");
// return texture;
// }
// else
// {
// //print("ͼƬ<CDBC><C6AC>δ<EFBFBD><CEB4><EFBFBD><EFBFBD>");
// return null;
// }
//}
#endregion
/// <summary>
/// Э<>̼<EFBFBD><CCBC><EFBFBD>ͼ<EFBFBD><CDBC>
/// </summary>
/// <param name="imgFullName">ͼ<><CDBC><EFBFBD><EFBFBD></param>
/// <param name="mat"><3E><><EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD></param>
public static IEnumerator LoadImage(string imgFullName, T mat, ImageLoadedEventHandler<T> ImageLoaded = null)
{
// Debug.Log(imgFullName);
//progress.gameObject.SetActive(true);
DOTween.KillAll();
progress.fillAmount = 0;
Texture2D tex = null;
if (dicTextures.ContainsKey(imgFullName))
{
tex = dicTextures[imgFullName];
}
else
{
UnityWebRequest www = UnityWebRequestTexture.GetTexture(imgFullName);
yield return www.SendWebRequest();
// Debug.Log($"<22><><EFBFBD><EFBFBD>{imgFullName}<7D><><EFBFBD>ȣ<EFBFBD>{www.downloadProgress}");
//while (!www.isDone)
//{
// yield return null;
// //GameObject.Find("progress").GetComponent<LoadProgress>().loadWebRequest = www;
// DOTween.To(() => progress.fillAmount, x => progress.fillAmount = x, www.downloadProgress, Time.deltaTime).OnComplete(() =>
// {
// if (progress.fillAmount >= 0.99)
// {
// DOTween.KillAll();
// progress.fillAmount = 0;
// }
// });
// //Tweener tweener = DOTween.To(() => progress.fillAmount, x=> progress.fillAmount, www.downloadProgress,Time.deltaTime);
// //progress.fillAmount = www.downloadProgress;
// //progress.fillAmount = Mathf.Lerp(progress.fillAmount, www.downloadProgress,Time.deltaTime);
// Debug.Log(www.downloadProgress);
//}
//DOTween.KillAll(); progress.fillAmount = 0;
//DOTween.To(() => progress.fillAmount, x => progress.fillAmount = x, 0, 0);
if (www.result == UnityWebRequest.Result.ConnectionError)
{
Debug.Log(www.error);
}
else
{
//progress.gameObject.SetActive(false);
try
{
tex = ((DownloadHandlerTexture)www.downloadHandler).texture;
if (!dicTextures.ContainsKey(imgFullName))
{
dicTextures.Add(imgFullName, tex);
}
}
catch
{
Debug.Log("ͼƬ<CDBC><C6AC><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>" + imgFullName);
}
}
www.Dispose();
}
if (ImageLoaded == null)
{
ImageLoaded = DefaultImageLoaded;
}
ImageLoaded(tex, mat);
}
public static void DefaultImageLoaded(Texture2D tex, T mat)
{
if (mat != null)
{
if (mat is Material)
{
Material matTmp = mat as Material;
// <20><><EFBFBD>²<EFBFBD><C2B2><EFBFBD>
matTmp.mainTexture = tex;
if (matTmp.IsKeywordEnabled("_EMISSION"))
{
matTmp.SetTexture("_EmissionMap", tex);
}
}
else if (mat is RawImage)
{
RawImage matTmp = mat as RawImage;
matTmp.texture = tex;
}
else if (mat is Image)
{
Image matTmp = mat as Image;
matTmp.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
}
// ClearImage();
}
}
public static void RemoveImage(string imageName)
{
// removeKeys.Add(imageName);
// Debug.Log("Remove:" + imageName);
if (dicTextures.ContainsKey(imageName))
{
Destroy(dicTextures[imageName]);
dicTextures.Remove(imageName);
}
// Resources.UnloadUnusedAssets();
}
public static void ClearImage()
{
foreach (string key in dicTextures.Keys)
{
Destroy(dicTextures[key]);
}
dicTextures.Clear();
}
//<2F><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>Э<EFBFBD><D0AD>
IEnumerator ToDestoryThis(Texture2D thisSprite)
{
yield return new WaitForSeconds(0.1f);
//Destroy(thisSprite);
Resources.UnloadUnusedAssets();//ж<><D0B6>δռ<CEB4>õ<EFBFBD>asset<65><74>Դ
System.GC.Collect();//<2F><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
}
}
/// <summary>
/// ͼƬ<CDBC><C6AC><EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD>¼<EFBFBD>ί<EFBFBD><CEAF>
/// </summary>
/// <param name="tex">ͼƬ<CDBC><C6AC><EFBFBD><EFBFBD></param>
public delegate void ImageLoadedEventHandler<T>(Texture2D tex, T mat);