A coroutine that can be executed in parallel.
using System.Collections;
using System.Collections.Generic;
using info.shibuya24;
using UnityEngine;
public class ParallelCoroutineTest : MonoBehaviour
{
IEnumerator Start()
{
var result = ParallelCoroutine.Execute(this, new List<IEnumerator>()
{
TestCoroutine(1),
TestCoroutine(2, 2),
TestCoroutine(3, 3),
TestCoroutine(4, 4),
TestCoroutine(5, 5)
}, () => Debug.Log("AllComplete"));
yield return new WaitForSeconds(2f);
// Cancel Coroutine
result.StopCoroutine();
}
IEnumerator TestCoroutine(int id, float sec = 1f)
{
Debug.Log($"Start : {id}");
yield return new WaitForSeconds(sec);
Debug.Log($"End : {id}");
}
}
- Unity 2018.4 or higher
You can add https://github.com/baobao/ParallelCoroutine.git
to Package Manager.
or
Add "info.shibuya24.parallelcoroutine": "https://github.com/baobao/ParallelCoroutine.git"
to Packages/manifest.json.
This library is under the MIT License.