Sample07_OrchestratIEnumerator.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #pragma warning disable 0168
  2. #pragma warning disable 0219
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using UnityEngine;
  9. namespace UniRx.Examples
  10. {
  11. public class Sample07_OrchestratIEnumerator : MonoBehaviour
  12. {
  13. // two coroutines
  14. IEnumerator AsyncA()
  15. {
  16. Debug.Log("a start");
  17. yield return new WaitForSeconds(3);
  18. Debug.Log("a end");
  19. }
  20. IEnumerator AsyncB()
  21. {
  22. Debug.Log("b start");
  23. yield return new WaitForEndOfFrame();
  24. Debug.Log("b end");
  25. }
  26. void Start()
  27. {
  28. // after completed AsyncA, run AsyncB as continuous routine.
  29. // UniRx expands SelectMany(IEnumerator) as SelectMany(IEnumerator.ToObservable())
  30. var cancel = Observable.FromCoroutine(AsyncA)
  31. .SelectMany(AsyncB)
  32. .Subscribe();
  33. // If you want to stop Coroutine(as cancel), call subscription.Dispose()
  34. // cancel.Dispose();
  35. }
  36. }
  37. }
  38. #pragma warning restore 0219
  39. #pragma warning restore 0168