Created
March 22, 2017 12:25
-
-
Save tmr111116/39283ed7fcd7901fbdcbac7c92377c3b to your computer and use it in GitHub Desktop.
ネストしたコルーチンを止めてみる。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
結果

yield return StartCoroutine(SubCoroutine())
だとSubCoroutine
が終わるまで止まらない。yield return SubCoroutine()
だとSubCoroutine
の途中で止まる。入れ子のコルーチンを
StartCoroutine
で再開するのは無理そう。