Skip to content

Instantly share code, notes, and snippets.

@hacha
Created September 19, 2016 07:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hacha/9ef45295e092564c84f68e84d00e3aff to your computer and use it in GitHub Desktop.
Save hacha/9ef45295e092564c84f68e84d00e3aff to your computer and use it in GitHub Desktop.
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