-
Notifications
You must be signed in to change notification settings - Fork 464
fix: distributed authority client synchronization issues #3350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
NoelStephensUnity
merged 11 commits into
develop-2.0.0
from
fix/distributed-authority-client-synchronization-updates
Mar 25, 2025
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c577a01
update
NoelStephensUnity 443a93c
fix - wip
NoelStephensUnity 8b29700
fix
NoelStephensUnity 82c3057
fix
NoelStephensUnity f6f6658
style
NoelStephensUnity 45a3715
test
NoelStephensUnity bf01a43
update
NoelStephensUnity 9e0f128
Merge branch 'develop-2.0.0' into fix/distributed-authority-client-sy…
NoelStephensUnity cb7bd9a
Update com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkS…
NoelStephensUnity 23c494d
style
NoelStephensUnity 65f73c7
Merge branch 'develop-2.0.0' into fix/distributed-authority-client-sy…
NoelStephensUnity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
...netcode.gameobjects/Tests/Runtime/DistributedAuthority/SpawnDuringSynchronizationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using NUnit.Framework; | ||
| using Unity.Netcode.TestHelpers.Runtime; | ||
| using UnityEngine; | ||
| using UnityEngine.TestTools; | ||
|
|
||
| namespace Unity.Netcode.RuntimeTests | ||
| { | ||
| [TestFixture(NetworkBehaviourSpawnTimes.OnNetworkSpawn, SceneManagement.Enabled)] | ||
| [TestFixture(NetworkBehaviourSpawnTimes.OnNetworkPostSpawn, SceneManagement.Enabled)] | ||
| [TestFixture(NetworkBehaviourSpawnTimes.OnSynchronized, SceneManagement.Enabled)] | ||
| [TestFixture(NetworkBehaviourSpawnTimes.OnNetworkSpawn, SceneManagement.Disabled)] | ||
| [TestFixture(NetworkBehaviourSpawnTimes.OnNetworkPostSpawn, SceneManagement.Disabled)] | ||
| [TestFixture(NetworkBehaviourSpawnTimes.OnSynchronized, SceneManagement.Disabled)] | ||
| internal class SpawnDuringSynchronizationTests : NetcodeIntegrationTest | ||
| { | ||
| protected override int NumberOfClients => 2; | ||
|
|
||
| private List<NetworkManager> m_AllNetworkManagers = new List<NetworkManager>(); | ||
| private StringBuilder m_ErrorLog = new StringBuilder(); | ||
|
|
||
| private NetworkBehaviourSpawnTimes m_SpawnTime; | ||
| private bool m_EnableSceneManagement; | ||
| internal enum NetworkBehaviourSpawnTimes | ||
| { | ||
| OnNetworkSpawn, | ||
| OnNetworkPostSpawn, | ||
| OnSynchronized | ||
| } | ||
|
|
||
| internal enum SceneManagement | ||
| { | ||
| Enabled, | ||
| Disabled, | ||
| } | ||
|
|
||
| internal class SpawnTestComponent : NetworkBehaviour | ||
| { | ||
| public GameObject PrefabToSpawn; | ||
| public NetworkObject SpawnedObject { get; private set; } | ||
| public NetworkBehaviourSpawnTimes SpawnTime; | ||
|
|
||
| private void SpawnObject(NetworkBehaviourSpawnTimes spawnTime) | ||
| { | ||
| if (IsOwner && SpawnTime == spawnTime) | ||
| { | ||
| SpawnedObject = NetworkObject.InstantiateAndSpawn(PrefabToSpawn, NetworkManager, OwnerClientId); | ||
| } | ||
| } | ||
|
|
||
| public override void OnNetworkSpawn() | ||
| { | ||
| SpawnObject(NetworkBehaviourSpawnTimes.OnNetworkSpawn); | ||
| base.OnNetworkSpawn(); | ||
| } | ||
|
|
||
| protected override void OnNetworkPostSpawn() | ||
| { | ||
| SpawnObject(NetworkBehaviourSpawnTimes.OnNetworkPostSpawn); | ||
| base.OnNetworkPostSpawn(); | ||
| } | ||
|
|
||
| protected override void OnNetworkSessionSynchronized() | ||
| { | ||
| SpawnObject(NetworkBehaviourSpawnTimes.OnSynchronized); | ||
| base.OnNetworkSessionSynchronized(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public SpawnDuringSynchronizationTests(NetworkBehaviourSpawnTimes spawnTime, SceneManagement sceneManagement) : base(NetworkTopologyTypes.DistributedAuthority, HostOrServer.DAHost) | ||
| { | ||
| m_SpawnTime = spawnTime; | ||
| m_EnableSceneManagement = sceneManagement == SceneManagement.Enabled; | ||
| } | ||
|
|
||
| protected override void OnCreatePlayerPrefab() | ||
| { | ||
| var spawnTestComponent = m_PlayerPrefab.AddComponent<SpawnTestComponent>(); | ||
| spawnTestComponent.SpawnTime = m_SpawnTime; | ||
| spawnTestComponent.NetworkObject.SetOwnershipStatus(NetworkObject.OwnershipStatus.None); | ||
| base.OnCreatePlayerPrefab(); | ||
| } | ||
|
|
||
| protected override void OnServerAndClientsCreated() | ||
| { | ||
| var spawnTestComponent = m_PlayerPrefab.GetComponent<SpawnTestComponent>(); | ||
| spawnTestComponent.PrefabToSpawn = CreateNetworkObjectPrefab("ObjToSpawn"); | ||
| spawnTestComponent.PrefabToSpawn.GetComponent<NetworkObject>().SetOwnershipStatus(NetworkObject.OwnershipStatus.Transferable); | ||
| if (!UseCMBService()) | ||
| { | ||
| m_ServerNetworkManager.NetworkConfig.EnableSceneManagement = m_EnableSceneManagement; | ||
| } | ||
| foreach (var networkManager in m_ClientNetworkManagers) | ||
| { | ||
| networkManager.NetworkConfig.EnableSceneManagement = m_EnableSceneManagement; | ||
| } | ||
| base.OnServerAndClientsCreated(); | ||
| } | ||
|
|
||
| private bool AllClientsSpawnedObject() | ||
| { | ||
| m_ErrorLog.Clear(); | ||
| var spawnTestComponent = (SpawnTestComponent)null; | ||
| foreach (var networkManager in m_AllNetworkManagers) | ||
| { | ||
| spawnTestComponent = networkManager.LocalClient.PlayerObject.GetComponent<SpawnTestComponent>(); | ||
| if (spawnTestComponent.SpawnedObject == null || !spawnTestComponent.SpawnedObject.IsSpawned) | ||
| { | ||
| m_ErrorLog.AppendLine($"{networkManager.name}'s player failed to spawn the network prefab!"); | ||
| break; | ||
| } | ||
| foreach (var networkManagerToCheck in m_AllNetworkManagers) | ||
| { | ||
| if (networkManagerToCheck == networkManager) | ||
| { | ||
| continue; | ||
| } | ||
| if (!networkManagerToCheck.SpawnManager.SpawnedObjects.ContainsKey(spawnTestComponent.NetworkObjectId)) | ||
| { | ||
| m_ErrorLog.AppendLine($"{networkManager.name}'s player failed to spawn the network prefab!"); | ||
| } | ||
| } | ||
| } | ||
| return m_ErrorLog.Length == 0; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Validates that a client can spawn network prefabs during OnNetworkSpawn, OnNetworkPostSpawn, and OnNetworkSessionSynchronized. | ||
| /// </summary> | ||
| [UnityTest] | ||
| public IEnumerator SpawnDuringSynchronization() | ||
| { | ||
| m_AllNetworkManagers.Clear(); | ||
| m_AllNetworkManagers.AddRange(m_ClientNetworkManagers); | ||
| if (!UseCMBService()) | ||
| { | ||
| m_AllNetworkManagers.Add(m_ServerNetworkManager); | ||
| } | ||
|
|
||
| yield return WaitForConditionOrTimeOut(AllClientsSpawnedObject); | ||
| AssertOnTimeout(m_ErrorLog.ToString()); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.