Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
.ci
 
 
 
 
 
 
 
 
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

ComponentTask-Unity

Tests GitHub release License: MIT

Unity package for running dotnet Task and Task<T> scoped to Unity components.

Description

Common problem with using c# async methods in Unity is that they have no concept of a component life-time. So unlike Coroutines when you 'start' a task and then destroy the component (by loading a new scene for example) the task does not stop. Most of the time leading to UnityEngine.MissingReferenceException when you try to access members of the component after it has been destroyed on the Unity side. And as a workaround you have to write ugly 'if (!this) return;' guards after every await.

This library aims to fix that problem by allowing you to run tasks 'on' your MonoBehaviour with very similar behaviour as Unity's Coroutines.

Usage

using System.Threading.Tasks;
using UnityEngine;

class MyClass : MonoBehaviour
{
    void Start()
    {
        this.StartTask(RunAsync);
    }

    async Task RunAsync()
    {
        while (true)
        {
            Debug.Log("Running...");
            await Task.Yield();
        }
    }
}

This example will print Running... every frame when the component is enabled and will stop when the component gets destroyed.

More examples

Documentation