Skip to content

Instantly share code, notes, and snippets.

@sebtoun
Created April 12, 2018 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sebtoun/3355fcfb8d11fb158800ce15d2bae404 to your computer and use it in GitHub Desktop.
Save sebtoun/3355fcfb8d11fb158800ce15d2bae404 to your computer and use it in GitHub Desktop.
C# events like object for Unity's coroutine
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoroutineEvent<T1, T2>
{
public delegate IEnumerator CoroutineDelegate( T1 parameter1, T2 parameter2 );
private readonly List<CoroutineDelegate> _eventHandlers = new List<CoroutineDelegate>();
private int _runningCoroutinesCount;
public void AddCoroutineDelegate( CoroutineDelegate coroutine )
{
_eventHandlers.Add( coroutine );
}
public bool RemoveCoroutineDelegate( CoroutineDelegate coroutine )
{
return _eventHandlers.Remove( coroutine );
}
public static CoroutineEvent<T1, T2> operator +( CoroutineEvent<T1, T2> self, CoroutineDelegate coroutine )
{
self.AddCoroutineDelegate( coroutine );
return self;
}
public static CoroutineEvent<T1, T2> operator -( CoroutineEvent<T1, T2> self, CoroutineDelegate coroutine )
{
self.RemoveCoroutineDelegate( coroutine );
return self;
}
public IEnumerator Coroutine( MonoBehaviour host, T1 parameter1, T2 parameter2 )
{
_runningCoroutinesCount = _eventHandlers.Count;
foreach ( var eventCoroutine in _eventHandlers )
{
host.StartCoroutine( WrapCoroutine( host, eventCoroutine( parameter1, parameter2 ) ) );
}
while ( _runningCoroutinesCount > 0 )
{
yield return null;
}
}
private IEnumerator WrapCoroutine( MonoBehaviour host, IEnumerator coroutine )
{
yield return host.StartCoroutine( coroutine );
_runningCoroutinesCount--;
}
}
public class CoroutineEvent<T>
{
public delegate IEnumerator CoroutineDelegate( T parameter );
private readonly List<CoroutineDelegate> _eventHandlers = new List<CoroutineDelegate>();
private int _runningCoroutinesCount;
public void AddCoroutineDelegate( CoroutineDelegate coroutine )
{
_eventHandlers.Add( coroutine );
}
public bool RemoveCoroutineDelegate( CoroutineDelegate coroutine )
{
return _eventHandlers.Remove( coroutine );
}
public static CoroutineEvent<T> operator +( CoroutineEvent<T> self, CoroutineDelegate coroutine )
{
self.AddCoroutineDelegate( coroutine );
return self;
}
public static CoroutineEvent<T> operator -( CoroutineEvent<T> self, CoroutineDelegate coroutine )
{
self.RemoveCoroutineDelegate( coroutine );
return self;
}
public IEnumerator Coroutine( MonoBehaviour host, T parameter )
{
_runningCoroutinesCount = _eventHandlers.Count;
foreach ( var eventCoroutine in _eventHandlers )
{
host.StartCoroutine( WrapCoroutine( host, eventCoroutine( parameter ) ) );
}
while ( _runningCoroutinesCount > 0 )
{
yield return null;
}
}
public bool HasHandlers()
{
return _eventHandlers.Count > 0;
}
private IEnumerator WrapCoroutine( MonoBehaviour host, IEnumerator coroutine )
{
yield return host.StartCoroutine( coroutine );
_runningCoroutinesCount--;
}
}
public class CoroutineEvent
{
public delegate IEnumerator CoroutineDelegate();
private readonly List<CoroutineDelegate> _eventHandlers = new List<CoroutineDelegate>();
private int _runningCoroutinesCount;
public void AddCoroutineDelegate( CoroutineDelegate coroutine )
{
_eventHandlers.Add( coroutine );
}
public bool RemoveCoroutineDelegate( CoroutineDelegate coroutine )
{
return _eventHandlers.Remove( coroutine );
}
public static CoroutineEvent operator +( CoroutineEvent self, CoroutineDelegate coroutine )
{
self.AddCoroutineDelegate( coroutine );
return self;
}
public static CoroutineEvent operator -( CoroutineEvent self, CoroutineDelegate coroutine )
{
self.RemoveCoroutineDelegate( coroutine );
return self;
}
public IEnumerator Coroutine( MonoBehaviour host )
{
_runningCoroutinesCount = _eventHandlers.Count;
foreach ( var eventCoroutine in _eventHandlers )
{
host.StartCoroutine( WrapCoroutine( host, eventCoroutine() ) );
}
while ( _runningCoroutinesCount > 0 )
{
yield return null;
}
}
public bool HasHandlers()
{
return _eventHandlers.Count > 0;
}
private IEnumerator WrapCoroutine( MonoBehaviour host, IEnumerator coroutine )
{
yield return host.StartCoroutine( coroutine );
_runningCoroutinesCount--;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment