Unity塔防類遊戲用協程實現產生怪的波數和個數
阿新 • • 發佈:2019-02-15
using UnityEngine;
using System.Collections;
public class EnemyNumHyp : MonoBehaviour
{
public GameObject enemyPrefabHyp;
// Use this for initialization
void Start()
{
StartCoroutine(Test02(10));
}
// Update is called once per frame
void Update()
{
}
//num:一共產生多少波怪
IEnumerator Test02(int num)
{
int count = 0;
while (count < num)
{
count++;
yield return StartCoroutine(Test01(5));
Debug.Log("第" + count + "怪產生結束");
//每波怪產生的時間差
yield return new WaitForSeconds(15);
Debug.Log("注意第" + (count + 1) + "波怪即將產生");
}
Debug.Log("GameOver");
}
//number:每波怪的數量
IEnumerator Test01(int number)
{
int count = 0;
while (count < number)
{
count++;
//每個怪產生的時間差
yield return new WaitForSeconds(0.5f);
//這裡克隆的位置資訊是博主需要的位置,根據情況自己設定
Instantiate(enemyPrefabHyp, new Vector3(-48,0.5f,-70f), Quaternion.identity);
}
}
}