You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity:Boss标签敌人存在时停止敌波生成的实现方案咨询

敌波生成暂停/恢复实现方案

需求说明

现有Unity敌人生成脚本,需要实现:

  • 当场景中存在标签为"Boss"的敌人时,立刻停止所有敌波生成流程
  • 当该Boss敌人被销毁后,自动恢复敌波生成

问题分析

直接停止协程会丢失当前生成进度,无法后续恢复;仅在协程单次循环中检测,会导致已进入等待的步骤无法中断。正确做法是在协程的所有等待节点加入暂停检测,让生成流程在暂停时挂起,直到条件解除。

实现代码修改

修改后的完整脚本如下:

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

public class EnemySpawner : MonoBehaviour
{
    [SerializeField] List<WaveConfigSO> waveConfigs; // 存储敌波顺序
    [SerializeField] float timeBetweenWaves = 0f;
    WaveConfigSO currentWave;
    [SerializeField] bool isLooping;

    // 控制生成暂停的开关
    private bool isSpawningPaused = false;
    // Boss标签常量,方便后续修改
    private const string BossTag = "Boss";

    void Start()
    {
        StartCoroutine(SpawnEnemyWaves());
    }

    void Update()
    {
        // 实时检测场景中是否存在Boss,更新暂停状态
        isSpawningPaused = GameObject.FindGameObjectWithTag(BossTag) != null;
    }

    public WaveConfigSO GetCurrentWave()
    {
        return currentWave;
    }

    IEnumerator SpawnEnemyWaves()
    {
        do
        {
            foreach (WaveConfigSO wave in waveConfigs)
            {
                currentWave = wave;

                for (int i = 0; i < currentWave.GetEnemyCount(); i++)
                {
                    // 暂停检测:若处于暂停状态,循环等待直到解除
                    yield return new WaitUntil(() => !isSpawningPaused);

                    Instantiate(currentWave.GetEnemyPrefab(0), 
                                currentWave.GetStartingWaypoint().position, 
                                Quaternion.Euler(0, 0, 180), 
                                transform);
                    
                    // 生成敌人后,等待下一次生成前再次检测暂停
                    yield return new WaitForSeconds(currentWave.GetRandomSpawnTime());
                }

                // 波次间隔前检测暂停
                yield return new WaitUntil(() => !isSpawningPaused);
                yield return new WaitForSeconds(timeBetweenWaves);
            }
        }
        while (isLooping);
    }
}

关键逻辑说明

  1. 暂停状态控制:新增isSpawningPaused变量,通过Update方法实时检测场景中Boss的存在,自动更新暂停状态。
  2. 协程挂起逻辑:在三个关键节点加入yield return new WaitUntil(() => !isSpawningPaused):
    • 生成每个敌人之前
    • 波次之间的间隔等待之前
      一旦Boss出现,生成流程会立刻在最近的等待节点挂起,直到Boss被销毁。
  3. 性能优化(可选):如果场景对象较多,每帧调用GameObject.FindGameObjectWithTag可能影响性能,可改为事件驱动方式:
    • 在Boss脚本中添加销毁事件:
      public static event Action OnBossDestroyed;
      void OnDestroy()
      {
          OnBossDestroyed?.Invoke();
      }
      
    • 在EnemySpawner中监听事件并初始化检测:
      void Start()
      {
          isSpawningPaused = GameObject.FindGameObjectWithTag(BossTag) != null;
          Boss.OnBossDestroyed += () => isSpawningPaused = false;
          StartCoroutine(SpawnEnemyWaves());
      }
      
      void OnDisable()
      {
          Boss.OnBossDestroyed -= () => isSpawningPaused = false;
      }
      
    这种方式避免了每帧检测,性能更优。

内容的提问来源于stack exchange,提问作者Weranjana De Silva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 14:13:13