In the following example, if Co3() is executed, then the coroutine can't be stopped by StopCoroutine()
public class TestCoroutine : MonoBehaviour {
private Coroutine coroutine;
// Use this for initialization
void Start () {
coroutine = StartCoroutine(Co1());
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.S))
{
if(coroutine != null)
{
StopCoroutine(coroutine);
Debug.LogError("Stop coroutine");
}
}
}
private IEnumerator Co1()
{
Debug.LogError("Co1 start");
yield return new WaitForSeconds(5);
Debug.LogError("Co1 end");
yield return Co2();
}
private IEnumerator Co2()
{
Debug.LogError("Co2 start");
float time = Time.time;
while(Time.time - time < 5)
{
yield return null;
}
Debug.LogError("Co2 end");
yield return Co3();
}
private IEnumerator Co3()
{
//coroutine can't be stopped by StopCoroutine if here is executed
Debug.LogError("Co3 start");
float time = Time.time;
while (Time.time - time < 5)
{
yield return null;
}
Debug.LogError("Co3 end");
}
}
沒有留言:
張貼留言