Task for Unity makes threads easier!DocumentationForumChanges- Improved Async Scheduler integration
What is Task for Unity?- Provides a simple way to manage threads
- Unlocks the power of multi-thread CPUs
Why is Task for Unity useful?If you have long running
thread-safe operations you can optimize your game by putting them on a separate thread without writing complex thread management code.
FeaturesTask.Run()
Runs work on separate thread (pool)Task.ContinueWith()
Run work asynchronously when Task completesTask<T>.Result
Task<T> performs same operations as Task except it has a Result where T = any type (int, float, etc)Task.Exception
Gets exception from thread. Exceptions are usually tricky to handle in separate threads but Task.Exception provides an easy way to handle them!Status
Check task's status with:
- Status
- IsCanceled
- IsCompleted
- IsFaulted (Exception)
Task.WhenAll
Creates a task that completes when all specified tasks are completeTask.WhenAny
Creates a task that completes when any specified tasks are completeTask.Wait()
Waits for Task to completeTask.WaitAll()
Waits for all specified tasks to completeTask.WaitAny()
Waits for any specified tasks to completeThread Pool
Thread pools are important to avoid the cost of creating a new thread. But Task manages this so you don't have to!Want to run code on main, or spawn objects on a thread?
Async SchedulerWant to run for loops in Parallel?
Parallel for Unity