A simple implementation of Unity's Coroutines to be used for any C# project
Find a file
2021-03-18 09:37:15 -04:00
.github Create FUNDING.yml 2020-06-15 02:05:22 +02:00
Coroutine update 2021-03-17 01:17:16 +01:00
CoroutineTests Added event based test. 2021-03-18 09:37:15 -04:00
Test revert back to using for loops to allow for nested coroutines to work correctly 2021-03-17 00:38:44 +01:00
.gitignore publish the thing 2019-06-23 01:29:11 +02:00
Coroutine.sln manually cleaned up the Coroutine.sln file 2021-03-18 04:34:03 -04:00
Jenkinsfile Only publish on master 2021-01-01 11:10:35 +01:00
LICENSE.md Create LICENSE.md 2019-06-23 01:03:41 +02:00
README.md update the documentation (which has been outdated for a while apparently, oops) 2020-12-20 22:00:40 +01:00

Coroutine

A simple implementation of Unity's Coroutines to be used for any C# project

Features

Coroutine adds the ability to run coroutines. Coroutines are methods that run in parallel to the rest of the application through the use of an Enumerator. This allows for the coroutine to pause execution using the yield return statement.

There are two predefined ways to pause a coroutine:

  • Waiting for a certain amount of seconds to have passed
  • Waiting for a certain custom event to occur

Additionally, Coroutine provides the following features:

  • Creation of custom events to wait for
  • Creation of custom wait conditions
  • No multi-threading, which allows for any kind of process to be executed in a coroutine, including rendering

How to Use

Setting up the CoroutineHandler

The CoroutineHandler is the place where coroutines get executed. For this to occur, the Tick method needs to be called continuously. The Tick method takes a single parameter which represents the amount of seconds since the last time it was called. It can either be called in your application's existing update loop or as follows.

var lastTime = DateTime.Now;
while (true) {
    var currTime = DateTime.Now;
    CoroutineHandler.Tick((currTime - lastTime).TotalSeconds);
    lastTime = currTime;
    Thread.Sleep(1);
}

Creating a Coroutine

To create a coroutine, simply create a method with the return type IEnumerator<Wait>. Then, you can use yield return to cause the coroutine to wait at any point:

private static IEnumerator<Wait> WaitSeconds() {
    Console.WriteLine("First thing " + DateTime.Now);
    yield return new Wait(1);
    Console.WriteLine("After 1 second " + DateTime.Now);
    yield return new Wait(5);
    Console.WriteLine("After 5 seconds " + DateTime.Now);
    yield return new Wait(10);
    Console.WriteLine("After 10 seconds " + DateTime.Now);
}

Starting a Coroutine

To start a coroutine, simply call Start:

CoroutineHandler.Start(WaitSeconds());

Using Events

To use an event, an Event instance first needs to be created. When not overriding any equality operators, only a single instance of each event should be used.

private static readonly Event TestEvent = new Event();

Waiting for an event in a coroutine works as follows:

private static IEnumerator<Wait> WaitForTestEvent() {
    yield return new Wait(TestEvent);
    Console.WriteLine("Test event received");
}

To actually cause the event to be raised, causing all currently waiting coroutines to be continued, simply call RaiseEvent:

CoroutineHandler.RaiseEvent(TestEvent);

Additional Examples

For additional examples, take a look at the Example class.