如何保持协程直到条件为真 Unity

Posted

技术标签:

【中文标题】如何保持协程直到条件为真 Unity【英文标题】:How to hold on coroutine until condition is true Unity 【发布时间】:2021-10-17 21:44:56 【问题描述】:

我的代码产生了一波又一波的敌人。例如,我有 3 波。他们每个人都有5个敌人。我使用协程生成它们。但与此同时,在第一波敌人生成之后,下一波的初始化就开始了。但是第一波的敌人还没有走完他们的路线。因此,我想添加代码,以便在第一波的敌人从场景中消失之前,不会开始第二波的初始化。


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

public class EnemySpawner : MonoBehaviour

    [SerializeField] List<WaveConfig> waveConfigs; // Here is config where i'm put a few waves ( for exmaple 3 waves with 5 enemies in each one)
    [SerializeField] int startingWave = 0;
    [SerializeField] bool looping = false;



    IEnumerator Start() //start courutine
    
        do
        
            yield return StartCoroutine(SpawnAllWaves());
        
        while (looping);

    


    private IEnumerator SpawnAllWaves() //coururent to spawn Enemies waves one by one
    
        for (int waveIndex = startingWave; waveIndex < waveConfigs.Count; waveIndex++)
        
            var currentWave = waveConfigs[waveIndex];
            yield return StartCoroutine(SpawnAllEnemiesInWave(currentWave)); //call coroutine to spawn all Enemies in wave.
        

    


    private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig) //Here is spawning each Enemies in particular wave
    
        for (int enemyCount = 0; enemyCount < waveConfig.GetNumberOfEnemies(); enemyCount++)
        
            var newEnemy = Instantiate(
                waveConfig.GetEnemyPrefab(),
                waveConfig.GetWaypoints()[0].transform.position,
                Quaternion.identity); //place enemy on scene
            newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveConfig); //set them pay to move on scene
            yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns()); //wait when next enemy in wave will generate

        
    


【问题讨论】:

yield return StartCoroutine 中的SpawnAllWaves 之后,您可以在循环中让步,直到它们完成,例如while(enemiesNotFinished)yield return null; 如果你想等到它发生,你的应用程序会出现挂起。所以完全否定你为什么把它放在协程中。如果你想在完成协程时发生一些事情。为什么不把它放在最后。像“wave_spawned” 看来你要找的是WaitUntil :-) 【参考方案1】:

在你的敌人中有一个像例如这样的类

public class Enemy : MonoBehaviour

    // Keeps track of all existing (alive) instances of this script
    private static readonly HashSet<Enemy> _instances = new HashSet<Enemy>();

    // return the amount of currently living instances
    public static int Count => _instances.Count;

    private void Awake ()
    
        // Add yourself to the instances
        _instances.Add(this);
    

    private void OnDestroy ()
    
        // on death remove yourself from the instances
        _instances.Remove(this);
    

然后你就可以等待

private IEnumerator SpawnAllWaves()

    for (int waveIndex = startingWave; waveIndex < waveConfigs.Count; waveIndex++)
    
        var currentWave = waveConfigs[waveIndex];
        yield return StartCoroutine(SpawnAllEnemiesInWave(currentWave));

        // After spawning all wait until all enemies are gone again
        yield return new WaitUntil(() => Enemy.Count == 0);
    

【讨论】:

以上是关于如何保持协程直到条件为真 Unity的主要内容,如果未能解决你的问题,请参考以下文章

break-continue

Firebase Unity 插件:如何保持用户会话? (自动登录)

如何刷新页面并保持元素不刷新(持久),直到用户单击提交?

睡眠并检查直到条件为真

Android - isFinishing() 的值会在 Activity 被销毁后保持为真吗?

如何复制父对象的旋转保持自旋转,因为它在unity3d中