Created
February 20, 2017 09:55
-
-
Save YutaKaseda/9ab1097554ad9f37a8931f82cfdcb467 to your computer and use it in GitHub Desktop.
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.Colletions; | |
using UniRx; | |
public class Example : MonoBehaviour | |
{ | |
void Start() | |
{ | |
//Coroutine実行中にExampleが破棄されると、エラーになる | |
Observable.ReturnUnit() | |
.SelectMany(_ => BadCoroutine()) | |
.Subscribe(_ => Debug.Log("Bad")) | |
.AddTo(this); | |
//Coroutine実行中でもExampleを破棄することで、正常にすべて破棄される | |
Observable.ReturnUnit() | |
.SelectMany(_ => GoodCoroutine()) | |
.Subscribe(_ => Debug.Log("Nice")) | |
.AddTo(this); | |
} | |
IEnumerator BadCoroutine() | |
{ | |
//一秒後にCoroutineを終了するIEnumeratorに変換したObserver | |
yield return Observable.Timer(System.TimeSpan.FromSeconds(1)) | |
.FirstOrDefault() | |
.ToYieldInstruction(); | |
} | |
IEnumerator GoodCoroutine() | |
{ | |
yield return Observable.Timer(System.TimeSpan.FromSeconds(1)) | |
.FirstOrDefault() | |
.ToYieldInstruction() | |
.AddTo(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment