Skip to content

Instantly share code, notes, and snippets.

@rakkarage
Created September 15, 2019 13:03
Show Gist options
  • Save rakkarage/f072d494eb940b0147b56052a1db5ffa to your computer and use it in GitHub Desktop.
Save rakkarage/f072d494eb940b0147b56052a1db5ffa to your computer and use it in GitHub Desktop.
MonoBehaviour Do, DoAfter, and Repeat for StartCoroutine
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