Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async/await with Awaitable instead of Coroutines and UniRx Observable #507

Open
achimmihca opened this issue Dec 7, 2024 · 0 comments
Open

Comments

@achimmihca
Copy link
Collaborator

Problem or question to be solved

Implementing concurrency in Unity has been a major pain because

  • async/await with Task of C# is good to write and read, but tricky to use because most of Unity API is only allowed on the main thread, such that one has to take care which thread is currently executing the code
  • the programming model with Unity's Coroutines is cumbersome (e.g. no method return values) and still executed on the main thread. So it does not provide a way to execute run long running tasks in the background (e.g. vocals isolation).

Due to these challenges, Reactive Programming via UniRx seemed a good alternative as it provides:

  • standardized interface (System.IObservable) and reactivex operators
  • easy way to switch from background thread to main thread and vice versa

However, working with UniRx has also been hard to write and maintain. For example, it has quite some learning curve, sometimes tricky semantics, is annoying to debug, and the syntax is not as smooth as async/await.

Suggested solution/s

By now, Unity supports a custom implementation of async/await via Awaitable (instead of a C# Task).
This seems to overcome the drawbacks of the previous approaches as it pairs the smooth syntax of async/await with good integration of Unity's API. Plus, it is easy to switch between background and main thread.

Your opinion on the topic

  • Prefer async/await with Awaitable in Unity over Coroutines and UniRx.
  • Consider to refactor some usage of Coroutines and UniRx to instead use async/await with Awaitable
    • Probably, AI tools can help with the refactoring (GitHub Copilot, JetBrains AI, ChatGPT, etc., choose your favorite)
    • Some concurrent code has grows really ugly and could use some love. Finally, Unity provides some tools that could make this fun again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant