Skip to content

Commit 5fae671

Browse files
fix: Shutdown NetworkManager when reloading the domain
1 parent 10e01eb commit 5fae671

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

com.unity.netcode.gameobjects/CHANGELOG.md

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

2323
### Fixed
2424

25+
- `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.
2526
- 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)
2627
- Issue where NetworkRigidbodyBase was not applying rotation correctly when using Rigidbody2D. (#4012)
2728
- 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)

com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ private static void InitializeOnload()
3030
Singleton = new NetworkManagerHelper();
3131

3232
NetworkManager.NetworkManagerHelper = Singleton;
33+
34+
AssemblyReloadEvents.beforeAssemblyReload -= AssemblyReloadEvents_beforeAssemblyReload;
35+
AssemblyReloadEvents.beforeAssemblyReload += AssemblyReloadEvents_beforeAssemblyReload;
36+
3337
EditorApplication.playModeStateChanged -= EditorApplication_playModeStateChanged;
3438
EditorApplication.hierarchyChanged -= EditorApplication_hierarchyChanged;
3539

@@ -55,6 +59,23 @@ private static void InitializeOnload()
5559
};
5660
}
5761

62+
private static void AssemblyReloadEvents_beforeAssemblyReload()
63+
{
64+
if (Application.isPlaying)
65+
{
66+
#if UNITY_2023_1_OR_NEWER
67+
var networkManager = Object.FindAnyObjectByType<NetworkManager>();
68+
#else
69+
var networkManager = Object.FindObjectOfType<NetworkManager>();
70+
#endif
71+
if (networkManager != null && (networkManager.IsServer || networkManager.IsClient))
72+
{
73+
networkManager.Shutdown();
74+
networkManager.ShutdownInternal();
75+
}
76+
}
77+
}
78+
5879
private static void EditorApplication_playModeStateChanged(PlayModeStateChange playModeStateChange)
5980
{
6081
switch (playModeStateChange)

0 commit comments

Comments
 (0)