Skip to main content

Async Programming

Async programming allows code to be executed concurrently or in parallel, improving performance and responsiveness by running tasks in the background without blocking the main thread. In game development, this is crucial for maintaining a smooth user experience, as it ensures that expensive tasks like loading assets or controlling complex game logic don't freeze or slow down the game.

In Unity, we primarily use three tools for async programming:

  • Coroutines (Unity's built-in solution)
  • Async/Await (a more modern C# approach)
  • UniTask (a third-party library that extends async/await for performance improvements in Unity)

These tools allow developers to organize and sequence asynchronous tasks, such as showing UI panels in a specific order, loading large assets without interrupting gameplay, or controlling game logic flow.

Why Use Async Programming in Games?

Users expect quick responses, smooth gameplay, and seamless transitions between game states. Async programming enables us to deliver this experience by:

  1. Preventing Frame Freezes: Operations like loading assets, saving progress, or connecting to a server can take time. Running these tasks on the main thread would result in frame drops or freezing the game. With async programming, these tasks run in the background without blocking the game loop.

  2. Smoother UI Transitions: When showing menus, popups, or game results, we want to present them in a specific order. Async programming helps by allowing us to wait for animations to finish or data to load before continuing to the next step.

  3. Efficient Use of Resources: Background operations such as loading assets while the player is still interacting with the game allow us to optimize performance and resource usage. This ensures smooth gameplay, even when working with limited memory or CPU.

  4. Responsive Game Logic: Complex game logic, like waiting for player input, animations to finish, or event-driven actions, can be efficiently controlled using async programming patterns. These patterns ensure that game flow remains natural without unnecessary waiting or blocking.

Common Use Cases in Unity Games

  • Loading Assets in the Background: We load large assets (textures, audio, prefabs) without pausing the game. For example, we may load the next level while the player is still on the current level, ensuring seamless transitions.

  • Sequencing UI Panels: We control the order of UI displays such as results panels or victory/defeat screens, waiting for each element to appear or animate before proceeding to the next.

  • Running Game Logic: Async helps in waiting for certain game events to complete, such as waiting for an enemy's attack animation to finish before calculating damage, or ensuring player input only after a cinematic ends.

Tools for Async Programming in Unity

Coroutines

Coroutines are Unity's built-in way of handling asynchronous operations. They allow you to suspend execution and resume at a later time. Coroutines are especially useful for simple timing operations or sequences, such as waiting for an animation to complete before proceeding to the next step.

What are Coroutines?

A coroutine is a method that returns an IEnumerator and allows pausing its execution using the yield statement. Coroutines do not run in parallel; they run on Unity's main thread but can pause and yield control back to the game loop until a certain condition is met (e.g., a delay, an event, etc.).

Example: Coroutine for Sequencing UI Panels

IEnumerator ShowResultsInOrder()
{
// Show panel
resultPanel.SetActive(true);

// Wait for 2 seconds
yield return new WaitForSeconds(2);

// Show score text
scoreText.SetActive(true);

// Wait for animation to finish
yield return new WaitForSeconds(1);

// Show continue button
continueButton.SetActive(true);
}

In this example, the coroutine sequences UI elements with delays, ensuring the result panel and buttons are displayed in order without blocking the main thread.

Advantages of Coroutines

  • Simple to use, especially for sequences and time-based actions.
  • Perfect for tasks like animations, timed actions, and transitions.

Limitations

  • Coroutines only run on Unity's main thread, so they are not suitable for background tasks.
  • No native exception handling mechanism.
  • Cannot directly await the completion of tasks that run in true parallel (e.g., I/O-bound or CPU-bound operations).

For more detailed information, visit Unity's official documentation on Coroutines.

Async/Await

Async/await is a modern feature of C# that allows more complex asynchronous operations. Unlike coroutines, async/await enables true concurrency, allowing tasks to run in the background without blocking the main thread. This is particularly useful for tasks such as loading assets, making network requests, or performing CPU-intensive computations.

What is Async/Await?

The async keyword in C# allows you to write methods that run asynchronously, and the await keyword allows you to wait for those operations to complete without freezing the main thread. These methods return Task or Task<T>, which represent ongoing operations that can be awaited.

Example: Loading an Asset Asynchronously

async Task LoadAssetAsync()
{
var asset = await Resources.LoadAsync<GameObject>("path/to/asset");
Instantiate(asset);
}

In this example, the asset is loaded asynchronously, and the method continues only after the asset has finished loading, ensuring no disruption to gameplay.

Advantages of Async/Await

  • Supports true parallelism for I/O-bound or CPU-bound operations.
  • Better suited for complex background tasks than coroutines.
  • Provides built-in exception handling with try/catch.

Limitations

  • Async/await can lead to performance overhead on mobile due to frequent memory allocations, but this can be mitigated with libraries like UniTask.
  • Requires careful management to avoid blocking the main thread.

For more detailed information, see Microsoft's Async Programming Documentation.

UniTask

UniTask is a third-party library that extends async/await functionality for Unity, addressing the performance concerns of C#'s native Task system, particularly in mobile environments. UniTask is designed to minimize garbage collection (GC) and avoid memory allocations common with regular Task objects, making it an ideal choice for performance-sensitive projects.

What is UniTask?

UniTask is similar to async/await but optimized for Unity, providing a zero-allocation, GC-friendly alternative to native Task. It offers powerful concurrency features that are particularly useful for mobile games where performance and memory management are key concerns.

Example: Running Multiple Async Tasks

async UniTask LoadAssetsAsync()
{
var loadPlayer = Resources.LoadAsync<GameObject>("PlayerPrefab");
var loadEnemy = Resources.LoadAsync<GameObject>("EnemyPrefab");

await UniTask.WhenAll(loadPlayer, loadEnemy);

Instantiate(loadPlayer.asset);
Instantiate(loadEnemy.asset);
}

Here, UniTask ensures both the player and enemy prefabs are loaded concurrently, and the method waits for both to finish before proceeding.

Advantages of UniTask

  • Significantly reduces memory allocation and GC overhead compared to regular Task.
  • Supports async/await syntax with additional Unity-specific features like UniTask.WhenAll, UniTask.Delay, etc.
  • Highly performant, especially for mobile platforms.

Limitations

  • Requires external package installation.
  • Not as commonly used or as well-documented as native coroutines or async/await.

You can find more about UniTask on its GitHub page.