Newer version of C# has async
/await
. But in Unity there is only yield
. How do I implement a method where I can yield
in parallel?
Similar to Promise.all([])
in Javascript, we don't care which one finishes first, we only care when they are all done.
To give more context, imagine you are designing a procedural terrain generator that generate in chunks; and you have already setup each chunk to generate using a ThreadPool
, and then provide an API that returns an IEnumerator
:
IEnumerator GenerateChunk() {
// procedural generation
// queue itself onto a ThreadPool
// when done, yield
}
IEnumerator GenerateChunks() {
for (int i = 0; i < chunks.Length; i++) {
yield return chunks[i].GenerateChunk();
}
}
void GenerateMap() {
StartCoroutine(GenerateChunks());
}
Can we do something like yield IEnumerator[]
?
UPDATE: I am not sure I have expressed myself clearly. Basically I want to kick start all GenerateChunk
at once, and allow them to finish as fast as possible, instead of yielding one after another.
Is my code already doing that, or do I need anything else?