Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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