1. アイデア: 画像をロードするすべてのリクエストがキューに追加され、ロードが完了するまで 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. 質問:
問題は比較的存在します。ロードしたデータがキューに残っているが、他のシーンにジャンプした場合はエラーとなります。
シーンにジャンプするたびに、キュー内のデータを確認して、isLoad=false を設定する必要があります。