提交 2179ddf7 编写于 作者: yqian1000's avatar yqian1000

优化图片加载

上级 c14f7cec
......@@ -37,7 +37,7 @@ public class Card
return _img;
}
}
Texture2D _img;
public Texture2D _img;
public Card(string series, string html, string imgUrl)
{
......
......@@ -120,7 +120,7 @@ public class Series
for (int i = 0; i < data.Count; i++)
{
Card card = new Card(data[i]);
//card.CheckImgFile();
card.CheckImgFile();
AddCard(card);
}
}
......
......@@ -8,6 +8,7 @@ using UnityEngine;
using UnityEngine.Networking;
using static Unity.VisualScripting.Member;
using DG.Tweening.Plugins.Core.PathCore;
using UnityEngine.Profiling;
public class DownlaodImage : SingletonMono<DownlaodImage>
{
......@@ -25,15 +26,17 @@ public class DownlaodImage : SingletonMono<DownlaodImage>
/// <returns></returns>
public Texture2D GetImage(Card card)
{
//Profiler.BeginSample("GetImage");
Texture2D texture2D = LoadImageInFile(card);
if (texture2D == null)
{
texture2D = (RebirthBG == null) ? LoadRebirthBG() : RebirthBG;
//DownloadQueue.Enqueue(card);
//if (!downloading) StartCoroutine(DownLoadQueue());
DownloadQueue.Enqueue(card);
//Logger.Log($"DownloadQueue.Enqueue>>{card.Name}>>{DownloadQueue.Count}");
if (!downloading) StartCoroutine(DownLoadQueue());
}
//Profiler.EndSample();
return texture2D;
}
/// <summary>
......@@ -146,23 +149,33 @@ public class DownlaodImage : SingletonMono<DownlaodImage>
downloading = true;
while (DownloadQueue.TryDequeue(out Card card))
{
//Logger.Log($"DownloadQueue:{DownloadQueue.Count}");
//尝试在Resource下加载
string path = "";
var resq = Resources.LoadAsync<Texture2D>(path);
while (!resq.isDone)
if (card._img != null) continue; //跳除重复加载
//load本地
Texture2D texture2D = LoadImageInFile(card);
if(texture2D)
{
yield return null;
card.Img = texture2D;
}
if (resq.asset)
else //loadResource
{
card.Img = resq.asset as Texture2D;
EventManager.TriggerEvent<string>("DownloadingImgComplete", card.Num);
string folder = card.Series.Replace("/", "");
string path = $"data/{folder}/Img/{card.Num.Replace("/", "_")}";
var resq = Resources.Load<Texture2D>(path);
if (resq)
{
card.Img = resq;
EventManager.TriggerEvent<string>("DownloadingImgComplete", card.Num);
}
else
{
//没有则下载
yield return StartCoroutine(DownloadingImg(card));
}
}
else
//没有则下载
yield return StartCoroutine(DownloadingImg(card));
yield return null;
}
......@@ -218,7 +231,7 @@ public class DownlaodImage : SingletonMono<DownlaodImage>
card.Img = texture;
SaveCard2Local(path, texture);
//Logger.Log($"完成下载图片{card.Num}:{path}");
EventManager.TriggerEvent<string>("DownloadingImgComplete", card.Num);
}
......
......@@ -36,7 +36,27 @@ public class UICardItem : InfiniteElem
dr_count.onValueChanged.AddListener(OnDrCount);
AddBtnClickListener(GetComponent<Button>(), OnClickSelf);
EventManager.AddListener<string>("DownloadingImgComplete", RefreshImg);
}
public void RefreshImg(string num)
{
if(num == card.Num)
{
if (card.Type == CardType.Rebirth)
{
rimg_card.gameObject.SetActive(false);
rimg_rebirth.gameObject.SetActive(true);
rimg_rebirth.texture = card.Img;
}
else
{
rimg_card.gameObject.SetActive(true);
rimg_rebirth.gameObject.SetActive(false);
rimg_card.texture = card.Img;
}
}
}
public void OnDrCount(int val)
......@@ -95,6 +115,7 @@ public class UICardItem : InfiniteElem
protected override void OnClose()
{
base.OnClose();
EventManager.RemoveListener<string>("DownloadingImgComplete", RefreshImg);
}
public override void OnSetData(object[] data = null)
......@@ -108,4 +129,8 @@ public class UICardItem : InfiniteElem
UICardsCtrl.Instance.OpenCardInfoView(new object[] { card });
}
private void OnDestroy()
{
EventManager.RemoveListener<string>("DownloadingImgComplete", RefreshImg);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册