Best practices: Async vs. coroutines - Unite Copenhagen

Unity · 75K views · 2019 · 42:54

Open on YouTube

Game programming & enginesSoftware architecture & designtalk

Why Stack Overflow recommends it

Stack Overflow users link it when Unity questions turn on async/await versus coroutines, especially for UI flows, exception handling and how the two approaches differ.

Recommended by 3 people across 3 different months, 2019 to 2022.

What it is

A Unite Copenhagen 2019 conference talk by Johannes Ahvenniemi of Seriously Digital Entertainment on using C# async/await instead of coroutines in Unity. It works through one example, an asynchronous prompt popup, built both ways, and covers pitfalls such as hidden exceptions.

What people said

Recommends Alexsandr Krivobokov · 2020-06 · Stack Overflow · on “Async/await style for UI in Unity”

[this video] You can watch a few minutes of this video (start at 36:00). Speaker talks about user interface issues this pattern solves.

Recommends fibriZo raZiel · 2021-01 · Stack Overflow · on “how to use c# threads in unity3d for android platform?”

I'd also suggest taking a look at this article [LINK] to better understand the difference between I/O-Bound (e.g. reading a file) and CPU-Bound operations. Then, try to adopt the async/await pattern in Unity3D following these guidelines [this video] .

Recommends Felipe · 2022-04 · Stack Overflow · on “In Unity, when should I use coroutines versus subtracting Time.deltaTime in Update()?”

Very true about pointless opinion arguments. Since the time of this answer, I have found a very interesting talk [this video] from Unite 2019 that brings up the question of async vs coroutines after dotnet 4 was integrated [LINK] . Lots to learn from it.

Cites Everts · 2019-10 · Stack Overflow · on “No exception in Async method”

Back to this, as mentioned on this video [this video] at 9:20, exception can be hidden. I guess that's what I had. Special thanks to whoever downvoted my question, probably thinking I misused the feature while it was a Unity limitation.

Cites Daniel Loureiro · 2021-05 · Stack Overflow · on “How does StartCoroutine / yield return pattern really work in Unity?”

On Unity 2017+ , you can use the native C# async / await keywords for async code, but before that, C# had no native way to implement async code . Unity had to use a workaround for async code. They achieved this by exploiting the C# iterators , which was a popular async technique at the time. A look at C# Iterators Let's say you have this code: IEnumerable SomeNumbers() { yield return 3; yield return 5; yield return 8; } If you run it through a loop, calling as if was an array, you will get 3 5 8 : // Output: 3 5 8 foreach (int number in SomeNumbers()) { Console.Write(number); } If you are not familiar with iterators (most languages have them to implement lists and collections), they work as

Cites Wappenull · 2021-06 · Stack Overflow · on “How to detect empty IEnumerator function calls during unit testing?”

@MickyD Thanks for the tip, but for this practical case, I will have to work with coroutines for now. And a bit of side-topic, I believe it is not fully interchangeable. See [this video] (Async vs. coroutines - By Unity official)