Skip to content

Commit dfef393

Browse files
fix: [NGOv2.x] Add OnPreShutdown to NetworkManager (#3366)
Forwardport of #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. ## 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 120cdcc commit dfef393

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1010

1111
### Added
1212

13+
- Added `NetworkManager.OnPreShutdown` which is called before the NetworkManager cleans up and shuts down. (#3366)
1314
- Added interpolator types as an inspector view selection for position, rotation, and scale. (#3337)
1415
- Added a new smooth dampening interpolator type that provides a nice balance between precision and smoothing results. (#3337)
1516
- Added `NetworkTimeSystem.TickLatency` property that provides the average latency of a client. (#3337)

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)