Advertisement
Guest User

Untitled

a guest
Jul 30th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TestStopCoroutine : MonoBehaviour {
  6.  
  7.   IEnumerator co = null;
  8.  
  9.   void Start() {
  10.     co = FunA();
  11.     Invoke("OnCancelButtonClick", 2f);
  12.     StartCoroutine(co);
  13.   }
  14.  
  15.   private IEnumerator FunA() {
  16.     Debug.Log("Enter FunA...");
  17.     yield return FunB();
  18.     Debug.Log("FunA end...");
  19.   }
  20.  
  21.   private IEnumerator FunB()
  22.   {
  23.     Debug.Log("Enter FunB...");
  24.     yield return FunC();
  25.     Debug.Log("FunB end...");
  26.   }
  27.  
  28.   private IEnumerator FunC() {
  29.     Debug.Log("Enter FunC...");
  30.     yield return RepeatPrint();
  31.     Debug.Log("FunC end...");
  32.   }
  33.  
  34.   private IEnumerator RepeatPrint() {
  35.     for (int i = 0; i < 5; i++) {
  36.       Debug.Log(i);
  37.       yield return new WaitForSeconds(1);
  38.     }
  39.   }
  40.  
  41.   /// <summary>
  42.   /// Set this function to a button on UI Canvas
  43.   /// </summary>
  44.   public void OnCancelButtonClick() {
  45.     if (co != null) {
  46.       StopCoroutine(co);
  47.       Debug.Log("Stop Coroutine...");
  48.       co = null;
  49.     }
  50.   }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement