Created
September 2, 2016 14:58
-
-
Save renaudbedard/cc719e2027105f47fd51878c350a6e3c to your computer and use it in GitHub Desktop.
El cheapo way to fix the nested stopcoroutine problem
This file contains 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
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