-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoroutine-pattern.cs
More file actions
52 lines (43 loc) · 1.34 KB
/
coroutine-pattern.cs
File metadata and controls
52 lines (43 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Coroutine Pattern (Legacy Reference)
// Coroutines are still supported but Awaitable is preferred for new code.
// Use this pattern only when maintaining existing codebases.
// See async-await-pattern.cs for the modern approach.
using System.Collections;
using UnityEngine;
namespace MyGame
{
public class CoroutineExample : MonoBehaviour
{
[SerializeField] private float _spawnInterval = 1f;
[SerializeField] private int _waveSize = 5;
private Coroutine _spawnCoroutine;
public void StartSpawning()
{
if (_spawnCoroutine != null)
StopCoroutine(_spawnCoroutine);
_spawnCoroutine = StartCoroutine(SpawnWaveRoutine());
}
public void StopSpawning()
{
if (_spawnCoroutine != null)
{
StopCoroutine(_spawnCoroutine);
_spawnCoroutine = null;
}
}
private IEnumerator SpawnWaveRoutine()
{
for (int i = 0; i < _waveSize; i++)
{
SpawnEnemy();
yield return new WaitForSeconds(_spawnInterval);
}
yield return new WaitForSeconds(3f);
StartCoroutine(SpawnWaveRoutine());
}
private void SpawnEnemy()
{
Debug.Log("Enemy spawned");
}
}
}