Provides a replacement for Unity game engine coroutines that can return a generic result, mostly used in a similar manner to UnityEngine.Coroutine
.
To use the Coroutine<T>
class, follow these steps:
- Ensure that you have a reference to a
MonoBehaviour
instance. - Call the
StartCoroutine<T>
extension method on your MonoBehaviour instance. (Note thatthis.
is required to use an extension method in C#.) - Pass the routine you want to execute as an
IEnumerator
parameter. - Cast the
Coroutine<T>
to aUnityEngine.Coroutine
to wait for execution. - Access the
.Result
.
using System;
using System.Collections;
using UnityEngine;
public class CoroutineExample : MonoBehaviour
{
private IEnumerator Start()
{
// Start the coroutine
Coroutine<int> coroutine = this.StartCoroutine<int>(YourRoutine());
// Wait for the coroutine to complete by casting to a UnityEngine.Coroutine
yield return (Coroutine)coroutine;
try
{
// Access the result of the coroutine
int result = coroutine.Result;
Debug.Log("Coroutine result: " + result);
}
catch (ApplicationException exception)
{
// Handle any excpetion
Debug.LogError("Coroutine exception: " + exception.Message);
}
}
private IEnumerator YourRoutine()
{
// Your coroutine logic here
yield return new WaitForSeconds(2);
// Exceptions will be re-thrown
if (UnityEngine.Random.value < 0.5f)
{
throw new ApplicationException("Something went wrong in the coroutine!");
}
yield return 42;
}
}
In the example above, a coroutine YourRoutine
is started using the StartCoroutine<int>
extension method. The result of the coroutine is accessed using the Result
property of the Coroutine<int>
object.
Exception will be thrown if any of these circumstances are true:
- Attempting to access the result of a coroutine before it has yielded a result will throw an
InvalidOperationException
. - After a coroutine is cancelled with
.Cancel()
, attempting to access the result will throw anOperationCanceledException
. - If the coroutine throws an exception during execution, the
Coroutine<T>
object will capture and re-throw the exception when the result is accessed.
A static class that extends the MonoBehaviour class with the StartCoroutine<T>
extension method.
StartCoroutine<T>(this MonoBehaviour monoBehaviour, IEnumerator routine)
: Starts a coroutine and returns aCoroutine<T>
object.
A class that represents a coroutine with a result.
Result
: The result of the coroutine. Accessing this property will throw an exception if the coroutine has not yielded a result yet.Cancel()
: Cancels the execution of the coroutine.implicit operator UnityEngine.Coroutine
: Allows implicit conversion fromCoroutine<T>
toUnityEngine.Coroutine
.
This code is provided under the MIT License. Feel free to modify and use it in your projects.