-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathNetworkManagerMonitor.cs
More file actions
30 lines (29 loc) · 983 Bytes
/
NetworkManagerMonitor.cs
File metadata and controls
30 lines (29 loc) · 983 Bytes
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
using UnityEngine;
using Unity.Netcode;
/// <summary>
/// This can be added to the same GameObject the NetworkManager component is assigned to in order to prevent
/// multiple NetworkManager instances from being instantiated if the same scene is loaded.
/// </summary>
public class NetworkManagerMonitor : MonoBehaviour
{
// Start is called before the first frame update
private void Start()
{
#if UNITY_2023_1_OR_NEWER
var networkManagerInstances = FindObjectsByType<NetworkManager>(FindObjectsSortMode.InstanceID);
#else
var networkManagerInstances = FindObjectsOfType<NetworkManager>();
#endif
foreach (var instance in networkManagerInstances)
{
if (instance.IsListening)
{
if (gameObject != instance.gameObject)
{
var networkManager = GetComponent<NetworkManager>();
Destroy(gameObject);
}
}
}
}
}