UniTask is a library that brings fast and powerful
await support to Unity. Its API is nearly identical to that of .NET's standard Task-based asynchronous pattern, but optimized for Unity's player loop. It also adds
await support for Unity's built-in asynchronous operations such as
If you have the
com.cysharp.unitask package installed in your project, the following features will automatically be enabled.
Entry points can use the
IAsyncStartable phase, which returns a
public class FooController : IAsyncStartable
public async UniTask StartAsync(CancellationToken cancellation)
Once a class that implements
IAsyncStartable is registered its
StartAsync method will be executed automatically, similarly to
For more information about
RegisterEntryPoint, see here.
Currently, the only
async interface provided by VContainer is
IAsyncStartable. If you need to execute
async code in different
PlayerLoop phases, UniTask itself provides ways to control the execution timing within the method.
If you want to schedule the process at different times, you can use UniTask's
Note that all
StartAsync calls will be scheduled for simultaneous execution on the main thread (unless otherwise specified within each implementation), and future
PlayerLoop phases will not wait for them to complete.
PlayerLoop documentation for more information.
asyncmethods fully support
- You can use the
- Alternatively, if you want to register a separate error handler for each LifetimeScope, use
builder.RegisterEntryPointExceptionHandler(ex => ..)
See the Entry point section for more information.
StartAsync is provided with a
CancellationToken that is canceled when the
LifetimeScope that registered it is destroyed.
Async Asset Loading
UniTask offers the ability to load assets and scenes asynchronously. This is helpful when used with one of VContainer's
var extraAsset = await Addressables.LoadAssetAsync<ExtraAsset>(key);
using (LifetimeScope.Enqueue(builder => builder.RegisterInstance(extraAsset))
See the Scoping section for more information.