[CreateAssetMenu(fileName = "TargetPositionData", menuName = "Custom/TargetPositionData", order = 1)] public class TargetPositionData : ScriptableObject { public List<Vector3> targetPositions = new List<Vector3>(); }
public class EnemySpawner : MonoBehaviour { [Header("Position Data")] public TargetPositionData positionData; // 위치 데이터 스크립터블 오브젝트 public GameObject spawnEnemy; public List<GameObject> SpawnEnemies() { List<GameObject> enemies = new List<GameObject>(); foreach (Vector3 position in positionData.targetPositions) { GameObject enemy = Instantiate(spawnEnemy, position, Quaternion.identity); enemy.transform.SetParent(this.transform); enemies.Add(enemy); } return enemies; } }
// 한 웨이브 적 스폰 protected void SpawnWaveEnemy() { if (currentWave < waves.Count) { for (int i = 0; i < waves.Count; i++) { foreach (EnemySpawner spawner in waves[i]) { List<GameObject> spawnedEnemies = spawner.SpawnEnemies(); foreach (GameObject enemyObject in spawnedEnemies) { Health enemyHealth = enemyObject.GetComponent<Health>(); enemyHealth.OnDie += OnEnemyDied; enemyCount++; } } } } }
public void OnEnemyDied() { enemyCount--; if (enemyCount == 0) { CheckWaveClear(); } }
// 웨이브 클리어 여부 확인 protected void CheckWaveClear() { enemyCount = 0; currentWave++; if (sectorCount < sectorDataList.Count) { if (currentWave >= waves.Count) { if (sectorDataList[sectorCount].hasEvent) { //sectorDataList[sectorCount].eventScenario.StartEventMode(); waitUntil = new WaitUntil(() => sectorDataList[sectorCount].eventScenario.isClear); StartCoroutine(WaitForEvent()); } else { OnClearSector(); currentWave = 0; sectorCount++; nextSector(); } } else { StartNextWave(); } } }
결과
이런식으로 원하는 만큼의 적을 소환할 수 있습니다.