Coroutine 2.1.4-61
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.
Showing the top 20 packages that depend on Coroutine.
Packages | Downloads |
---|---|
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
66 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
67 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
69 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
77 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
83 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
89 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
92 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
107 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
125 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
173 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
180 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
182 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
206 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
512 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
545 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
640 |
MLEM.Startup
MLEM Library for Extending MonoGame combined with some other useful libraries into a quick Game startup class
|
818 |
MLEM.Startup.FNA
MLEM Library for Extending FNA combined with some other useful libraries into a quick Game startup class
|
66 |
.NET Framework 4.5
- System.ValueTuple (>= 4.5.0)
.NET 6.0
- No dependencies.
.NET Standard 2.0
- No dependencies.
Version | Downloads | Last updated |
---|---|---|
2.1.5-65 | 50 | 02/23/2023 |
2.1.4-64 | 33 | 02/23/2023 |
2.1.4-63 | 43 | 02/23/2023 |
2.1.4-62 | 49 | 10/31/2022 |
2.1.4-61 | 40 | 10/22/2022 |
2.1.4-60 | 44 | 10/22/2022 |
2.1.4-59 | 35 | 10/22/2022 |
2.1.4-58 | 45 | 09/14/2022 |
2.1.3-57 | 36 | 09/14/2022 |
2.1.3-56 | 96 | 12/29/2021 |
2.1.2-55 | 71 | 12/29/2021 |
2.1.2-54 | 76 | 12/29/2021 |
2.1.2-53 | 68 | 12/29/2021 |
2.1.1-51 | 87 | 12/29/2021 |
2.1.1-3 | 78 | 12/29/2021 |
2.1.1-2 | 74 | 12/29/2021 |
2.1.0-52 | 68 | 12/29/2021 |
2.1.0-50 | 71 | 12/29/2021 |
2.1.0-48 | 72 | 12/29/2021 |
2.0.2-47 | 77 | 12/29/2021 |
2.0.2-46 | 76 | 12/29/2021 |
2.0.2-45 | 65 | 12/29/2021 |
2.0.2-44 | 83 | 12/29/2021 |
2.0.2-43 | 84 | 12/29/2021 |
2.0.2-39 | 64 | 12/29/2021 |
2.0.2-38 | 68 | 12/29/2021 |
2.0.2-37 | 68 | 12/29/2021 |
2.0.2-36 | 65 | 12/29/2021 |
2.0.2-35 | 65 | 12/29/2021 |
2.0.2-34 | 66 | 12/29/2021 |
2.0.2-33 | 73 | 12/29/2021 |
2.0.2-32 | 73 | 12/29/2021 |
2.0.2-31 | 73 | 12/29/2021 |
2.0.2-30 | 72 | 12/29/2021 |
2.0.1-29 | 66 | 12/29/2021 |
2.0.1-28 | 79 | 12/29/2021 |
2.0.1-27 | 66 | 12/29/2021 |
2.0.1-26 | 83 | 12/29/2021 |
2.0.1-25 | 72 | 12/29/2021 |
2.0.1-24 | 74 | 12/29/2021 |
2.0.1-23 | 65 | 12/29/2021 |
2.0.1-22 | 82 | 12/29/2021 |
2.0.1-21 | 69 | 12/29/2021 |
2.0.1-2 | 67 | 12/29/2021 |
2.0.1-1 | 72 | 12/29/2021 |
2.0.0-20 | 77 | 12/29/2021 |
2.0.0-18 | 62 | 12/29/2021 |
1.0.4-17 | 67 | 12/29/2021 |
1.0.4-15 | 72 | 12/29/2021 |
1.0.4-14 | 69 | 12/29/2021 |
1.0.3-13 | 72 | 12/29/2021 |
1.0.3-12 | 77 | 12/29/2021 |
1.0.3-11 | 69 | 12/29/2021 |
1.0.3-10 | 75 | 12/29/2021 |
1.0.3-9 | 77 | 12/29/2021 |
1.0.3-8 | 79 | 12/29/2021 |
1.0.3-7 | 70 | 12/29/2021 |
1.0.3-6 | 64 | 12/29/2021 |
1.0.2-5 | 78 | 12/29/2021 |