|
| 1 | +#if UNIFIED_NETCODE |
| 2 | +using System; |
| 3 | +using System.Collections; |
| 4 | +using NUnit.Framework; |
| 5 | +using Unity.Netcode.Components; |
| 6 | +using Unity.Netcode.TestHelpers.Runtime; |
| 7 | +using UnityEngine; |
| 8 | +using UnityEngine.TestTools; |
| 9 | + |
| 10 | +namespace Unity.Netcode.RuntimeTests |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Validates that hiding and then showing a spawned hybrid instance respawns it on the client. |
| 14 | + /// </summary> |
| 15 | + [TestFixture(HostOrServer.UnifiedHost)] |
| 16 | + [TestFixture(HostOrServer.UnifiedServer)] |
| 17 | + internal class HybridNetworkShowHideTests : NetcodeIntegrationTest |
| 18 | + { |
| 19 | + protected override int NumberOfClients => 1; |
| 20 | + |
| 21 | + private GameObject m_HybridPrefab; |
| 22 | + private GameObject m_AttachablePrefab; |
| 23 | + private GameObject m_AttachableNodePrefab; |
| 24 | + private ulong m_NetworkObjectId; |
| 25 | + |
| 26 | + public HybridNetworkShowHideTests(HostOrServer hostOrServer) : base(hostOrServer) { } |
| 27 | + |
| 28 | + protected override bool UseUnifiedTests() |
| 29 | + { |
| 30 | + return true; |
| 31 | + } |
| 32 | + |
| 33 | + protected override void OnServerAndClientsCreated() |
| 34 | + { |
| 35 | + m_HybridPrefab = CreateNetworkObjectPrefab("HybridShowHide"); |
| 36 | + m_AttachablePrefab = CreateNetworkObjectPrefab("HybridAttachable"); |
| 37 | + var attachableChild = new GameObject("AttachableChild"); |
| 38 | + attachableChild.transform.parent = m_AttachablePrefab.transform; |
| 39 | + attachableChild.AddComponent<AttachableBehaviour>(); |
| 40 | + m_AttachableNodePrefab = CreateNetworkObjectPrefab("HybridAttachableNode"); |
| 41 | + var nodeChild = new GameObject("NodeChild"); |
| 42 | + nodeChild.transform.parent = m_AttachableNodePrefab.transform; |
| 43 | + nodeChild.AddComponent<AttachableNode>(); |
| 44 | + base.OnServerAndClientsCreated(); |
| 45 | + } |
| 46 | + |
| 47 | + private bool IsSpawnedOnClient() |
| 48 | + { |
| 49 | + return GetNonAuthorityNetworkManager().SpawnManager.SpawnedObjects.ContainsKey(m_NetworkObjectId); |
| 50 | + } |
| 51 | + |
| 52 | + private IEnumerator SpawnOnAll(GameObject prefab, Action<NetworkObject> onSpawned) |
| 53 | + { |
| 54 | + var authorityInstance = SpawnObject(prefab, GetAuthorityNetworkManager()).GetComponent<NetworkObject>(); |
| 55 | + yield return WaitForSpawnedOnAllOrTimeOut(authorityInstance); |
| 56 | + AssertOnTimeout($"[Setup] {authorityInstance.name} was not spawned on all instances!"); |
| 57 | + onSpawned(authorityInstance); |
| 58 | + } |
| 59 | + |
| 60 | + private IEnumerator HideThenShow(NetworkObject authorityInstance, string context) |
| 61 | + { |
| 62 | + var nonAuthority = GetNonAuthorityNetworkManager(); |
| 63 | + m_NetworkObjectId = authorityInstance.NetworkObjectId; |
| 64 | + authorityInstance.NetworkHide(nonAuthority.LocalClientId); |
| 65 | + yield return WaitForConditionOrTimeOut(() => !IsSpawnedOnClient()); |
| 66 | + AssertOnTimeout($"[{context}] {authorityInstance.name} was not despawned on Client-{nonAuthority.LocalClientId}!"); |
| 67 | + |
| 68 | + authorityInstance.NetworkShow(nonAuthority.LocalClientId); |
| 69 | + yield return WaitForConditionOrTimeOut(IsSpawnedOnClient); |
| 70 | + AssertOnTimeout($"[{context}] {authorityInstance.name} was not respawned on Client-{nonAuthority.LocalClientId}!"); |
| 71 | + } |
| 72 | + |
| 73 | + [UnityTest] |
| 74 | + public IEnumerator HideThenShowRespawnsOnClient() |
| 75 | + { |
| 76 | + var nonAuthority = GetNonAuthorityNetworkManager(); |
| 77 | + NetworkObject authorityInstance = null; |
| 78 | + yield return SpawnOnAll(m_HybridPrefab, spawned => authorityInstance = spawned); |
| 79 | + var clientInstance = nonAuthority.SpawnManager.SpawnedObjects[authorityInstance.NetworkObjectId]; |
| 80 | + |
| 81 | + // A second cycle shows the respawned instance can be hidden and shown again. |
| 82 | + for (int i = 0; i < 2; i++) |
| 83 | + { |
| 84 | + yield return HideThenShow(authorityInstance, $"Cycle {i}"); |
| 85 | + Assert.AreSame(clientInstance, nonAuthority.SpawnManager.SpawnedObjects[m_NetworkObjectId], $"[Cycle {i}] Client-{nonAuthority.LocalClientId} did not respawn the instance its ghost kept alive!"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + [UnityTest] |
| 90 | + public IEnumerator HideThenShowParentedChild() |
| 91 | + { |
| 92 | + var nonAuthority = GetNonAuthorityNetworkManager(); |
| 93 | + NetworkObject parent = null; |
| 94 | + NetworkObject child = null; |
| 95 | + yield return SpawnOnAll(m_HybridPrefab, spawned => parent = spawned); |
| 96 | + yield return SpawnOnAll(m_HybridPrefab, spawned => child = spawned); |
| 97 | + var clientParent = nonAuthority.SpawnManager.SpawnedObjects[parent.NetworkObjectId].transform; |
| 98 | + var clientChild = nonAuthority.SpawnManager.SpawnedObjects[child.NetworkObjectId].transform; |
| 99 | + Assert.IsTrue(child.TrySetParent(parent, false), $"{nameof(NetworkObject.TrySetParent)} failed!"); |
| 100 | + yield return WaitForConditionOrTimeOut(() => clientChild.parent == clientParent); |
| 101 | + AssertOnTimeout($"[Setup] {child.name} was not parented on Client-{nonAuthority.LocalClientId}!"); |
| 102 | + |
| 103 | + yield return HideThenShow(child, "Parented"); |
| 104 | + Assert.AreSame(clientParent, clientChild.parent, $"Client-{nonAuthority.LocalClientId} did not keep {child.name} parented!"); |
| 105 | + } |
| 106 | + |
| 107 | + [UnityTest] |
| 108 | + public IEnumerator HideThenShowThenAttachAndDetach() |
| 109 | + { |
| 110 | + var nonAuthority = GetNonAuthorityNetworkManager(); |
| 111 | + NetworkObject source = null; |
| 112 | + NetworkObject node = null; |
| 113 | + yield return SpawnOnAll(m_AttachablePrefab, spawned => source = spawned); |
| 114 | + yield return SpawnOnAll(m_AttachableNodePrefab, spawned => node = spawned); |
| 115 | + yield return HideThenShow(source, "Attachable"); |
| 116 | + |
| 117 | + var clientSource = nonAuthority.SpawnManager.SpawnedObjects[source.NetworkObjectId].transform; |
| 118 | + var clientAttachable = clientSource.GetComponentInChildren<AttachableBehaviour>().transform; |
| 119 | + var clientNode = nonAuthority.SpawnManager.SpawnedObjects[node.NetworkObjectId].GetComponentInChildren<AttachableNode>().transform; |
| 120 | + var attachable = source.GetComponentInChildren<AttachableBehaviour>(); |
| 121 | + |
| 122 | + attachable.Attach(node.GetComponentInChildren<AttachableNode>()); |
| 123 | + yield return WaitForConditionOrTimeOut(() => clientAttachable.parent == clientNode); |
| 124 | + AssertOnTimeout($"Client-{nonAuthority.LocalClientId} did not attach {clientAttachable.name}!"); |
| 125 | + |
| 126 | + attachable.Detach(); |
| 127 | + yield return WaitForConditionOrTimeOut(() => clientAttachable.parent == clientSource); |
| 128 | + AssertOnTimeout($"Client-{nonAuthority.LocalClientId} did not detach {clientAttachable.name}!"); |
| 129 | + } |
| 130 | + |
| 131 | + [UnityTest] |
| 132 | + public IEnumerator DespawnWhileHiddenReleasesClientInstance() |
| 133 | + { |
| 134 | + var nonAuthority = GetNonAuthorityNetworkManager(); |
| 135 | + NetworkObject authorityInstance = null; |
| 136 | + yield return SpawnOnAll(m_HybridPrefab, spawned => authorityInstance = spawned); |
| 137 | + m_NetworkObjectId = authorityInstance.NetworkObjectId; |
| 138 | + var clientInstance = nonAuthority.SpawnManager.SpawnedObjects[m_NetworkObjectId]; |
| 139 | + |
| 140 | + authorityInstance.NetworkHide(nonAuthority.LocalClientId); |
| 141 | + yield return WaitForConditionOrTimeOut(() => !IsSpawnedOnClient()); |
| 142 | + AssertOnTimeout($"[Setup] {authorityInstance.name} was not despawned on Client-{nonAuthority.LocalClientId}!"); |
| 143 | + Assert.IsTrue(nonAuthority.SpawnManager.GhostSpawnManager.IsGhostPendingSpawn(m_NetworkObjectId), $"Client-{nonAuthority.LocalClientId} is not holding the hidden instance for a later show!"); |
| 144 | + |
| 145 | + authorityInstance.Despawn(); |
| 146 | + yield return WaitForConditionOrTimeOut(() => clientInstance == null); |
| 147 | + AssertOnTimeout($"N4E did not destroy the hidden instance on Client-{nonAuthority.LocalClientId}!"); |
| 148 | + Assert.IsFalse(nonAuthority.SpawnManager.GhostSpawnManager.IsGhostPendingSpawn(m_NetworkObjectId), $"Client-{nonAuthority.LocalClientId} still holds the destroyed instance as pending!"); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | +#endif |
0 commit comments