Skip to content

Instantly share code, notes, and snippets.

@grimmdev
Created December 28, 2020 02:25
Show Gist options
  • Save grimmdev/3b67b25b31002fec6f98fe59f5079121 to your computer and use it in GitHub Desktop.
Save grimmdev/3b67b25b31002fec6f98fe59f5079121 to your computer and use it in GitHub Desktop.
Coroutine Helper for when you're lazy and like to use coroutines.
using System.Collections;
using UnityEngine;
public class CoroutineHelper : MonoBehaviour
{
private IEnumerator routine;
public bool isDone
{
get { return done; }
}
private bool done;
private IEnumerator HandleRoutine()
{
yield return StartCoroutine(routine);
done = true;
Destroy(gameObject);
}
public static bool HelpersDone(CoroutineHelper[] helpers)
{
for (int i = 0; i < helpers.Length; i++)
{
if (!(helpers[i] == null) && !helpers[i].isDone)
{
return false;
}
}
return true;
}
public static CoroutineHelper Init(IEnumerator ro, bool startNow = false)
{
CoroutineHelper coroutineHelper = new GameObject("CoroutineHelper").AddComponent<CoroutineHelper>();
coroutineHelper.routine = ro;
if(startNow)
coroutineHelper.Go();
return coroutineHelper;
}
public static CoroutineHelper[] InitArr(IEnumerator[] ro, bool startNow = false)
{
CoroutineHelper[] array = new CoroutineHelper[ro.Length];
for (int i = 0; i < array.Length; i++)
{
array[i] = CoroutineHelper.Init(ro[i]);
if (startNow)
array[i].Go();
}
return array;
}
public void Go()
{
StartCoroutine(HandleRoutine());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment