Skip to content

Commit d0f73d4

Browse files
fix: Add OnPreShutdown to NetworkManager (#3358)
This PR adds a `NetworkManager.OnPreShutdown` callback that happens before the internal shutdown is done. This is to allow accessing any state that is cleaned up by the `NetworkManager` during shutdown, such as accessing dynamically spawned NetworkObjects. fix: #3145 close: #3145 ## Changelog - Added: The event NetworkManager.OnPreShutdown has been added which is called before the NetworkManager cleans up and shuts down. ## Testing and Documentation - A test has been added to ensure this is being called, and called before OnServerStopped. <!-- Uncomment and mark items off with a * if this PR deprecates any API: ### Deprecated API - [ ] An `[Obsolete]` attribute was added along with a `(RemovedAfter yyyy-mm-dd)` entry. - [ ] An [api updater] was added. - [ ] Deprecation of the API is explained in the CHANGELOG. - [ ] The users can understand why this API was removed and what they should use instead. --> --------- Co-authored-by: Noel Stephens <[email protected]>
1 parent ced4384 commit d0f73d4

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-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 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

pvpExceptions.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,7 @@
928928
"Unity.Netcode.RuntimeTests.NetworkManagerEventsTests: IEnumerator OnServerStartedCalledWhenServerStarts(): undocumented",
929929
"Unity.Netcode.RuntimeTests.NetworkManagerEventsTests: IEnumerator OnClientStartedCalledWhenClientStarts(): undocumented",
930930
"Unity.Netcode.RuntimeTests.NetworkManagerEventsTests: IEnumerator OnClientAndServerStartedCalledWhenHostStarts(): undocumented",
931+
"Unity.Netcode.RuntimeTests.NetworkManagerEventsTests: IEnumerator OnPreShutdownCalledWhenShuttingDown(): undocumented",
931932
"Unity.Netcode.RuntimeTests.NetworkManagerEventsTests: IEnumerator Teardown(): undocumented",
932933
"Unity.Netcode.RuntimeTests.NetworkManagerSceneManagerTests: undocumented",
933934
"Unity.Netcode.RuntimeTests.NetworkManagerSceneManagerTests: void SceneManagerAssigned(): undocumented",

0 commit comments

Comments
 (0)