Coroutine 2.1.5
dotnet add package Coroutine --version 2.1.5
NuGet\Install-Package Coroutine -Version 2.1.5
<PackageReference Include="Coroutine" Version="2.1.5" />
paket add Coroutine --version 2.1.5
#r "nuget: Coroutine, 2.1.5"
// Install Coroutine as a Cake Addin #addin nuget:?package=Coroutine&version=2.1.5 // Install Coroutine as a Cake Tool #tool nuget:?package=Coroutine&version=2.1.5
Coroutine is 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
- No multi-threading, which allows for any kind of process to be executed in a coroutine, including rendering
- Thread-safety, which allows for coroutines to be started from different threads
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 time 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);
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");
}
Of course, having time-based waits and event-based waits in the same coroutine is also supported.
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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.5
- System.ValueTuple (>= 4.5.0)
-
.NETStandard 2.0
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Coroutine:
Package | Downloads |
---|---|
SadConsole
A library that emulates old-school console and command prompt style graphics. |
|
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class |
|
MLEM.Startup.FNA
MLEM Library for Extending FNA combined with some other useful libraries into a quick Game startup class |
|
MLEM.Startup.KNI
MLEM Library for Extending KNI combined with some other useful libraries into a quick Game startup class |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Coroutine:
Repository | Stars |
---|---|
Thraka/SadConsole
A .NET ascii/ansi console engine written in C# for MonoGame and XNA. Create your own text roguelike (or other) games!
|
Version | Downloads | Last updated |
---|---|---|
2.1.5 | 10,366 | 2/23/2023 |
2.1.4 | 1,475 | 9/14/2022 |
2.1.3 | 1,754 | 11/29/2021 |
2.1.2 | 1,317 | 11/7/2021 |
2.1.1 | 1,569 | 5/29/2021 |
2.1.0 | 503 | 3/21/2021 |
2.0.2 | 393 | 3/17/2021 |
2.0.1 | 680 | 12/20/2020 |
2.0.0 | 2,525 | 6/13/2020 |
1.0.4 | 1,153 | 5/19/2020 |
1.0.3 | 954 | 2/28/2020 |
1.0.2 | 1,721 | 11/20/2019 |
1.0.1 | 2,311 | 8/5/2019 |
1.0.0 | 613 | 6/22/2019 |