|
| 1 | +using System.Collections; |
| 2 | +using Unity.Netcode.Components; |
| 3 | +using Unity.Netcode.TestHelpers.Runtime; |
| 4 | +using UnityEngine; |
| 5 | +using UnityEngine.TestTools; |
| 6 | + |
| 7 | + |
| 8 | +namespace Unity.Netcode.RuntimeTests |
| 9 | +{ |
| 10 | + internal class NetworkTransformErrorTests : NetcodeIntegrationTest |
| 11 | + { |
| 12 | + protected override int NumberOfClients => 1; |
| 13 | + |
| 14 | + private GameObject m_AuthorityPrefab; |
| 15 | + private GameObject m_NonAuthorityPrefab; |
| 16 | + |
| 17 | + private HostAndClientPrefabHandler m_HostAndClientPrefabHandler; |
| 18 | + |
| 19 | + public class EmptyNetworkBehaviour : NetworkBehaviour { } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// PrefabHandler that tracks and separates the client GameObject from the host GameObject. |
| 23 | + /// Allows independent management of client and host game world while still instantiating NetworkObjects as expected. |
| 24 | + /// </summary> |
| 25 | + private class HostAndClientPrefabHandler : INetworkPrefabInstanceHandler |
| 26 | + { |
| 27 | + /// <summary> |
| 28 | + /// The registered prefab is the prefab the networking stack is instantiated with. |
| 29 | + /// Registering the prefab simulates the prefab that exists on the authority. |
| 30 | + /// </summary> |
| 31 | + private readonly GameObject m_RegisteredPrefab; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Mocks the registered prefab changing on the non-authority after registration. |
| 35 | + /// Allows testing situations mismatched GameObject state between the authority and non-authority. |
| 36 | + /// </summary> |
| 37 | + private readonly GameObject m_InstantiatedPrefab; |
| 38 | + |
| 39 | + public HostAndClientPrefabHandler(GameObject authorityPrefab, GameObject nonAuthorityPrefab) |
| 40 | + { |
| 41 | + m_RegisteredPrefab = authorityPrefab; |
| 42 | + m_InstantiatedPrefab = nonAuthorityPrefab; |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Returns the prefab that will mock the instantiated prefab not matching the registered prefab |
| 47 | + /// </summary> |
| 48 | + public NetworkObject Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation) |
| 49 | + { |
| 50 | + return Object.Instantiate(m_InstantiatedPrefab).GetComponent<NetworkObject>(); |
| 51 | + } |
| 52 | + |
| 53 | + public void Destroy(NetworkObject networkObject) |
| 54 | + { |
| 55 | + Object.Destroy(networkObject.gameObject); |
| 56 | + } |
| 57 | + |
| 58 | + public void Register(NetworkManager networkManager) |
| 59 | + { |
| 60 | + // Register the version that will be spawned by the authority (i.e. Host) |
| 61 | + networkManager.PrefabHandler.AddHandler(m_RegisteredPrefab, this); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Creates a GameObject and sets the transform parent to the given transform |
| 67 | + /// Adds a component of the given type to the GameObject |
| 68 | + /// </summary> |
| 69 | + private static void AddChildToNetworkObject<T>(Transform transform) where T : Component |
| 70 | + { |
| 71 | + var gameObj = new GameObject(); |
| 72 | + gameObj.transform.parent = transform; |
| 73 | + gameObj.AddComponent<T>(); |
| 74 | + } |
| 75 | + |
| 76 | + protected override void OnServerAndClientsCreated() |
| 77 | + { |
| 78 | + // Create a prefab that has many child NetworkBehaviours |
| 79 | + m_AuthorityPrefab = CreateNetworkObjectPrefab("AuthorityPrefab"); |
| 80 | + AddChildToNetworkObject<EmptyNetworkBehaviour>(m_AuthorityPrefab.transform); |
| 81 | + AddChildToNetworkObject<EmptyNetworkBehaviour>(m_AuthorityPrefab.transform); |
| 82 | + AddChildToNetworkObject<NetworkTransform>(m_AuthorityPrefab.transform); |
| 83 | + |
| 84 | + // Create a second prefab with only one NetworkBehaviour |
| 85 | + // This simulates the GameObjects on the other NetworkBehaviours being disabled |
| 86 | + m_NonAuthorityPrefab = CreateNetworkObjectPrefab("NonAuthorityPrefab"); |
| 87 | + AddChildToNetworkObject<NetworkTransform>(m_NonAuthorityPrefab.transform); |
| 88 | + |
| 89 | + // Create and register a prefab handler |
| 90 | + // The prefab handler will behave as if the GameObjects have been disabled on the non-authority client |
| 91 | + m_HostAndClientPrefabHandler = new HostAndClientPrefabHandler(m_AuthorityPrefab, m_NonAuthorityPrefab); |
| 92 | + m_HostAndClientPrefabHandler.Register(m_ServerNetworkManager); |
| 93 | + foreach (var client in m_ClientNetworkManagers) |
| 94 | + { |
| 95 | + m_HostAndClientPrefabHandler.Register(client); |
| 96 | + } |
| 97 | + |
| 98 | + base.OnServerAndClientsCreated(); |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Validates the fix where <see cref="NetworkTransformMessage"/> would throw an exception |
| 103 | + /// if a user sets a <see cref="GameObject"/> with one or more <see cref="NetworkBehaviour"/> components |
| 104 | + /// to inactive. |
| 105 | + /// </summary> |
| 106 | + [UnityTest] |
| 107 | + public IEnumerator DisabledGameObjectErrorTest() |
| 108 | + { |
| 109 | + var instance = SpawnObject(m_AuthorityPrefab, m_ServerNetworkManager); |
| 110 | + var networkObjectInstance = instance.GetComponent<NetworkObject>(); |
| 111 | + var networkTransformInstance = instance.GetComponentInChildren<NetworkTransform>(); |
| 112 | + |
| 113 | + yield return WaitForConditionOrTimeOut(() => ObjectSpawnedOnAllClients(networkObjectInstance.NetworkObjectId)); |
| 114 | + AssertOnTimeout("Timed out waiting for object to spawn!"); |
| 115 | + |
| 116 | + var errorMessage = $"[Netcode] {nameof(NetworkBehaviour)} index {networkTransformInstance.NetworkBehaviourId} was out of bounds for {m_NonAuthorityPrefab.name}(Clone). " + |
| 117 | + $"{nameof(NetworkBehaviour)}s must be the same, and in the same order, between server and client."; |
| 118 | + LogAssert.Expect(LogType.Error, errorMessage); |
| 119 | + errorMessage = $"[{nameof(NetworkTransformMessage)}][Invalid] Targeted {nameof(NetworkTransform)}, {nameof(NetworkBehaviour.NetworkBehaviourId)} " + |
| 120 | + $"({networkTransformInstance.NetworkBehaviourId}), does not exist! Make sure you are not spawning {nameof(NetworkObject)}s with disabled {nameof(GameObject)}s that have " + |
| 121 | + $"{nameof(NetworkBehaviour)} components on them."; |
| 122 | + LogAssert.Expect(LogType.Error, errorMessage); |
| 123 | + |
| 124 | + yield return new WaitForSeconds(0.3f); |
| 125 | + } |
| 126 | + |
| 127 | + private bool ObjectSpawnedOnAllClients(ulong networkObjectId) |
| 128 | + { |
| 129 | + foreach (var client in m_ClientNetworkManagers) |
| 130 | + { |
| 131 | + if (!client.SpawnManager.SpawnedObjects.ContainsKey(networkObjectId)) |
| 132 | + { |
| 133 | + return false; |
| 134 | + } |
| 135 | + } |
| 136 | + return true; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments