Repository navigation
fix: respawn hybrid NetworkObject on NetworkShow after NetworkHide #4236
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 2 commits into
develop-3.x.x
from
fix/hybrid-hide-show-respawn
Oct 30, 2026
Merged
Changes from all commits
Commits
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
152 changes: 152 additions & 0 deletions
152
com.unity.netcode.gameobjects/Tests/Runtime/NetworkObject/HybridNetworkShowHideTests.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,152 @@ | ||
| #if UNIFIED_NETCODE | ||
| using System; | ||
| using System.Collections; | ||
| using NUnit.Framework; | ||
| using Unity.Netcode.Components; | ||
| using Unity.Netcode.TestHelpers.Runtime; | ||
| using UnityEngine; | ||
| using UnityEngine.TestTools; | ||
|
|
||
| namespace Unity.Netcode.RuntimeTests | ||
| { | ||
| /// <summary> | ||
| /// Validates that hiding and then showing a spawned hybrid instance respawns it on the client. | ||
| /// </summary> | ||
| [TestFixture(HostOrServer.UnifiedHost)] | ||
| [TestFixture(HostOrServer.UnifiedServer)] | ||
| internal class HybridNetworkShowHideTests : NetcodeIntegrationTest | ||
| { | ||
| protected override int NumberOfClients => 1; | ||
|
|
||
| private GameObject m_HybridPrefab; | ||
| private GameObject m_AttachablePrefab; | ||
| private GameObject m_AttachableNodePrefab; | ||
| private ulong m_NetworkObjectId; | ||
|
|
||
| public HybridNetworkShowHideTests(HostOrServer hostOrServer) : base(hostOrServer) { } | ||
|
|
||
| protected override bool UseUnifiedTests() | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| protected override void OnServerAndClientsCreated() | ||
| { | ||
| m_HybridPrefab = CreateNetworkObjectPrefab("HybridShowHide"); | ||
| m_AttachablePrefab = CreateNetworkObjectPrefab("HybridAttachable"); | ||
| var attachableChild = new GameObject("AttachableChild"); | ||
| attachableChild.transform.parent = m_AttachablePrefab.transform; | ||
| attachableChild.AddComponent<AttachableBehaviour>(); | ||
| m_AttachableNodePrefab = CreateNetworkObjectPrefab("HybridAttachableNode"); | ||
| var nodeChild = new GameObject("NodeChild"); | ||
| nodeChild.transform.parent = m_AttachableNodePrefab.transform; | ||
| nodeChild.AddComponent<AttachableNode>(); | ||
| base.OnServerAndClientsCreated(); | ||
| } | ||
|
|
||
| private bool IsSpawnedOnClient() | ||
| { | ||
| return GetNonAuthorityNetworkManager().SpawnManager.SpawnedObjects.ContainsKey(m_NetworkObjectId); | ||
| } | ||
|
|
||
| private IEnumerator SpawnOnAll(GameObject prefab, Action<NetworkObject> onSpawned) | ||
| { | ||
| var authorityInstance = SpawnObject(prefab, GetAuthorityNetworkManager()).GetComponent<NetworkObject>(); | ||
| yield return WaitForSpawnedOnAllOrTimeOut(authorityInstance); | ||
| AssertOnTimeout($"[Setup] {authorityInstance.name} was not spawned on all instances!"); | ||
| onSpawned(authorityInstance); | ||
| } | ||
|
|
||
| private IEnumerator HideThenShow(NetworkObject authorityInstance, string context) | ||
| { | ||
| var nonAuthority = GetNonAuthorityNetworkManager(); | ||
| m_NetworkObjectId = authorityInstance.NetworkObjectId; | ||
| authorityInstance.NetworkHide(nonAuthority.LocalClientId); | ||
| yield return WaitForConditionOrTimeOut(() => !IsSpawnedOnClient()); | ||
| AssertOnTimeout($"[{context}] {authorityInstance.name} was not despawned on Client-{nonAuthority.LocalClientId}!"); | ||
|
|
||
| authorityInstance.NetworkShow(nonAuthority.LocalClientId); | ||
| yield return WaitForConditionOrTimeOut(IsSpawnedOnClient); | ||
| AssertOnTimeout($"[{context}] {authorityInstance.name} was not respawned on Client-{nonAuthority.LocalClientId}!"); | ||
| } | ||
|
|
||
| [UnityTest] | ||
| public IEnumerator HideThenShowRespawnsOnClient() | ||
| { | ||
| var nonAuthority = GetNonAuthorityNetworkManager(); | ||
| NetworkObject authorityInstance = null; | ||
| yield return SpawnOnAll(m_HybridPrefab, spawned => authorityInstance = spawned); | ||
| var clientInstance = nonAuthority.SpawnManager.SpawnedObjects[authorityInstance.NetworkObjectId]; | ||
|
|
||
| // A second cycle shows the respawned instance can be hidden and shown again. | ||
| for (int i = 0; i < 2; i++) | ||
| { | ||
| yield return HideThenShow(authorityInstance, $"Cycle {i}"); | ||
| Assert.AreSame(clientInstance, nonAuthority.SpawnManager.SpawnedObjects[m_NetworkObjectId], $"[Cycle {i}] Client-{nonAuthority.LocalClientId} did not respawn the instance its ghost kept alive!"); | ||
| } | ||
| } | ||
|
|
||
| [UnityTest] | ||
| public IEnumerator HideThenShowParentedChild() | ||
| { | ||
| var nonAuthority = GetNonAuthorityNetworkManager(); | ||
| NetworkObject parent = null; | ||
| NetworkObject child = null; | ||
| yield return SpawnOnAll(m_HybridPrefab, spawned => parent = spawned); | ||
| yield return SpawnOnAll(m_HybridPrefab, spawned => child = spawned); | ||
| var clientParent = nonAuthority.SpawnManager.SpawnedObjects[parent.NetworkObjectId].transform; | ||
| var clientChild = nonAuthority.SpawnManager.SpawnedObjects[child.NetworkObjectId].transform; | ||
| Assert.IsTrue(child.TrySetParent(parent, false), $"{nameof(NetworkObject.TrySetParent)} failed!"); | ||
| yield return WaitForConditionOrTimeOut(() => clientChild.parent == clientParent); | ||
| AssertOnTimeout($"[Setup] {child.name} was not parented on Client-{nonAuthority.LocalClientId}!"); | ||
|
|
||
| yield return HideThenShow(child, "Parented"); | ||
| Assert.AreSame(clientParent, clientChild.parent, $"Client-{nonAuthority.LocalClientId} did not keep {child.name} parented!"); | ||
| } | ||
|
|
||
| [UnityTest] | ||
| public IEnumerator HideThenShowThenAttachAndDetach() | ||
| { | ||
| var nonAuthority = GetNonAuthorityNetworkManager(); | ||
| NetworkObject source = null; | ||
| NetworkObject node = null; | ||
| yield return SpawnOnAll(m_AttachablePrefab, spawned => source = spawned); | ||
| yield return SpawnOnAll(m_AttachableNodePrefab, spawned => node = spawned); | ||
| yield return HideThenShow(source, "Attachable"); | ||
|
|
||
| var clientSource = nonAuthority.SpawnManager.SpawnedObjects[source.NetworkObjectId].transform; | ||
| var clientAttachable = clientSource.GetComponentInChildren<AttachableBehaviour>().transform; | ||
| var clientNode = nonAuthority.SpawnManager.SpawnedObjects[node.NetworkObjectId].GetComponentInChildren<AttachableNode>().transform; | ||
| var attachable = source.GetComponentInChildren<AttachableBehaviour>(); | ||
|
|
||
| attachable.Attach(node.GetComponentInChildren<AttachableNode>()); | ||
| yield return WaitForConditionOrTimeOut(() => clientAttachable.parent == clientNode); | ||
| AssertOnTimeout($"Client-{nonAuthority.LocalClientId} did not attach {clientAttachable.name}!"); | ||
|
|
||
| attachable.Detach(); | ||
| yield return WaitForConditionOrTimeOut(() => clientAttachable.parent == clientSource); | ||
| AssertOnTimeout($"Client-{nonAuthority.LocalClientId} did not detach {clientAttachable.name}!"); | ||
| } | ||
|
|
||
| [UnityTest] | ||
| public IEnumerator DespawnWhileHiddenReleasesClientInstance() | ||
| { | ||
| var nonAuthority = GetNonAuthorityNetworkManager(); | ||
| NetworkObject authorityInstance = null; | ||
| yield return SpawnOnAll(m_HybridPrefab, spawned => authorityInstance = spawned); | ||
| m_NetworkObjectId = authorityInstance.NetworkObjectId; | ||
| var clientInstance = nonAuthority.SpawnManager.SpawnedObjects[m_NetworkObjectId]; | ||
|
|
||
| authorityInstance.NetworkHide(nonAuthority.LocalClientId); | ||
| yield return WaitForConditionOrTimeOut(() => !IsSpawnedOnClient()); | ||
| AssertOnTimeout($"[Setup] {authorityInstance.name} was not despawned on Client-{nonAuthority.LocalClientId}!"); | ||
| Assert.IsTrue(nonAuthority.SpawnManager.GhostSpawnManager.IsGhostPendingSpawn(m_NetworkObjectId), $"Client-{nonAuthority.LocalClientId} is not holding the hidden instance for a later show!"); | ||
|
|
||
| authorityInstance.Despawn(); | ||
| yield return WaitForConditionOrTimeOut(() => clientInstance == null); | ||
| AssertOnTimeout($"N4E did not destroy the hidden instance on Client-{nonAuthority.LocalClientId}!"); | ||
| Assert.IsFalse(nonAuthority.SpawnManager.GhostSpawnManager.IsGhostPendingSpawn(m_NetworkObjectId), $"Client-{nonAuthority.LocalClientId} still holds the destroyed instance as pending!"); | ||
| } | ||
| } | ||
| } | ||
| #endif |
2 changes: 2 additions & 0 deletions
2
com.unity.netcode.gameobjects/Tests/Runtime/NetworkObject/HybridNetworkShowHideTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.