Skip to content

lonelyone007/componenttask-unity

 
 

Repository files navigation

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

About

Library to run dotnet's 'Task' and 'Task<T>' scoped to Unity components.

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 92.6%
  • Shell 7.1%
  • Makefile 0.3%