Skip to content

Instantly share code, notes, and snippets.

@tmr111116
Created March 22, 2017 12:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmr111116/39283ed7fcd7901fbdcbac7c92377c3b to your computer and use it in GitHub Desktop.
Save tmr111116/39283ed7fcd7901fbdcbac7c92377c3b to your computer and use it in GitHub Desktop.
ネストしたコルーチンを止めてみる。
using System.Collections;
using UnityEngine;
public class NestCoroutine : MonoBehaviour
{
private IEnumerator _coroutine;
private int _count;
private int Count
{
get
{
return _count;
}
set
{
_count = value;
Debug.Log(_count);
}
}
private void Start()
{
Count = 0;
_coroutine = MainCoroutine();
StartCoroutine(_coroutine);
}
[ContextMenu("StopCoroutine")]
private void StopCoroutine()
{
Debug.LogWarning("Stop");
StopCoroutine(_coroutine);
}
[ContextMenu("RestartCoroutine")]
private void RestartCoroutine()
{
Debug.LogWarning("Restart");
StartCoroutine(_coroutine);
}
private IEnumerator MainCoroutine()
{
for (int i = 1; i <= 5; i++)
{
yield return new WaitForSeconds(1);
Count = i;
}
yield return StartCoroutine(SubCoroutine());
yield return SubCoroutine();
yield return new WaitForSeconds(1);
Count = 99;
}
private IEnumerator SubCoroutine()
{
var last = Count + 5;
for (int i = Count + 1; i <= last; i++)
{
yield return new WaitForSeconds(1);
Count = i;
}
}
}
@tmr111116
Copy link
Author

結果
2017-03-22 21 25 37

yield return StartCoroutine(SubCoroutine()) だと SubCoroutine が終わるまで止まらない。
yield return SubCoroutine() だと SubCoroutine の途中で止まる。

入れ子のコルーチンを StartCoroutine で再開するのは無理そう。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment