Skip to content

Instantly share code, notes, and snippets.

@cooloon
Created July 11, 2018 03:40
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 cooloon/961dafae9055109ad9ac5371c7e2b15e to your computer and use it in GitHub Desktop.
Save cooloon/961dafae9055109ad9ac5371c7e2b15e to your computer and use it in GitHub Desktop.
[Unity] Pause and Resume coroutine
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
IEnumerator _routine;
IEnumerator routine { get { return _routine ?? (_routine = RoutineImpl()); } }
void OnEnable()
{
StartCoroutine(routine);
}
void OnDisable()
{
StopCoroutine(routine);
}
public IEnumerator RoutineImpl()
{
while (true)
{
Debug.LogFormat("[{0}] Log-A", Time.frameCount);
yield return null;
Debug.LogFormat("[{0}] Log-B", Time.frameCount);
yield return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment