unity3d 无法停止Unity中的协同程序

9rygscc1  于 2023-03-03  发布在  其他
关注(0)|答案(5)|浏览(251)

我试图阻止在玩家死亡时产生的波,但是我似乎不能阻止协程。我真的不知道如何解决这个问题,因为添加if语句和break不起作用。我如何调用StopCoroutine并让例程停止?我需要引入新的方法吗?

void Start () {
    gameOver = false;
    StartCoroutine (SpawnWaves());
}

void Update()
{
    if (gameOver)
    {
        StopCoroutine(SpawnWaves());
    }
  }

IEnumerator SpawnWaves()
{
    yield return new WaitForSeconds(startWait);
    while (true)
    {
        for (int i = 0; i < hazardCount; i++)
        {
            GameObject enemy = enemies[Random.Range(0, enemies.Length)];
            Instantiate(enemy, spawnPosition1, spawnRotation1);
            Instantiate(enemy, spawnPosition2, spawnRotation2);
            Instantiate(enemy, spawnPosition3, spawnRotation3);
            Instantiate(enemy, spawnPosition4, spawnRotation4);
            Instantiate(enemy, spawnPosition5, spawnRotation5);
            Instantiate(enemy, spawnPosition6, spawnRotation6);
            yield return new WaitForSeconds(spawnWait);
        }
        yield return new WaitForSeconds(waveWait);
        enemies[0].GetComponent<EnemyBehviour>().currentHealth *= enemyHealthMultiplier;
    }
}
ncgqoxb0

ncgqoxb01#

1.当玩家死亡时,您需要将gameOver设置为True
1.您还需要添加逻辑来将while循环更改为false,以退出while循环。
例如,当currentHealth == 0在while循环中时设置gameOver = true,然后设置while(gameOver == false)

utugiqy6

utugiqy62#

我认为问题可能在于当你调用StopCoroutine()时,你并没有指示它停止你的特定协程示例,而是需要存储一个对协程的引用,并将其作为参数传递给Stop。

IEnumerator wavecoroutine = SpawnWaves();
StartCoroutine(wavecoroutine);

...

StopCoroutine(wavecoroutine);
r8xiu3jd

r8xiu3jd3#

完全修复感谢@AlexG的指导

void Start () {
    gameOver = false;
    StartCoroutine (SpawnWaves());
}

IEnumerator SpawnWaves()
{
    yield return new WaitForSeconds(startWait);
    while (gameOver != true)
    {
        for (int i = 0; i < hazardCount; i++)
        {
            if(gameOver)break;
            GameObject enemy = enemies[Random.Range(0, enemies.Length)];
            Instantiate(enemy, spawnPosition1, spawnRotation1);
            Instantiate(enemy, spawnPosition2, spawnRotation2);
            Instantiate(enemy, spawnPosition3, spawnRotation3);
            Instantiate(enemy, spawnPosition4, spawnRotation4);
            Instantiate(enemy, spawnPosition5, spawnRotation5);
            Instantiate(enemy, spawnPosition6, spawnRotation6);
            yield return new WaitForSeconds(spawnWait);
        }
        yield return new WaitForSeconds(waveWait);
        enemies[0].GetComponent<EnemyBehviour>().currentHealth *= eenter code herenemyHealthMultiplier;
    }
}
5vf7fwbs

5vf7fwbs4#

ryeMoss实际上得到了正确的答案,你不需要改变while循环的条件,而是需要确保你传递了一个引用值给StopCoroutine方法,你可以在docs中看到这正是他们所做的。
问题是当SpawnWaves被调用时,它返回一个新的IEnumerator,这显然不是你想要的,当你试图阻止它的时候,哈哈。
只需在Start方法中更改为

gameOver = false;
waves = SpawnWaves(); <-- or whatever you want to call it
StartCoroutine(waves); <-- using a reference

然后将waves传递到StopCoroutine
始终仔细阅读文档;在学习新的库、框架等时,它们是你最好的朋友:)

yhxst69z

yhxst69z5#

这是最新的正确答案:

Coroutine CO_WriteBleDevice = null;
IEnumerator DO_WriteBleDevice()
{
    // something work
    CO_WriteBleDevice = null;
}

void StartWriteBleDevice()
{
    if (CO_WriteBleDevice == null)
        CO_WriteBleDevice = StartCoroutine(DO_WriteBleDevice());
}

void StopWriteBleDevice()
{
    if (CO_WriteBleDevice != null)
    {
        StopCoroutine(CO_WriteBleDevice);
        CO_WriteBleDevice = null;
    }
}

相关问题