Skip to content

Commit 558fc67

Browse files
fix: scenemanager clean up ScenesLoaded after synch unloading of remaining scenes not used (#2971)
* fix This resolves an issue where if you set remaining loaded scenes to be unloaded after synchronization the synchronized scenes loaded list is not updated which provides a false positive of a scene being loaded and synchronized. * fix Fixing issue with 6000.0.11f1 changes to velocity and isKinematic. * fix Missed one spot where Rigidbody2D.velocity was being used. Noticed a bug where it was using Rigidbody2D.velocity as opposed to Rigidbody2D.angularVelocity. * test adding validation test for PR fixes * update adding changelog entries.
1 parent ee0e045 commit 558fc67

File tree

7 files changed

+59
-13
lines changed

7 files changed

+59
-13
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Additional documentation and release notes are available at [Multiplayer Documen
1212

1313
### Fixed
1414

15+
- Fixed issue where `NetworkManager.ScenesLoaded` was not being updated if `PostSynchronizationSceneUnloading` was set and any loaded scenes not used during synchronization were unloaded. (#2971)
16+
- Fixed issue where `Rigidbody2d` under Unity 6000.0.11f1 has breaking changes where `velocity` is now `linearVelocity` and `isKinematic` is replaced by `bodyType`. (#2971)
1517
- Fixed issue where `NetworkSpawnManager.InstantiateAndSpawn` and `NetworkObject.InstantiateAndSpawn` were not honoring the ownerClientId parameter when using a client-server network topology. (#2968)
1618
- Fixed issue where internal delta serialization could not have a byte serializer defined when serializing deltas for other types. Added `[GenerateSerializationForType(typeof(byte))]` to both the `NetworkVariable` and `AnticipatedNetworkVariable` classes to assure a byte serializer is defined.(#2962)
1719
- Fixed issue when scene management was disabled and the session owner would still try to synchronize a late joining client. (#2962)

com.unity.netcode.gameobjects/Runtime/Components/NetworkRigidBodyBase.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ public void SetLinearVelocity(Vector3 linearVelocity)
177177
{
178178
if (m_IsRigidbody2D)
179179
{
180+
#if COM_UNITY_MODULES_PHYSICS2D_LINEAR
181+
m_Rigidbody2D.linearVelocity = linearVelocity;
182+
#else
180183
m_Rigidbody2D.velocity = linearVelocity;
184+
#endif
181185
}
182186
else
183187
{
@@ -197,7 +201,11 @@ public Vector3 GetLinearVelocity()
197201
{
198202
if (m_IsRigidbody2D)
199203
{
204+
#if COM_UNITY_MODULES_PHYSICS2D_LINEAR
205+
return m_Rigidbody2D.linearVelocity;
206+
#else
200207
return m_Rigidbody2D.velocity;
208+
#endif
201209
}
202210
else
203211
{
@@ -238,7 +246,7 @@ public Vector3 GetAngularVelocity()
238246
{
239247
if (m_IsRigidbody2D)
240248
{
241-
return Vector3.forward * m_Rigidbody2D.velocity;
249+
return Vector3.forward * m_Rigidbody2D.angularVelocity;
242250
}
243251
else
244252
{
@@ -481,7 +489,7 @@ public bool IsKinematic()
481489
{
482490
if (m_IsRigidbody2D)
483491
{
484-
return m_Rigidbody2D.isKinematic;
492+
return m_Rigidbody2D.bodyType == RigidbodyType2D.Kinematic;
485493
}
486494
else
487495
{
@@ -510,7 +518,7 @@ public void SetIsKinematic(bool isKinematic)
510518
{
511519
if (m_IsRigidbody2D)
512520
{
513-
m_Rigidbody2D.isKinematic = isKinematic;
521+
m_Rigidbody2D.bodyType = isKinematic ? RigidbodyType2D.Kinematic : RigidbodyType2D.Dynamic;
514522
}
515523
else
516524
{
@@ -715,7 +723,11 @@ private void ApplyFixedJoint2D(NetworkRigidbodyBase bodyToConnect, Vector3 posit
715723

716724
if (zeroVelocity)
717725
{
726+
#if COM_UNITY_MODULES_PHYSICS2D_LINEAR
727+
m_Rigidbody2D.linearVelocity = Vector2.zero;
728+
#else
718729
m_Rigidbody2D.velocity = Vector2.zero;
730+
#endif
719731
m_Rigidbody2D.angularVelocity = 0.0f;
720732
}
721733

com.unity.netcode.gameobjects/Runtime/SceneManagement/DefaultSceneManagerHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ public void UnloadUnassignedScenes(NetworkManager networkManager = null)
227227
foreach (var sceneToUnload in m_ScenesToUnload)
228228
{
229229
SceneManager.UnloadSceneAsync(sceneToUnload);
230+
// Update the ScenesLoaded when we unload scenes
231+
if (sceneManager.ScenesLoaded.ContainsKey(sceneToUnload.handle))
232+
{
233+
sceneManager.ScenesLoaded.Remove(sceneToUnload.handle);
234+
}
230235
}
231236
}
232237

com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,16 +2406,6 @@ private void HandleClientSceneEvent(uint sceneEventId)
24062406
NetworkManager.ConnectionManager.CreateAndSpawnPlayer(NetworkManager.LocalClientId);
24072407
}
24082408

2409-
// Client is now synchronized and fully "connected". This also means the client can send "RPCs" at this time
2410-
NetworkManager.ConnectionManager.InvokeOnClientConnectedCallback(NetworkManager.LocalClientId);
2411-
2412-
// Notify the client that they have finished synchronizing
2413-
OnSceneEvent?.Invoke(new SceneEvent()
2414-
{
2415-
SceneEventType = sceneEventData.SceneEventType,
2416-
ClientId = NetworkManager.LocalClientId, // Client sent this to the server
2417-
});
2418-
24192409
// Process any SceneEventType.ObjectSceneChanged messages that
24202410
// were deferred while synchronizing and migrate the associated
24212411
// NetworkObjects to their newly assigned scenes.
@@ -2429,6 +2419,16 @@ private void HandleClientSceneEvent(uint sceneEventId)
24292419
SceneManagerHandler.UnloadUnassignedScenes(NetworkManager);
24302420
}
24312421

2422+
// Client is now synchronized and fully "connected". This also means the client can send "RPCs" at this time
2423+
NetworkManager.ConnectionManager.InvokeOnClientConnectedCallback(NetworkManager.LocalClientId);
2424+
2425+
// Notify the client that they have finished synchronizing
2426+
OnSceneEvent?.Invoke(new SceneEvent()
2427+
{
2428+
SceneEventType = sceneEventData.SceneEventType,
2429+
ClientId = NetworkManager.LocalClientId, // Client sent this to the server
2430+
});
2431+
24322432
OnSynchronizeComplete?.Invoke(NetworkManager.LocalClientId);
24332433

24342434
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)

com.unity.netcode.gameobjects/Runtime/com.unity.netcode.runtime.asmdef

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@
7272
"name": "com.unity.services.multiplayer",
7373
"expression": "0.2.0",
7474
"define": "MULTIPLAYER_SERVICES_SDK_INSTALLED"
75+
},
76+
{
77+
"name": "Unity",
78+
"expression": "6000.0.11f1",
79+
"define": "COM_UNITY_MODULES_PHYSICS2D_LINEAR"
7580
}
7681
],
7782
"noEngineReferences": false

com.unity.netcode.gameobjects/TestHelpers/Runtime/IntegrationTestSceneHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,11 @@ public void UnloadUnassignedScenes(NetworkManager networkManager = null)
677677
foreach (var sceneToUnload in m_ScenesToUnload)
678678
{
679679
SceneManager.UnloadSceneAsync(sceneToUnload.Key);
680+
// Update the ScenesLoaded when we unload scenes
681+
if (sceneManager.ScenesLoaded.ContainsKey(sceneToUnload.Key.handle))
682+
{
683+
sceneManager.ScenesLoaded.Remove(sceneToUnload.Key.handle);
684+
}
680685
}
681686
}
682687

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,28 @@ public IEnumerator SceneVerifyBeforeLoadTest()
361361
}
362362

363363
m_IsTestingVerifyScene = false;
364+
var currentSceneName = m_CurrentScene;
364365
Assert.AreEqual(m_ServerNetworkManager.SceneManager.UnloadScene(m_CurrentScene), SceneEventProgressStatus.Started);
365366

366367
// Now wait for scenes to unload
367368
yield return WaitForConditionOrTimeOut(ConditionPassed);
368369
AssertOnTimeout($"Timed out waiting for all clients to unload {m_CurrentSceneName}!\n{PrintFailedCondition()}");
370+
371+
// Verify that all NetworkSceneManager instances reflect the change in scenes synchronized
372+
var scenesSynchronized = m_ServerNetworkManager.SceneManager.GetSynchronizedScenes();
373+
foreach (var scene in scenesSynchronized)
374+
{
375+
Assert.False(scene.name.Equals(currentSceneName), $"Host still thinks scene {currentSceneName} is loaded and synchronized!");
376+
}
377+
378+
foreach (var client in m_ClientNetworkManagers)
379+
{
380+
scenesSynchronized = client.SceneManager.GetSynchronizedScenes();
381+
foreach (var scene in scenesSynchronized)
382+
{
383+
Assert.False(scene.name.Equals(currentSceneName), $"Client-{client.LocalClientId} still thinks scene {currentSceneName} is loaded and synchronized!");
384+
}
385+
}
369386
}
370387
}
371388
}

0 commit comments

Comments
 (0)