Last active
April 12, 2018 08:14
-
-
Save sebtoun/5e90f8ee47723b4b923781d48c6df02e to your computer and use it in GitHub Desktop.
Unity's coroutines extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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