Skip to content

Instantly share code, notes, and snippets.

@renaudbedard
Created September 2, 2016 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renaudbedard/cc719e2027105f47fd51878c350a6e3c to your computer and use it in GitHub Desktop.
Save renaudbedard/cc719e2027105f47fd51878c350a6e3c to your computer and use it in GitHub Desktop.
El cheapo way to fix the nested stopcoroutine problem
class Foo : MonoBehaviour
{
Coroutine m_coroutine;
readonly HashSet<IEnumerator> m_innerCoroutines = new HashSet<IEnumerator>();
public void StartThing()
{
m_coroutine = StartCoroutine(ParentCoroutine());
}
public void EndThing()
{
if (m_coroutine != null)
{
StopCoroutine(m_coroutine);
m_coroutine = null;
foreach (var innerCoroutine in m_innerCoroutines)
StopCoroutine(innerCoroutine);
m_innerCoroutines.Clear();
}
}
public IEnumerator ParentCoroutine()
{
yield return StartAndTrack(ChildCoroutine(1));
yield return StartAndTrack(ChildCoroutine(2));
yield return StartAndTrack(ChildCoroutine(3));
}
IEnumerator StartAndTrack(IEnumerator _coroutine)
{
m_innerCoroutines.Add(_coroutine);
yield return _coroutine;
m_innerCoroutines.Remove(_coroutine);
}
public IEnumerator ChildCoroutine(float _varyingInput)
{
// do stuff based on input
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment