Skip to content

Instantly share code, notes, and snippets.

@sebtoun
Last active April 12, 2018 08:14
Show Gist options
  • Save sebtoun/5e90f8ee47723b4b923781d48c6df02e to your computer and use it in GitHub Desktop.
Save sebtoun/5e90f8ee47723b4b923781d48c6df02e to your computer and use it in GitHub Desktop.
Unity's coroutines extensions
using System;
using System.Collections;
using UnityEngine;
public static class CoroutineExtensions
{
public static Coroutine Then( this Coroutine self, Action action )
{
return DefaultCoroutineHost.Instance.StartCoroutine( ChainWithAction( self, action ) );
}
public static Coroutine Then( this Coroutine self, IEnumerator coroutine )
{
return DefaultCoroutineHost.Instance.StartCoroutine( ChainWithCoroutine( self, coroutine ) );
}
private static IEnumerator ChainWithCoroutine( Coroutine first, IEnumerator second )
{
yield return first;
yield return DefaultCoroutineHost.Instance.StartCoroutine( second );
}
private static IEnumerator ChainWithAction( Coroutine coroutine, Action action )
{
yield return coroutine;
action();
}
}
using Gamelogic.Extensions;
using UnityEngine;
public class DefaultCoroutineHost : GLMonoBehaviour
{
private static DefaultCoroutineHost _instance;
public static DefaultCoroutineHost Instance
{
get
{
if ( _instance == null )
{
_instance = FindObjectOfType<DefaultCoroutineHost>();
if ( _instance == null )
{
_instance = new GameObject( "Default Coroutine Host", typeof( DefaultCoroutineHost ) )
.GetComponent<DefaultCoroutineHost>();
}
}
return _instance;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment