Skip to content

Instantly share code, notes, and snippets.

@YutaKaseda
Created February 20, 2017 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YutaKaseda/9ab1097554ad9f37a8931f82cfdcb467 to your computer and use it in GitHub Desktop.
Save YutaKaseda/9ab1097554ad9f37a8931f82cfdcb467 to your computer and use it in GitHub Desktop.
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