Skip to content

Instantly share code, notes, and snippets.

@conanfred
Forked from grimmdev/CoroutineHelper.cs
Created March 21, 2022 11:17
Show Gist options
  • Save conanfred/764754a599a8d80a7916144fdc4641ac to your computer and use it in GitHub Desktop.
Save conanfred/764754a599a8d80a7916144fdc4641ac 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