mirror of
https://github.com/Ellpeck/Coroutine.git
synced 2024-11-22 05:43:29 +01:00
publish the thing
This commit is contained in:
parent
498b6e35bd
commit
a7ee8cdac0
5 changed files with 88 additions and 3 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -3,3 +3,4 @@ bin
|
||||||
obj
|
obj
|
||||||
packages
|
packages
|
||||||
*.user
|
*.user
|
||||||
|
*.nupkg
|
|
@ -46,6 +46,9 @@
|
||||||
<Compile Include="WaitEvent.cs" />
|
<Compile Include="WaitEvent.cs" />
|
||||||
<Compile Include="WaitSeconds.cs" />
|
<Compile Include="WaitSeconds.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="Coroutine.nuspec" />
|
||||||
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
Other similar extension points exist, see Microsoft.Common.targets.
|
Other similar extension points exist, see Microsoft.Common.targets.
|
||||||
|
|
19
Coroutine/Coroutine.nuspec
Normal file
19
Coroutine/Coroutine.nuspec
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<package>
|
||||||
|
<metadata>
|
||||||
|
<id>$id$</id>
|
||||||
|
<version>$version$</version>
|
||||||
|
<title>$title$</title>
|
||||||
|
<authors>$author$</authors>
|
||||||
|
<owners>$author$</owners>
|
||||||
|
<licenseUrl>https://github.com/Ellpeck/Coroutine/blob/master/LICENSE.md</licenseUrl>
|
||||||
|
<projectUrl>https://github.com/Ellpeck/Coroutine</projectUrl>
|
||||||
|
<requireLicenseAcceptance>false</requireLicenseAcceptance>
|
||||||
|
<description>$description$</description>
|
||||||
|
<summary>$description$</summary>
|
||||||
|
<copyright>Copyright 2019</copyright>
|
||||||
|
<tags>coroutine utility unity</tags>
|
||||||
|
|
||||||
|
<releaseNotes>Initial release</releaseNotes>
|
||||||
|
</metadata>
|
||||||
|
</package>
|
64
README.md
64
README.md
|
@ -1,2 +1,66 @@
|
||||||
# Coroutine
|
# Coroutine
|
||||||
A simple implementation of Unity's Coroutines to be used for any C# project
|
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. This can either be done in your application's existing update loop or as follows:
|
||||||
|
```cs
|
||||||
|
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:
|
||||||
|
```cs
|
||||||
|
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`:
|
||||||
|
```cs
|
||||||
|
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.
|
||||||
|
```cs
|
||||||
|
private static readonly Event TestEvent = new Event();
|
||||||
|
```
|
||||||
|
|
||||||
|
Waiting for an event in a coroutine works as follows:
|
||||||
|
```cs
|
||||||
|
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`:
|
||||||
|
```cs
|
||||||
|
CoroutineHandler.RaiseEvent(TestEvent);
|
||||||
|
```
|
|
@ -40,8 +40,6 @@ namespace Test {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerator<Wait> WaitForTestEvent() {
|
private static IEnumerator<Wait> WaitForTestEvent() {
|
||||||
yield return new WaitSeconds(5);
|
|
||||||
Console.WriteLine("Waited 5 seconds before waiting for the event");
|
|
||||||
yield return new WaitEvent(TestEvent);
|
yield return new WaitEvent(TestEvent);
|
||||||
Console.WriteLine("Test event received");
|
Console.WriteLine("Test event received");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue