Skip to content

Guide · Open source

How to use UniTask in Unity: async/await guide

Updated · written with AI from the project's own documentation and checked against our catalogue data

UniTask is an open-source C# library for Unity that gives you async/await with a lighter task type than the standard Task. This guide shows how to install it, await Unity's asynchronous operations and coroutines, replace common coroutine waits, and run several loads in parallel. By the end you will know the core patterns and when the library fits your project.

UniTaskCysharp/UniTask · ★ 11.3k · MIT

Quick start

  1. 1Install the package. Add UniTask to your project through the UPM git URL from the README, or import the unitypackage from the releases page.
  2. 2Add the namespace. Put using Cysharp.Threading.Tasks; at the top of your script so the extension methods are available.
  3. 3Write an async method. Declare a method returning UniTask or UniTask<T> with the async keyword.
  4. 4Await Unity operations. Await Resources.LoadAsync, scene loads or web requests directly instead of yielding coroutines.
  5. 5Replace coroutine waits. Swap yield return waits for UniTask.Delay, UniTask.DelayFrame, UniTask.Yield or UniTask.WaitUntil.
  6. 6Combine results. Use UniTask.WhenAll to run operations together and deconstruct the returned tuple.

Install UniTask in your Unity project

The README lists installation via the UPM package with a git reference, or via the asset package (UniTask.*.*.*.unitypackage) published on the project's releases page. The git method is shown below.

The library requires Unity after 2018.3. The lowest officially supported version is 2018.4.13f1.

  • Use the git URL install method from the README (UPM package).
  • Alternatively, download the unitypackage asset from the releases page.
git clone https://github.com/Cysharp/UniTask.git

Await Unity operations and coroutines

Add `using Cysharp.Threading.Tasks;` to bring in the extension methods. You can then await Unity's asynchronous objects such as AsyncOperation, ResourceRequest, AssetBundleRequest, UnityWebRequestAsyncOperation and IEnumerator coroutines.

Methods that return `UniTask<T>` are declared with `async`, and you return a value with `return`. Use `WithCancellation` or `ToUniTask` when you need a cancellation token or a progress callback.

  • await asyncOperation; for a direct await.
  • .WithCancellation(CancellationToken) to support cancellation.
  • .ToUniTask(IProgress, PlayerLoopTiming, CancellationToken) for progress and timing options.
async UniTask<string> LoadTextAsync() { var asset = await Resources.LoadAsync<TextAsset>("foo"); return (asset as TextAsset)?.text ?? throw new InvalidOperationException("Asset not found"); }

Replace coroutine waits with UniTask calls

The README maps many coroutine waits to simple UniTask methods. Each one is awaited inside an async method instead of yielded from a coroutine.

Frame-based and time-based waits are both covered. Note that `await` directly returns from the native timing of the PlayerLoop, while `WithCancellation` and `ToUniTask` return from the timing you specify.

  • UniTask.Delay(TimeSpan.FromSeconds(10), ignoreTimeScale: false) replaces yield return new WaitForSeconds.
  • UniTask.DelayFrame(100) waits a number of frames.
  • UniTask.Yield() replaces yield return null; UniTask.NextFrame() is also listed.
  • UniTask.WaitForFixedUpdate() replaces yield return new WaitForFixedUpdate().
  • UniTask.WaitUntil(() => condition) replaces yield return WaitUntil.
  • UniTask.WaitUntilValueChanged(this, x => x.isActive) is a special helper for WaitUntil.
await UniTask.Delay(TimeSpan.FromSeconds(10), ignoreTimeScale: false); await UniTask.Yield(PlayerLoopTiming.PreLateUpdate); await UniTask.WaitForFixedUpdate();

Run several async operations in parallel

Use `UniTask.WhenAll` to start several operations and wait for all of them. Unlike Task.WhenAll, the result is a value tuple, so you can deconstruct each result and receive different types together.

The README also shows a shorthand where a tuple of tasks can be awaited directly. Note that this is still on Unity's main loop; the README says UniTask runs on the PlayerLoop and does not use threads by default.

  • UniTask.WhenAll(task1, task2, task3) returns a tuple of results.
  • UniTask.WhenAny and UniTask.WhenEach are also mentioned in the README.
var (a, b, c) = await UniTask.WhenAll( LoadAsSprite("foo"), LoadAsSprite("bar"), LoadAsSprite("baz"));

Cancel and clean up tasks with lifetime tokens

The README shows that `GetCancellationTokenOnDestroy` on a MonoBehaviour ties a task to the lifetime of its GameObject. The README also notes that after Unity 2022.2 you can use `destroyCancellationToken` in a MonoBehaviour instead.

The README includes a TaskTracker window that can help track tasks and prevent memory leaks. The detailed cancellation and exception rules are in a separate README section that is not included in the source provided here.

  • Pass this.GetCancellationTokenOnDestroy() to WithCancellation.
  • On Unity 2022.2 or newer, the README suggests destroyCancellationToken.
var asset = await Resources.LoadAsync<TextAsset>("bar").WithCancellation(this.GetCancellationTokenOnDestroy());

What people use it for

  • Loading a Resources asset or scene asynchronously and awaiting it in one readable method.
  • Fetching several web requests in parallel with UniTask.WhenAll and reading each result.
  • Replacing a chain of coroutines that wait for frames, seconds or a condition.
  • Targeting WebGL, where the README says UniTask runs because it does not depend on threads.

Frequently asked questions

How do I install UniTask in Unity?

The README describes installing via the UPM package using a git URL, or by importing the UniTask unitypackage from the releases page. The git command given is git clone https://github.com/Cysharp/UniTask.git.

What Unity version do I need for UniTask?

The README says the library requires Unity after 2018.3, and the lowest officially supported version is 2018.4.13f1. It relies on C# 7.0 task-like custom async builders.

Is UniTask free to use?

The repository is licensed under the MIT License, as listed on this directory page. Check the LICENSE file in the repository for the full terms.

Does UniTask work on WebGL?

The README says UniTask runs completely on Unity's PlayerLoop, so it does not use threads and runs on WebGL and wasm.

Is UniTask faster than Task?

The README says Task is heavy and not matched to Unity's single-threaded model, and that UniTask achieves faster and lower-allocation async. It does not provide benchmark numbers in the source provided here.

What are alternatives to UniTask?

The comparable projects listed for this directory are Unity Awaitable, UniRx and the standard Task API from System.Threading.Tasks. UniTask also has a README section comparing it with Awaitable.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

UniTask suits Unity developers who already use coroutines and want async/await, especially when loading assets or web requests, or when targeting WebGL. It is a poor fit for projects not using Unity, for beginners new to both C# and async, or for code whose main goal is real multithreaded parallel work.

Install options & downloads