MainThreadAwaitable Awaitable object that completes when switching to the main thread.
Resumes execution on the Unity main thread. Completes immediately when called from the main thread.
private async Awaitable<float> DoSomeHeavyComputationInBackgroundAsync(bool continueOnMainThread = true) { await Awaitable.BackgroundThreadAsync(); // Do some heavy math here float result = 42;
// By default, switch back to main thread: if(continueOnMainThread){ await Awaitable.MainThreadAsync(); } return result; }
public async Awaitable Start() { var computationResult = await DoSomeHeavyComputationInBackgroundAsync(); await SceneManager.LoadSceneAsync("my-scene"); }