Created
September 15, 2019 13:03
-
-
Save rakkarage/f072d494eb940b0147b56052a1db5ffa to your computer and use it in GitHub Desktop.
MonoBehaviour Do, DoAfter, and Repeat for StartCoroutine
This file contains hidden or 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; | |
namespace ca.HenrySoftware.Rage | |
{ | |
public static partial class MonoBehaviourExtensions | |
{ | |
public static void Do(this MonoBehaviour m, Action a) | |
{ | |
m.StartCoroutine(DoCoroutine(a)); | |
} | |
private static IEnumerator DoCoroutine(Action a) | |
{ | |
yield return null; | |
a(); | |
} | |
public static void DoAfter(this MonoBehaviour m, Action a, float t) | |
{ | |
m.StartCoroutine(DoAfterCoroutine(a, t)); | |
} | |
private static IEnumerator DoAfterCoroutine(Action a, float t) | |
{ | |
yield return new WaitForSeconds(t); | |
a(); | |
} | |
public static void Repeat(this MonoBehaviour m, Action a, float t) | |
{ | |
m.StartCoroutine(RepeatCoroutine(a, t)); | |
} | |
private static IEnumerator RepeatCoroutine(Action a, float t) | |
{ | |
a(); | |
while (true) | |
yield return DoAfterCoroutine(a, t); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment