Hacker News new | ask | show | jobs
by jayd16 1461 days ago
You can use async Tasks as long as you keep them on the main thread. They end up making a lot of garbage though.
1 comments

That's what ValueTasks are there for, no?
ValueTasks help but they can only be awaited once. The Unity runtime knows how to handles the same IEnumerator yielded by multiple parent routines.

Cancellation is another gotcha with Tasks. The main way to cancel tasks is to use exceptions as control flow. This causes cancellation to be very expensive. You can still use cancellation tokens and check for IsCancelled and finish your task cooperatively but then you're kind of doing thinks in an non-canononical way.

Unity also hasn't done the work to make cheap unity lifecycle delay calls such as Task wait for end of frame, (although they could). The work arounds are expensive.

Is that Unity-specific stuff? From my experience, cancellation in idiomatic C# is mostly done via tokens, not exceptions.
IIRC there's a little more work needed for unity, but Cysharp has you covered (https://github.com/Cysharp/UniTask)