Create a gist now

Instantly share code, notes, and snippets.

2つのコルーチンの処理完了待ち合わせをUniRxを使って書いてみたもの
using UnityEngine;
using System.Collections;
using UniRx;
public class Foo : MonoBehaviour
{
IEnumerator Start ()
{
// 並列的に実行したい2つの処理があって、その両方が終わるまで待ち合わせをしたい。
Debug.Log("start");
StartCoroutine(ProcessA());
StartCoroutine(ProcessB());
yield return new WaitUntil(() => finishA && finishB);
Debug.Log("end");
// 上記を、終了フラグを使わずに待ち合わせを書けないか考えてみた書き方
Debug.Log("start");
var streamA = Observable.FromCoroutine(() => ProcessA());
var streamB = Observable.FromCoroutine(() => ProcessB());
yield return Observable.Zip(streamA, streamB, (a, b) => Unit.Default).StartAsCoroutine();
Debug.Log("end");
}
bool finishA, finishB;
IEnumerator ProcessA ()
{
// 1秒で終わる処理
yield return new WaitForSeconds(1);
Debug.Log("ProcessA end");
finishA = true;
}
IEnumerator ProcessB ()
{
// 2秒で終わる処理
yield return new WaitForSeconds(2);
Debug.Log("ProcessB end");
finishB = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment