Skip to content

Commit 7f6c9c7

Browse files
committed
Test updated
1 parent 096c85c commit 7f6c9c7

1 file changed

Lines changed: 16 additions & 15 deletions

File tree

com.unity.netcode.gameobjects/Tests/Runtime/Prefabs/NetworkPrefabHandlerWithDataTests.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,23 @@ public IEnumerator InstantiationPayload_SyncsCorrectly()
8686
yield return NetcodeIntegrationTestHelpers.WaitForClientsConnectedToServer(server, clients.Length + 1, null, 512);
8787

8888
//Sets the values to synchronize
89-
server_handler.networksSerializableToSynchronize = new NetworkSerializableTest() { Value = 12, Value2 = 3.14f };
89+
var instantiationData = new NetworkSerializableTest() { Value = 12, Value2 = 3.14f };
9090

9191
// Spawn the prefab on the server
92-
var spawned = server.SpawnManager.InstantiateAndSpawn(_prefab.GetComponent<NetworkObject>());
93-
Assert.NotNull(spawned);
92+
var instance = GameObject.Instantiate<NetworkObject>(_prefab.GetComponent<NetworkObject>());
93+
instance.InjectInstantiationData(instantiationData);
94+
instance.Spawn();
95+
Assert.NotNull(instance);
9496

9597
// wait for the clients to receive the instantiation payload
9698
var timeoutHelper = new TimeoutHelper();
97-
yield return NetcodeIntegrationTest.WaitForConditionOrTimeOut(() => client_handlers.All(handler => (handler.networksSerializableToSynchronize.Value == server_handler.networksSerializableToSynchronize.Value)&& (handler.networksSerializableToSynchronize.Value2 == server_handler.networksSerializableToSynchronize.Value2)));
99+
yield return NetcodeIntegrationTest.WaitForConditionOrTimeOut(() => client_handlers.All(handler => handler.networksSerializableToSynchronize.IsSynchronizedWith(instantiationData)));
98100
Assert.False(timeoutHelper.TimedOut, "Did not successfully sync all handlers");
99101

100102
// Check that the values are synchronized
101103
for (int i = 0; i < client_handlers.Length; i++)
102104
{
103-
Assert.IsTrue(client_handlers[i].IsSynchronizedWith(server_handler), "Client handler " + i + " is not synchronized with server handler");
105+
Assert.IsTrue(client_handlers[i].networksSerializableToSynchronize.IsSynchronizedWith(instantiationData), "Client handler " + i + " is not synchronized with server handler");
104106
}
105107
}
106108

@@ -114,6 +116,7 @@ public PrefabInstanceHandlerWithData(GameObject prefab)
114116
}
115117
public NetworkObject Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation, NetworkSerializableTest data)
116118
{
119+
Debug.Log($"Instantiating {Prefab.name} with data: {data.Value}, {data.Value2}");
117120
networksSerializableToSynchronize = data;
118121
var instance = GameObject.Instantiate(Prefab, position, rotation).GetComponent<NetworkObject>();
119122
return instance;
@@ -124,16 +127,6 @@ public void Destroy(NetworkObject networkObject)
124127
GameObject.DestroyImmediate(networkObject.gameObject);
125128
}
126129

127-
public bool IsSynchronizedWith(PrefabInstanceHandlerWithData other)
128-
{
129-
if (other == null)
130-
return false;
131-
132-
bool isSynchronized = true;
133-
isSynchronized &= networksSerializableToSynchronize.Value == other.networksSerializableToSynchronize.Value;
134-
isSynchronized &= networksSerializableToSynchronize.Value2 == other.networksSerializableToSynchronize.Value2;
135-
return isSynchronized;
136-
}
137130
}
138131

139132
struct NetworkSerializableTest : INetworkSerializable
@@ -145,6 +138,14 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
145138
serializer.SerializeValue(ref Value);
146139
serializer.SerializeValue(ref Value2);
147140
}
141+
142+
public bool IsSynchronizedWith(NetworkSerializableTest other)
143+
{
144+
bool isSynchronized = true;
145+
isSynchronized &= Value == other.Value;
146+
isSynchronized &= Value2 == other.Value2;
147+
return isSynchronized;
148+
}
148149
}
149150
}
150151
}

0 commit comments

Comments
 (0)