public static void StopCoroutine(MonoBehaviour mb, string tag) { if (!routinesMap.ContainsKey(mb)) { return; } RoutineActivity current = null; // Stop an already running coroutine if present bool isPresent = routinesMap[mb].TryGetValue(tag, out current); if (isPresent) { //Trace.Script("Stopping " + tag); current.Stop(); } }
public static void StartCoroutine(MonoBehaviour mb, IEnumerator routine, string tag, System.Action onFinished = null) { RoutineActivity current = null; // Check if this monobehaviour has already been used before if (!routinesMap.ContainsKey(mb)) { routinesMap.Add(mb, new Dictionary <string, RoutineActivity>()); } // Stop an already running coroutine if present bool isPresent = routinesMap[mb].TryGetValue(tag, out current); if (isPresent) { current.Stop(); } // Otherwise add it to the list else { current = new RoutineActivity(mb); routinesMap[mb].Add(tag, current); } // Now add/replace the current routine if (onFinished != null) { IEnumerator composedRoutine = ComposeRoutineWithCallback(mb, current, routine, onFinished); if (mb.isActiveAndEnabled) { current.coroutine = mb.StartCoroutine(composedRoutine); } } else { if (mb.isActiveAndEnabled) { current.coroutine = mb.StartCoroutine(routine); } } // Start it //Trace.Script("Starting " + tag); }