游戏开启之后,暂停5秒,生成3个小兵,每个小兵的生成间隔是1秒,生成完三个小兵之后,停10秒,再生成下一波
using System.Collections;
using UnityEngine;
//小兵生成器
public class SoldierSpwan : MonoBehaviour
{
//保存协程,用于稍后关闭
private Coroutine Coroutine;
public GameObject SodierPF;
private void OnEnable()
{
//开启协程
Coroutine = StartCoroutine(SpwanSolider());
}
private void OnDisable()
{
//关闭协程
StopCoroutine(Coroutine);
}
private IEnumerator SpwanSolider()
{
//暂停5秒
yield return new WaitForSeconds(5f);
while (true)
{
//一次生成三个小兵
for(int i = 0; i < 3; i++)
{
Instantiate(SodierPF, this.transform.position, Quaternion.identity);
//每个小兵的生成间隔是1秒
yield return new WaitForSeconds(1f);
}
//生成完三个小兵之后,停10秒。再生成下一波
yield return new WaitForSeconds(10f);
}
}
}