-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathShutdownWhenNoClients.cs
More file actions
67 lines (58 loc) · 2.09 KB
/
ShutdownWhenNoClients.cs
File metadata and controls
67 lines (58 loc) · 2.09 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
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.Collections;
using Unity.Netcode;
using UnityEngine;
public class ShutdownWhenNoClients : MonoBehaviour
{
[Tooltip("When enabled, the DGS will wait for the defined ShutdownWaitTime period of time and if no new clients connected will shutdown")]
public bool WaitBeforeShutdown = true;
[Tooltip("The period of time a server will wait after the last client has disconnected before completely shutting itself down.")]
public float ShutdownWaitTime = 4.0f;
private NetworkManager m_NetworkManager;
private void Awake()
{
m_NetworkManager = GetComponent<NetworkManager>();
if (m_NetworkManager == null)
{
Debug.LogError($"No {nameof(NetworkManager)} found on {name}! This component should be placed on the same {nameof(GameObject)} as the {nameof(NetworkManager)}!");
// Disable until resolved
gameObject.SetActive(false);
}
else
{
m_NetworkManager.OnServerStarted += OnServerStarted;
m_NetworkManager.OnServerStopped += OnServerStopped;
}
}
private void OnServerStarted()
{
m_NetworkManager.OnClientDisconnectCallback += OnClientDisconnectCallback;
}
private void OnServerStopped(bool obj)
{
m_NetworkManager.OnClientDisconnectCallback -= OnClientDisconnectCallback;
}
private void OnClientDisconnectCallback(ulong clientId)
{
if (m_NetworkManager.ConnectedClientsList.Count == 1 && m_NetworkManager.ConnectedClients.ContainsKey(clientId))
{
if (WaitBeforeShutdown)
{
StartCoroutine(WaitForShutdown());
}
else
{
m_NetworkManager.Shutdown();
}
}
}
private IEnumerator WaitForShutdown()
{
yield return new WaitForSeconds(ShutdownWaitTime);
// Make sure no clients have connected while waiting to shutdown
if (m_NetworkManager.ConnectedClients.Count == 0)
{
// If none then shut down
m_NetworkManager.Shutdown();
}
}
}