1、思路:加載圖片的請求都加到隊列中,然后一個加載完一個再去加載下一個,直到加載完。
List<Action> _loadAction = new List<Action>();
public void LoadRaw(string url, Image image)
{
if(string.IsNullOrEmpty(url)) return;
_loadAction.Add(()=>{ StartCoroutine(StartLoadImage(url,image))});
}
IEnumerator StartLoadImage(string url,Image image)
{
using(UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url))
{
yield return uwr.SendWebRequest();
if(uwr.isHttpError || uwr.isNetworkError) Debug.Log(uwr.error);
else
{
try
{
Texture2D texture2d = new Texture2D(1,1);
texture2d = DownloadHandlerTexture.GetContent(uwr);
image.sprite = Sprite.Create(texture2d,new Rect(0,0,texture2d.width,texture2d.height),Vector2.zero);
Resources.UnloadUnusedAssets();
_loadAction.RemoveAt(0);
isLoad = false;
}
catch(Exception ex)
{
}
}
}
}
bool isLoad = false;
Void Update()
{
if(!isLoad && _loadAction.Count != 0)
{
isLoad = true;
Action action = _loadAction[0];
action.Invoke();
}
}
2、問題:
? ? ? ? 問題是相對也存在的。當加載的數(shù)據(jù)還在隊列中,但是已經(jīng)跳轉到其它的場景,則會出現(xiàn)報錯的問題。文章來源:http://www.zghlxwxcb.cn/news/detail-564288.html
? ? ? ? 每次跳轉場景的時候,需要把隊列中的數(shù)據(jù)情況,然后isLoad=false;文章來源地址http://www.zghlxwxcb.cn/news/detail-564288.html
到了這里,關于Unity隊列加載圖片,解決大量同時加載資源卡頓問題與思路的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!