Skip to content

Commit 977bb1c

Browse files
committed
Added OnPreShutdown event to NetworkManager
1 parent c754a2c commit 977bb1c

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,12 @@ public struct ConnectionApprovalRequest
807807
/// </summary>
808808
public event Action OnClientStarted = null;
809809

810+
/// <summary>
811+
/// Subscribe to this event to get notifications before a <see cref="NetworkManager"/> instance is being destroyed.
812+
/// This is useful if you want to use the state of anything the NetworkManager cleans up during its shutdown.
813+
/// </summary>
814+
public event Action OnPreShutdown = null;
815+
810816
/// <summary>
811817
/// This callback is invoked once the local server is stopped.
812818
/// </summary>
@@ -1481,6 +1487,8 @@ internal void ShutdownInternal()
14811487
NetworkLog.LogInfo(nameof(ShutdownInternal));
14821488
}
14831489

1490+
OnPreShutdown?.Invoke();
1491+
14841492
this.UnregisterAllNetworkUpdates();
14851493

14861494
// Everything is shutdown in the order of their dependencies

com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerEventsTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,46 @@ public IEnumerator OnClientAndServerStartedCalledWhenHostStarts()
236236
Assert.AreEqual(2, callbacksInvoked, "either OnServerStarted or OnClientStarted wasn't invoked");
237237
}
238238

239+
[UnityTest]
240+
public IEnumerator OnPreShutdownCalledWhenShuttingDown()
241+
{
242+
bool preShutdownInvoked = false;
243+
bool shutdownInvoked = false;
244+
var gameObject = new GameObject(nameof(OnPreShutdownCalledWhenShuttingDown));
245+
m_ServerManager = gameObject.AddComponent<NetworkManager>();
246+
247+
// Set dummy transport that does nothing
248+
var transport = gameObject.AddComponent<DummyTransport>();
249+
m_ServerManager.NetworkConfig = new NetworkConfig() { NetworkTransport = transport };
250+
251+
Action onPreShutdown = () =>
252+
{
253+
preShutdownInvoked = true;
254+
Assert.IsFalse(shutdownInvoked, "OnPreShutdown was invoked after OnServerStopped");
255+
};
256+
257+
Action<bool> onServerStopped = (bool wasAlsoClient) =>
258+
{
259+
shutdownInvoked = true;
260+
Assert.IsTrue(preShutdownInvoked, "OnPreShutdown wasn't invoked before OnServerStopped");
261+
};
262+
263+
// Start server to cause initialization process
264+
Assert.True(m_ServerManager.StartServer());
265+
Assert.True(m_ServerManager.IsListening);
266+
267+
m_ServerManager.OnPreShutdown += onPreShutdown;
268+
m_ServerManager.OnServerStopped += onServerStopped;
269+
m_ServerManager.Shutdown();
270+
Object.DestroyImmediate(gameObject);
271+
272+
yield return WaitUntilManagerShutsdown();
273+
274+
Assert.False(m_ServerManager.IsListening);
275+
Assert.True(preShutdownInvoked, "OnPreShutdown wasn't invoked");
276+
Assert.True(shutdownInvoked, "OnServerStopped wasn't invoked");
277+
}
278+
239279
private IEnumerator WaitUntilManagerShutsdown()
240280
{
241281
/* Need two updates to actually shut down. First one to see the transport failing, which

0 commit comments

Comments
 (0)