Last active
September 8, 2022 22:44
-
-
Save nikescar1/65255cacc6450c2c464802f413a87a83 to your computer and use it in GitHub Desktop.
Scheduling Coroutines
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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using System; | |
public class CoroutineTesting : MonoBehaviour | |
{ | |
public class CoroutineGroup | |
{ | |
public class CoroutineWithFinish | |
{ | |
public IEnumerator coroutine; | |
public bool isFinished; | |
public CoroutineWithFinish(IEnumerator coroutine) | |
{ | |
this.coroutine = coroutine; | |
} | |
public Coroutine Start(MonoBehaviour parent) | |
{ | |
isFinished = false; | |
return parent.StartCoroutine(Runner()); | |
} | |
private IEnumerator Runner() | |
{ | |
yield return coroutine; | |
isFinished = true; | |
} | |
} | |
public List<CoroutineWithFinish> coroutineList = new List<CoroutineWithFinish>(); | |
public CoroutineGroup(params IEnumerator[] coroutines) | |
{ | |
foreach (var coroutine in coroutines) | |
{ | |
coroutineList.Add(new CoroutineWithFinish(coroutine)); | |
} | |
} | |
public void Add(IEnumerator coroutine) | |
{ | |
coroutineList.Add(new CoroutineWithFinish(coroutine)); | |
} | |
public void StartRunSimultaneously(MonoBehaviour parent, Action onFinished) | |
{ | |
foreach (CoroutineWithFinish coroutine in coroutineList) | |
{ | |
coroutine.Start(parent); | |
} | |
parent.StartCoroutine(StartRunSimultaneouslyCoroutine(parent, onFinished)); | |
} | |
private IEnumerator StartRunSimultaneouslyCoroutine(MonoBehaviour parent, Action onFinished) | |
{ | |
while (coroutineList.FindAll(c => c.isFinished).Count != coroutineList.Count) | |
{ | |
yield return null; | |
} | |
onFinished?.Invoke(); | |
} | |
public void StartRunConsecutively(MonoBehaviour parent, Action onFinished) | |
{ | |
parent.StartCoroutine(StartRunConsecutivelyCoroutine(parent, onFinished)); | |
} | |
private IEnumerator StartRunConsecutivelyCoroutine(MonoBehaviour parent, Action onFinished) | |
{ | |
foreach (CoroutineWithFinish coroutine in coroutineList) | |
{ | |
yield return coroutine.Start(parent); | |
} | |
onFinished?.Invoke(); | |
} | |
} | |
public bool testSimultaneous; | |
public bool testConsecutive; | |
public void TestCoroutineGroupSimultaneous() | |
{ | |
CoroutineGroup coroutineGroupSimultaneous = new CoroutineGroup(); | |
coroutineGroupSimultaneous.Add(C1(OnIndividualFinished)); | |
coroutineGroupSimultaneous.Add(C2(OnIndividualFinished)); | |
coroutineGroupSimultaneous.Add(C3(OnIndividualFinished)); | |
coroutineGroupSimultaneous.StartRunSimultaneously(this, OnAllFinished); | |
} | |
public void TestCoroutineGroupConsecutive() | |
{ | |
CoroutineGroup coroutineGroupConsecutive = new CoroutineGroup(C1(OnIndividualFinished), C2(OnIndividualFinished), C3(OnIndividualFinished)); | |
coroutineGroupConsecutive.StartRunConsecutively(this, OnAllFinished); | |
} | |
private void OnIndividualFinished(string name, float duration) | |
{ | |
Debug.Log($"{name} duration:{duration} finished at {TimeSpan.FromSeconds(Time.time)}"); | |
} | |
private void OnAllFinished() | |
{ | |
Debug.Log("Finished"); | |
} | |
IEnumerator C1(Action<string, float> onFinish = null) | |
{ | |
float duration = .5f; | |
yield return new WaitForSeconds(duration); | |
onFinish?.Invoke("C1", duration); | |
} | |
IEnumerator C2(Action<string, float> onFinish = null) | |
{ | |
float duration = .25f; | |
yield return new WaitForSeconds(duration); | |
onFinish?.Invoke("C2", duration); | |
} | |
IEnumerator C3(Action<string, float> onFinish = null) | |
{ | |
float duration = .75f; | |
yield return new WaitForSeconds(duration); | |
onFinish?.Invoke("C3", duration); | |
} | |
public void Update() | |
{ | |
if (testSimultaneous) | |
{ | |
testSimultaneous = false; | |
TestCoroutineGroupConsecutive(); | |
} | |
if (testConsecutive) | |
{ | |
testConsecutive = false; | |
TestCoroutineGroupConsecutive(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment