From 5fae671d18993e63305aa0dbaad6009e8c63a3b3 Mon Sep 17 00:00:00 2001 From: Simon Lemay Date: Mon, 6 Jul 2026 16:59:47 -0400 Subject: [PATCH 1/3] fix: Shutdown NetworkManager when reloading the domain --- com.unity.netcode.gameobjects/CHANGELOG.md | 1 + .../Editor/NetworkManagerHelper.cs | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index 0c5e47d1a0..b1dbeec9a8 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -22,6 +22,7 @@ Additional documentation and release notes are available at [Multiplayer Documen ### Fixed +- `NetworkManager` will now perform a proper shutdown when the domain is reloaded in play mode, instead of being left in some half-initialized state that leaks memory and socket handles. - Issue when FastBufferReader is attempting to read a string and all or a portion of the character count has already been read by user script, it could read a character length that results in a negative byte length which could result in an editor crash. (#4052) - Issue where NetworkRigidbodyBase was not applying rotation correctly when using Rigidbody2D. (#4012) - Issue where NetworkRigidbodyBase was always checking the 3D rigid body's interpolation mode when determining if it is kinematic and needs to put the rigid body to sleep and then switch to interpolation. (#4012) diff --git a/com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs b/com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs index a3224e6471..858511ddb4 100644 --- a/com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs +++ b/com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs @@ -30,6 +30,10 @@ private static void InitializeOnload() Singleton = new NetworkManagerHelper(); NetworkManager.NetworkManagerHelper = Singleton; + + AssemblyReloadEvents.beforeAssemblyReload -= AssemblyReloadEvents_beforeAssemblyReload; + AssemblyReloadEvents.beforeAssemblyReload += AssemblyReloadEvents_beforeAssemblyReload; + EditorApplication.playModeStateChanged -= EditorApplication_playModeStateChanged; EditorApplication.hierarchyChanged -= EditorApplication_hierarchyChanged; @@ -55,6 +59,23 @@ private static void InitializeOnload() }; } + private static void AssemblyReloadEvents_beforeAssemblyReload() + { + if (Application.isPlaying) + { +#if UNITY_2023_1_OR_NEWER + var networkManager = Object.FindAnyObjectByType(); +#else + var networkManager = Object.FindObjectOfType(); +#endif + if (networkManager != null && (networkManager.IsServer || networkManager.IsClient)) + { + networkManager.Shutdown(); + networkManager.ShutdownInternal(); + } + } + } + private static void EditorApplication_playModeStateChanged(PlayModeStateChange playModeStateChange) { switch (playModeStateChange) From ee90d320e333b5c6d7db472569daff40107907ca Mon Sep 17 00:00:00 2001 From: Simon Lemay Date: Mon, 6 Jul 2026 17:06:02 -0400 Subject: [PATCH 2/3] Add PR number to CHANGELOG entry --- com.unity.netcode.gameobjects/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index b1dbeec9a8..510d3e61b4 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -22,7 +22,7 @@ Additional documentation and release notes are available at [Multiplayer Documen ### Fixed -- `NetworkManager` will now perform a proper shutdown when the domain is reloaded in play mode, instead of being left in some half-initialized state that leaks memory and socket handles. +- `NetworkManager` will now perform a proper shutdown when the domain is reloaded in play mode, instead of being left in some half-initialized state that leaks memory and socket handles. (#4068) - Issue when FastBufferReader is attempting to read a string and all or a portion of the character count has already been read by user script, it could read a character length that results in a negative byte length which could result in an editor crash. (#4052) - Issue where NetworkRigidbodyBase was not applying rotation correctly when using Rigidbody2D. (#4012) - Issue where NetworkRigidbodyBase was always checking the 3D rigid body's interpolation mode when determining if it is kinematic and needs to put the rigid body to sleep and then switch to interpolation. (#4012) From 87da13fb4bc9296d69846e809d09c151289eecc1 Mon Sep 17 00:00:00 2001 From: Simon Lemay Date: Tue, 7 Jul 2026 10:57:17 -0400 Subject: [PATCH 3/3] Use the singleton instead --- .../Editor/NetworkManagerHelper.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs b/com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs index 858511ddb4..e3a12fe772 100644 --- a/com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs +++ b/com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs @@ -63,11 +63,7 @@ private static void AssemblyReloadEvents_beforeAssemblyReload() { if (Application.isPlaying) { -#if UNITY_2023_1_OR_NEWER - var networkManager = Object.FindAnyObjectByType(); -#else - var networkManager = Object.FindObjectOfType(); -#endif + var networkManager = NetworkManager.Singleton; if (networkManager != null && (networkManager.IsServer || networkManager.IsClient)) { networkManager.Shutdown();