Skip to content

Greener-Games/TaskRunner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Global Task Runner

openupm

A lightweight and easy-to-use Task/Coroutine runner for Unity.

This utility allows you to run and manage long-running processes (coroutines) without needing a MonoBehaviour instance. It provides a simple API to start, stop, pause, and resume tasks.

Installation

You can install this package via the Unity Package Manager.

  1. Open the Package Manager in Window > Package Manager.
  2. Click the + button in the top-left corner and select "Add package from git URL...".
  3. Enter the following URL: https://github.com/Greener-Games/TaskRunner.git

The package will be installed in your project.

Usage

Here is a basic example of how to use the TaskRunner:

using UnityEngine;
using GG.GlobalTaskRunner;
using System.Collections;

public class Example : MonoBehaviour
{
    void Start()
    {
        // Create and start a new task
        TaskRunner.Create(MyCoroutine());
    }

    IEnumerator MyCoroutine()
    {
        Debug.Log("Task started!");
        yield return new WaitForSeconds(5);
        Debug.Log("Task finished after 5 seconds.");
    }
}

You can also create a TaskRunner instance to have more control over the task:

using UnityEngine;
using GG.GlobalTaskRunner;
using System.Collections;

public class AdvancedExample : MonoBehaviour
{
    private TaskRunner myTask;

    void Start()
    {
        // Create a task without starting it immediately
        myTask = new TaskRunner(MyCoroutine(), autoStart: false);

        // Add a handler for when the task finishes
        myTask.TaskFinishedHandler += OnTaskFinished;

        // Start the task
        myTask.Start();
    }

    void OnTaskFinished(bool manuallyStopped)
    {
        if (manuallyStopped)
        {
            Debug.Log("Task was stopped manually.");
        }
        else
        {
            Debug.Log("Task completed successfully.");
        }
    }

    IEnumerator MyCoroutine()
    {
        Debug.Log("Task started!");
        for (int i = 0; i < 5; i++)
        {
            Debug.Log("Working... " + i);
            yield return new WaitForSeconds(1);
        }
    }

    void OnDestroy()
    {
        // It's good practice to stop the task if the object is destroyed
        if (myTask != null && myTask.Running)
        {
            myTask.Stop();
        }
    }
}

Documentation

For more detailed information and API reference, please see the full documentation.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages