Skip to content

Commit ca1a26d

Browse files
committed
Add OnPreShutdown and test
1 parent ced4384 commit ca1a26d

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
@@ -477,6 +477,12 @@ public struct ConnectionApprovalRequest
477477
/// </summary>
478478
public event Action OnClientStarted = null;
479479

480+
/// <summary>
481+
/// Subscribe to this static event to get notifications before a <see cref="NetworkManager"/> instance is being destroyed.
482+
/// This is useful if you want to use the state of anything the NetworkManager cleans up during its shutdown.
483+
/// </summary>
484+
public event Action OnPreShutdown = null;
485+
480486
/// <summary>
481487
/// This callback is invoked once the local server is stopped.
482488
/// </summary>
@@ -1198,6 +1204,8 @@ internal void ShutdownInternal()
11981204
NetworkLog.LogInfo(nameof(ShutdownInternal));
11991205
}
12001206

1207+
OnPreShutdown?.Invoke();
1208+
12011209
this.UnregisterAllNetworkUpdates();
12021210

12031211
// 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
@@ -237,6 +237,46 @@ public IEnumerator OnClientAndServerStartedCalledWhenHostStarts()
237237
Assert.AreEqual(2, callbacksInvoked, "either OnServerStarted or OnClientStarted wasn't invoked");
238238
}
239239

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

0 commit comments

Comments
 (0)