diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index 5374068ba0..b55ceabfe1 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. (#4068) - Issue where a NullReferenceException was thrown when a non-authority failed to spawn a NetworkObject. (#4067) - 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) diff --git a/com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs b/com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs index a3224e6471..e3a12fe772 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,19 @@ private static void InitializeOnload() }; } + private static void AssemblyReloadEvents_beforeAssemblyReload() + { + if (Application.isPlaying) + { + var networkManager = NetworkManager.Singleton; + if (networkManager != null && (networkManager.IsServer || networkManager.IsClient)) + { + networkManager.Shutdown(); + networkManager.ShutdownInternal(); + } + } + } + private static void EditorApplication_playModeStateChanged(PlayModeStateChange playModeStateChange) { switch (playModeStateChange)