Coroutine 1.0.0

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

There is a newer version of this package available.
See the version list below for details.
Install-Package Coroutine -Version 1.0.0
dotnet add package Coroutine --version 1.0.0
<PackageReference Include="Coroutine" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Coroutine --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Coroutine, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Coroutine as a Cake Addin
#addin nuget:?package=Coroutine&version=1.0.0

// Install Coroutine as a Cake Tool
#tool nuget:?package=Coroutine&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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 WaitSeconds(1);
    Console.WriteLine("After 1 second " + DateTime.Now);
    yield return new WaitSeconds(5);
    Console.WriteLine("After 5 seconds " + DateTime.Now);
    yield return new WaitSeconds(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 WaitEvent(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.

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 WaitSeconds(1);
    Console.WriteLine("After 1 second " + DateTime.Now);
    yield return new WaitSeconds(5);
    Console.WriteLine("After 5 seconds " + DateTime.Now);
    yield return new WaitSeconds(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 WaitEvent(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.

Release Notes

Initial release

Dependencies

This package has no dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Coroutine:

Package Downloads
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.1.1 69 5/29/2021
2.1.0 166 3/21/2021
2.0.2 68 3/17/2021
2.0.1 200 12/20/2020
2.0.0 613 6/13/2020
1.0.4 243 5/19/2020
1.0.3 294 2/28/2020
1.0.2 404 11/20/2019
1.0.1 526 8/5/2019
1.0.0 256 6/22/2019
Show less