夏虫
  • 0
  • 新人请关照

Unity3D C# 这个协程为何是死循环?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{

bool a = true;
bool b = true;

void Update()
{
    if (a)
    {
        a = false;
        for (int i = 0; i < 3; i++)
        {
            if (b)
            {
                b = false;
                StartCoroutine("Wait");
                Invoke("Stop", 5);
            }
            else
            {
                i--;
            }
        }
        a = true;
    }
}

IEnumerator Wait()
{
    int i = 0;
    while (i<5)
    {
        yield return new WaitForSeconds(1);
        i++;
        Debug.Log(i);
    }
}

void Stop()
{
    StopCoroutine("Wait");
    b = true;
}

}

阅读 211
评论
    撰写回答

    登录后参与交流、获取后续更新提醒