Skip to content

Commit 1644801

Browse files
fix: In-scene placed NetworkObjects getting destroyed if early disconnect (#2923)
* fix Fixes an issue where in-scene placed NetworkObjects could be destroyed if a connection failed. * test Adding test to validate fix
1 parent 2b38d55 commit 1644801

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

+3-10
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,18 @@ Additional documentation and release notes are available at [Multiplayer Documen
99

1010
### Added
1111

12-
### Fixed
13-
14-
- Fixed issue where an in-scene placed `NetworkObject` with `NetworkTransform` that is also parented under a `GameObject` would not properly synchronize when the parent `GameObject` had a world space position other than 0,0,0. (#2895)
15-
16-
### Changed
17-
18-
## [Unreleased]
19-
20-
### Added
21-
2212
- Added `NetworkBehaviour.OnNetworkPreSpawn` and `NetworkBehaviour.OnNetworkPostSpawn` methods that provide the ability to handle pre and post spawning actions during the `NetworkObject` spawn sequence. (#2906)
2313
- Added a client-side only `NetworkBehaviour.OnNetworkSessionSynchronized` convenience method that is invoked on all `NetworkBehaviour`s after a newly joined client has finished synchronizing with the network session in progress. (#2906)
2414
- Added `NetworkBehaviour.OnInSceneObjectsSpawned` convenience method that is invoked when all in-scene `NetworkObject`s have been spawned after a scene has been loaded or upon a host or server starting. (#2906)
2515

2616
### Fixed
2717

18+
- Fixed issue where in-scene placed NetworkObjects could be destroyed if a client disconnects early and/or before approval. (#2923)
2819
- Fixed issue where `NetworkDeltaPosition` would "jitter" periodically if both unreliable delta state updates and half-floats were used together. (#2922)
2920
- Fixed issue where `NetworkRigidbody2D` would not properly change body type based on the instance's authority when spawned. (#2916)
3021
- Fixed issue where a `NetworkObject` component's associated `NetworkBehaviour` components would not be detected if scene loading is disabled in the editor and the currently loaded scene has in-scene placed `NetworkObject`s. (#2906)
22+
- Fixed issue where an in-scene placed `NetworkObject` with `NetworkTransform` that is also parented under a `GameObject` would not properly synchronize when the parent `GameObject` had a world space position other than 0,0,0. (#2895)
23+
3124

3225
### Changed
3326

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ internal void DespawnAndDestroyNetworkObjects()
885885
{
886886
// If it is an in-scene placed NetworkObject then just despawn and let it be destroyed when the scene
887887
// is unloaded. Otherwise, despawn and destroy it.
888-
var shouldDestroy = !(networkObjects[i].IsSceneObject != null && networkObjects[i].IsSceneObject.Value);
888+
var shouldDestroy = !(networkObjects[i].IsSceneObject == null || (networkObjects[i].IsSceneObject != null && networkObjects[i].IsSceneObject.Value));
889889

890890
// If we are going to destroy this NetworkObject, check for any in-scene placed children that need to be removed
891891
if (shouldDestroy)

testproject/Assets/Tests/Runtime/NetworkSceneManager/InScenePlacedNetworkObjectTests.cs

+48
Original file line numberDiff line numberDiff line change
@@ -574,4 +574,52 @@ protected bool ScaleValuesMatch(Transform transformA, Transform transformB)
574574
}
575575

576576
}
577+
578+
internal class InScenePlacedNetworkObjectClientTests : NetcodeIntegrationTest
579+
{
580+
private const string k_SceneToLoad = "InSceneNetworkObject";
581+
582+
protected override int NumberOfClients => 0;
583+
584+
private Scene m_Scene;
585+
586+
protected override IEnumerator OnSetup()
587+
{
588+
SceneManager.sceneLoaded += SceneManager_sceneLoaded;
589+
SceneManager.LoadScene(k_SceneToLoad, LoadSceneMode.Additive);
590+
return base.OnSetup();
591+
}
592+
593+
private void SceneManager_sceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
594+
{
595+
if (scene.name == k_SceneToLoad && loadSceneMode == LoadSceneMode.Additive)
596+
{
597+
m_Scene = scene;
598+
SceneManager.sceneLoaded -= SceneManager_sceneLoaded;
599+
}
600+
}
601+
602+
protected override IEnumerator OnTearDown()
603+
{
604+
if (m_Scene.isLoaded)
605+
{
606+
SceneManager.UnloadSceneAsync(m_Scene);
607+
}
608+
return base.OnTearDown();
609+
}
610+
611+
[UnityTest]
612+
public IEnumerator DespawnAndDestroyNetworkObjects()
613+
{
614+
// Simulate a client disconnecting early by just invoking DespawnAndDestroyNetworkObjects to assure
615+
// this method does not destroy in-scene placed NetworkObjects.
616+
m_ServerNetworkManager.SpawnManager.DespawnAndDestroyNetworkObjects();
617+
618+
yield return s_DefaultWaitForTick;
619+
620+
var insceneObject = GameObject.Find("InSceneObject");
621+
Assert.IsNotNull(insceneObject, $"Could not find the in-scene placed {nameof(NetworkObject)}: InSceneObject!");
622+
}
623+
624+
}
577625
}

0 commit comments

Comments
 (0)