Created
September 19, 2016 07:43
-
-
Save hacha/9ef45295e092564c84f68e84d00e3aff to your computer and use it in GitHub Desktop.
2つのコルーチンの処理完了待ち合わせをUniRxを使って書いてみたもの
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 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