forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawner.cs
More file actions
54 lines (49 loc) · 1.26 KB
/
Spawner.cs
File metadata and controls
54 lines (49 loc) · 1.26 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
53
54
using System.Collections;
using Unity.Netcode;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject Prefab;
public float Frequency;
[Range(1, 100)]
public int MaxInstances = 40;
private void OnServerStarted()
{
StartCoroutine(SpawnRoutine());
}
private IEnumerator SpawnRoutine()
{
var instanceCount = 0;
while (true)
{
var go = Instantiate(Prefab, transform.position, Quaternion.identity);
var networkObject = go.GetComponent<NetworkObject>();
networkObject.Spawn();
instanceCount++;
// If frequency == 0 then it is a single spawner
if (Frequency > 0)
{
yield return new WaitForSeconds(Frequency);
if (instanceCount >= MaxInstances)
{
break;
}
}
else
{
break;
}
}
}
private void Start()
{
NetworkManager.Singleton.OnServerStarted += OnServerStarted;
}
private void OnDestroy()
{
if (NetworkManager.Singleton != null)
{
NetworkManager.Singleton.OnServerStarted -= OnServerStarted;
}
}
}