等待所有请求继续 [英] Wait for all requests to continue
问题描述
请,此代码可以异步完成吗?我遇到了问题,因为所有请求都必须在代码可以继续之前终止,我看到了如何等待一个孤立的请求,但是后来我无法使用循环,如果有人可以的话,我已经尝试解决了一段时间.帮我,那太好了.
Please, can this code be done async? I have problems because all requests must be terminated before the code can continue, I saw something about how wait an isolated request, but then i couldn't use the loop, I've been trying to solve this for a while, if anyone can help me it would e great.
StartCoroutine(GetTexture());
IEnumerator GetTexture() {
for (int i = 0; i < 10; i++)
{
string url = string.Format("https://galinha.webhost.com/img/{0}.jpg", words[i]);
UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError) {
selectionSprites.Add(sprites[i]);
}
else {
Texture2D myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
Sprite spriteFromWeb = Sprite.Create(myTexture, new Rect(0, 0, myTexture.width, myTexture.height), new Vector2(0, 0));
selectionSprites.Add(spriteFromWeb);
}
}//for
推荐答案
此代码可以异步完成吗?
can this code be done async?
请注意, async
是一个关键字,这里有点误导.当然,您可以执行Web请求等操作,但由于大多数Unity API不能在线程上使用,因此代码将完全不同.
note that async
is a keyword and a bit misleading here. Ofcourse you can do web requests etc async
but the code would be completely different since most of the Unity API can't be used on threads...
协程从不async
,但仍在主线程中运行,就像它们是临时的Update
方法.实际上,MoveNext
对协程的调用是在Update
之后立即进行的.
Coroutines are never async
but still run in the main thread like if they were temporary Update
methods. In fact the MoveNext
call for Coroutines is done right after the Update
.
所以我想您的意思是
可以并行等待这些请求吗?
can these requests be awaited parallel?
最理想的解决方案是,我可以想象您无需太多更改代码结构的地方就是存储所有 UnityWebRequestAsyncOperation
s( SendWebRequest
排列成一个数组,然后yield
直到所有它们都是 isDone
例如
Esiest solution I can imagine where you wouldn't have to change your code structure too much would be to store all UnityWebRequestAsyncOperation
s (what is returned by SendWebRequest
in an array and yield
until all of them are isDone
like e.g.
using System.Linq;
...
IEnumerator GetTexture()
{
var requests = new UnityWebRequestAsyncOperation[10];
// Start all requests
for (var i = 0; i < 10; i++)
{
var url = string.Format("https://galinha.webhost.com/img/{0}.jpg", words[i]);
var www = UnityWebRequestTexture.GetTexture(url);
// starts the request but doesn't wait for it for now
requests[i] = www.SendWebRequest();
}
// Now wait for all requests parallel
yield return new WaitUntil(() => AllRequestsDone(requests));
// Now evaluate all results
HandleAllRequestsWhenFinished(requests);
}
private void HandleAllRequestsWhenFinished(UnityWebRequestAsyncOperation[] requests)
{
for(var i = 0; i < 10; i++)
{
var www = requests[i].webRequest;
if(www.isNetworkError || www.isHttpError)
{
selectionSprites.Add(sprites[i]);
}
else
{
Texture2D myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
Sprite spriteFromWeb = Sprite.Create(myTexture, new Rect(0, 0, myTexture.width, myTexture.height), new Vector2(0, 0));
selectionSprites.Add(spriteFromWeb);
}
}
}
private bool AllRequestsDone(UnityWebRequestAsyncOperation[] requests)
{
// A little Linq magic
// returns true if All requests are done
return requests.All(r => r.isDone);
// Ofcourse you could do the same using a loop
//foreach(var r in requests)
//{
// if(!r.isDone) return false;
//}
//return true;
}
出于完整性考虑: WaitUntil
和
For completeness: WaitUntil
and Linq All
这篇关于等待所有请求继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!