Skip to content

Commit 3b853e0

Browse files
fix: DestroyGameObject message changes updated for NetworkObject.NetworkHide - MTT-3552 (#1960)
* fix MTT-3552 This fixes the issue with NetworkHide not being updated to changes in DestroyObjectMessage. * test This updates the NetworkShowHideTests to verify the GameObject is indeed destroyed on the client side when hidden. It also fixes a few other potential timing issues and cleaned up some of the code to leverage from more recent updates to NetcodeIntegrationTest.
1 parent d078654 commit 3b853e0

File tree

2 files changed

+94
-81
lines changed

2 files changed

+94
-81
lines changed

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ public void NetworkHide(ulong clientId)
285285

286286
var message = new DestroyObjectMessage
287287
{
288-
NetworkObjectId = NetworkObjectId
288+
NetworkObjectId = NetworkObjectId,
289+
DestroyGameObject = true
289290
};
290291
// Send destroy call
291292
var size = NetworkManager.SendMessage(ref message, NetworkDelivery.ReliableSequenced, clientId);

com.unity.netcode.gameobjects/Tests/Runtime/NetworkShowHideTests.cs

Lines changed: 92 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,53 @@
1-
using System;
21
using System.Collections;
32
using System.Collections.Generic;
3+
using System.Linq;
44
using NUnit.Framework;
55
using UnityEngine;
66
using UnityEngine.TestTools;
77
using Unity.Netcode.TestHelpers.Runtime;
88

99
namespace Unity.Netcode.RuntimeTests
1010
{
11-
public class NetworkShowHideTest : NetworkBehaviour
11+
public class NetworkShowHideTestComponent : NetworkBehaviour
1212
{
1313

1414
}
1515

1616
public class ShowHideObject : NetworkBehaviour
1717
{
18+
public static List<ShowHideObject> ClientTargetedNetworkObjects = new List<ShowHideObject>();
19+
public static ulong ClientIdToTarget;
20+
21+
public static NetworkObject GetNetworkObjectById(ulong networkObjectId)
22+
{
23+
foreach (var entry in ClientTargetedNetworkObjects)
24+
{
25+
if (entry.NetworkObjectId == networkObjectId)
26+
{
27+
return entry.NetworkObject;
28+
}
29+
}
30+
return null;
31+
}
32+
33+
public override void OnNetworkSpawn()
34+
{
35+
if (NetworkManager.LocalClientId == ClientIdToTarget)
36+
{
37+
ClientTargetedNetworkObjects.Add(this);
38+
}
39+
base.OnNetworkSpawn();
40+
}
41+
42+
public override void OnNetworkDespawn()
43+
{
44+
if (ClientTargetedNetworkObjects.Contains(this))
45+
{
46+
ClientTargetedNetworkObjects.Remove(this);
47+
}
48+
base.OnNetworkDespawn();
49+
}
50+
1851
public NetworkVariable<int> MyNetworkVariable;
1952

2053
private void Start()
@@ -27,7 +60,6 @@ public void Changed(int before, int after)
2760
{
2861
Debug.Log($"Value changed from {before} to {after}");
2962
}
30-
3163
}
3264

3365
public class NetworkShowHideTests : NetcodeIntegrationTest
@@ -46,30 +78,17 @@ public class NetworkShowHideTests : NetcodeIntegrationTest
4678

4779
protected override void OnCreatePlayerPrefab()
4880
{
49-
var networkTransform = m_PlayerPrefab.AddComponent<NetworkShowHideTest>();
81+
var networkTransform = m_PlayerPrefab.AddComponent<NetworkShowHideTestComponent>();
5082
}
5183

5284
protected override void OnServerAndClientsCreated()
5385
{
54-
m_PrefabToSpawn = PreparePrefab(typeof(ShowHideObject));
55-
}
56-
57-
public GameObject PreparePrefab(Type type)
58-
{
59-
var prefabToSpawn = new GameObject();
60-
prefabToSpawn.AddComponent(type);
61-
var networkObjectPrefab = prefabToSpawn.AddComponent<NetworkObject>();
62-
NetcodeIntegrationTestHelpers.MakeNetworkObjectTestPrefab(networkObjectPrefab);
63-
m_ServerNetworkManager.NetworkConfig.NetworkPrefabs.Add(new NetworkPrefab() { Prefab = prefabToSpawn });
64-
foreach (var clientNetworkManager in m_ClientNetworkManagers)
65-
{
66-
clientNetworkManager.NetworkConfig.NetworkPrefabs.Add(new NetworkPrefab() { Prefab = prefabToSpawn });
67-
}
68-
return prefabToSpawn;
86+
m_PrefabToSpawn = CreateNetworkObjectPrefab("ShowHideObject");
87+
m_PrefabToSpawn.AddComponent<ShowHideObject>();
6988
}
7089

7190
// Check that the first client see them, or not, as expected
72-
private IEnumerator CheckVisible(bool target)
91+
private IEnumerator CheckVisible(bool isVisible)
7392
{
7493
int count = 0;
7594
do
@@ -83,21 +102,31 @@ private IEnumerator CheckVisible(bool target)
83102
Assert.Fail("timeout waiting for object to reach the expect visibility");
84103
break;
85104
}
86-
} while (m_NetSpawnedObject1.IsNetworkVisibleTo(m_ClientId0) != target ||
87-
m_NetSpawnedObject2.IsNetworkVisibleTo(m_ClientId0) != target ||
88-
m_NetSpawnedObject3.IsNetworkVisibleTo(m_ClientId0) != target ||
89-
m_Object1OnClient0.IsSpawned != target ||
90-
m_Object2OnClient0.IsSpawned != target ||
91-
m_Object3OnClient0.IsSpawned != target
105+
} while (m_NetSpawnedObject1.IsNetworkVisibleTo(m_ClientId0) != isVisible ||
106+
m_NetSpawnedObject2.IsNetworkVisibleTo(m_ClientId0) != isVisible ||
107+
m_NetSpawnedObject3.IsNetworkVisibleTo(m_ClientId0) != isVisible ||
108+
m_Object1OnClient0.IsSpawned != isVisible ||
109+
m_Object2OnClient0.IsSpawned != isVisible ||
110+
m_Object3OnClient0.IsSpawned != isVisible
92111
);
93112

94-
Debug.Assert(m_NetSpawnedObject1.IsNetworkVisibleTo(m_ClientId0) == target);
95-
Debug.Assert(m_NetSpawnedObject2.IsNetworkVisibleTo(m_ClientId0) == target);
96-
Debug.Assert(m_NetSpawnedObject3.IsNetworkVisibleTo(m_ClientId0) == target);
113+
Debug.Assert(m_NetSpawnedObject1.IsNetworkVisibleTo(m_ClientId0) == isVisible);
114+
Debug.Assert(m_NetSpawnedObject2.IsNetworkVisibleTo(m_ClientId0) == isVisible);
115+
Debug.Assert(m_NetSpawnedObject3.IsNetworkVisibleTo(m_ClientId0) == isVisible);
116+
117+
Debug.Assert(m_Object1OnClient0.IsSpawned == isVisible);
118+
Debug.Assert(m_Object2OnClient0.IsSpawned == isVisible);
119+
Debug.Assert(m_Object3OnClient0.IsSpawned == isVisible);
97120

98-
Debug.Assert(m_Object1OnClient0.IsSpawned == target);
99-
Debug.Assert(m_Object2OnClient0.IsSpawned == target);
100-
Debug.Assert(m_Object3OnClient0.IsSpawned == target);
121+
var clientNetworkManager = m_ClientNetworkManagers.Where((c) => c.LocalClientId == m_ClientId0).First();
122+
if (isVisible)
123+
{
124+
Assert.True(ShowHideObject.ClientTargetedNetworkObjects.Count == 3, $"Client-{clientNetworkManager.LocalClientId} should have 3 instances visible but only has {ShowHideObject.ClientTargetedNetworkObjects.Count}!");
125+
}
126+
else
127+
{
128+
Assert.True(ShowHideObject.ClientTargetedNetworkObjects.Count == 0, $"Client-{clientNetworkManager.LocalClientId} should have no visible instances but still has {ShowHideObject.ClientTargetedNetworkObjects.Count}!");
129+
}
101130
}
102131

103132
// Set the 3 objects visibility
@@ -136,59 +165,43 @@ private void Show(bool individually, bool visibility)
136165
}
137166
}
138167

139-
private IEnumerator RefreshNetworkObjects()
168+
private bool RefreshNetworkObjects()
140169
{
141-
var serverClientPlayerResult = new NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject>();
142-
yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation(
143-
x => x.NetworkObjectId == m_NetSpawnedObject1.NetworkObjectId && x.IsSpawned,
144-
m_ClientNetworkManagers[0],
145-
serverClientPlayerResult);
146-
m_Object1OnClient0 = serverClientPlayerResult.Result;
147-
yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation(
148-
x => x.NetworkObjectId == m_NetSpawnedObject2.NetworkObjectId && x.IsSpawned,
149-
m_ClientNetworkManagers[0],
150-
serverClientPlayerResult);
151-
m_Object2OnClient0 = serverClientPlayerResult.Result;
152-
serverClientPlayerResult = new NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject>();
153-
yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation(
154-
x => x.NetworkObjectId == m_NetSpawnedObject3.NetworkObjectId && x.IsSpawned,
155-
m_ClientNetworkManagers[0],
156-
serverClientPlayerResult);
157-
m_Object3OnClient0 = serverClientPlayerResult.Result;
158-
159-
// make sure the objects are set with the right network manager
160-
m_Object1OnClient0.NetworkManagerOwner = m_ClientNetworkManagers[0];
161-
m_Object2OnClient0.NetworkManagerOwner = m_ClientNetworkManagers[0];
162-
m_Object3OnClient0.NetworkManagerOwner = m_ClientNetworkManagers[0];
170+
m_Object1OnClient0 = ShowHideObject.GetNetworkObjectById(m_NetSpawnedObject1.NetworkObjectId);
171+
m_Object2OnClient0 = ShowHideObject.GetNetworkObjectById(m_NetSpawnedObject2.NetworkObjectId);
172+
m_Object3OnClient0 = ShowHideObject.GetNetworkObjectById(m_NetSpawnedObject3.NetworkObjectId);
173+
if (m_Object1OnClient0 == null || m_Object2OnClient0 == null || m_Object3OnClient0 == null)
174+
{
175+
return false;
176+
}
177+
Assert.True(m_Object1OnClient0.NetworkManagerOwner == m_ClientNetworkManagers[0]);
178+
Assert.True(m_Object2OnClient0.NetworkManagerOwner == m_ClientNetworkManagers[0]);
179+
Assert.True(m_Object3OnClient0.NetworkManagerOwner == m_ClientNetworkManagers[0]);
180+
return true;
163181
}
164182

165183

166184
[UnityTest]
167185
public IEnumerator NetworkShowHideTest()
168186
{
169187
m_ClientId0 = m_ClientNetworkManagers[0].LocalClientId;
170-
171-
// create 3 objects
188+
ShowHideObject.ClientTargetedNetworkObjects.Clear();
189+
ShowHideObject.ClientIdToTarget = m_ClientId0;
172190

173191

174-
var spawnedObject1 = UnityEngine.Object.Instantiate(m_PrefabToSpawn);
175-
var spawnedObject2 = UnityEngine.Object.Instantiate(m_PrefabToSpawn);
176-
var spawnedObject3 = UnityEngine.Object.Instantiate(m_PrefabToSpawn);
192+
// create 3 objects
193+
var spawnedObject1 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
194+
var spawnedObject2 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
195+
var spawnedObject3 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
177196
m_NetSpawnedObject1 = spawnedObject1.GetComponent<NetworkObject>();
178197
m_NetSpawnedObject2 = spawnedObject2.GetComponent<NetworkObject>();
179198
m_NetSpawnedObject3 = spawnedObject3.GetComponent<NetworkObject>();
180-
m_NetSpawnedObject1.NetworkManagerOwner = m_ServerNetworkManager;
181-
m_NetSpawnedObject2.NetworkManagerOwner = m_ServerNetworkManager;
182-
m_NetSpawnedObject3.NetworkManagerOwner = m_ServerNetworkManager;
183-
m_NetSpawnedObject1.Spawn();
184-
m_NetSpawnedObject2.Spawn();
185-
m_NetSpawnedObject3.Spawn();
186-
187199

188200
for (int mode = 0; mode < 2; mode++)
189201
{
190202
// get the NetworkObject on a client instance
191-
yield return RefreshNetworkObjects();
203+
yield return WaitForConditionOrTimeOut(RefreshNetworkObjects);
204+
AssertOnTimeout($"Could not refresh all NetworkObjects!");
192205

193206
// check object start visible
194207
yield return CheckVisible(true);
@@ -207,7 +220,8 @@ public IEnumerator NetworkShowHideTest()
207220

208221
// show them to that client
209222
Show(mode == 0, true);
210-
yield return RefreshNetworkObjects();
223+
yield return WaitForConditionOrTimeOut(RefreshNetworkObjects);
224+
AssertOnTimeout($"Could not refresh all NetworkObjects!");
211225

212226
// verify they become visible
213227
yield return CheckVisible(true);
@@ -218,24 +232,21 @@ public IEnumerator NetworkShowHideTest()
218232
public IEnumerator NetworkShowHideQuickTest()
219233
{
220234
m_ClientId0 = m_ClientNetworkManagers[0].LocalClientId;
235+
ShowHideObject.ClientTargetedNetworkObjects.Clear();
236+
ShowHideObject.ClientIdToTarget = m_ClientId0;
221237

222-
var spawnedObject1 = UnityEngine.Object.Instantiate(m_PrefabToSpawn);
223-
var spawnedObject2 = UnityEngine.Object.Instantiate(m_PrefabToSpawn);
224-
var spawnedObject3 = UnityEngine.Object.Instantiate(m_PrefabToSpawn);
238+
var spawnedObject1 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
239+
var spawnedObject2 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
240+
var spawnedObject3 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
225241
m_NetSpawnedObject1 = spawnedObject1.GetComponent<NetworkObject>();
226242
m_NetSpawnedObject2 = spawnedObject2.GetComponent<NetworkObject>();
227243
m_NetSpawnedObject3 = spawnedObject3.GetComponent<NetworkObject>();
228-
m_NetSpawnedObject1.NetworkManagerOwner = m_ServerNetworkManager;
229-
m_NetSpawnedObject2.NetworkManagerOwner = m_ServerNetworkManager;
230-
m_NetSpawnedObject3.NetworkManagerOwner = m_ServerNetworkManager;
231-
m_NetSpawnedObject1.Spawn();
232-
m_NetSpawnedObject2.Spawn();
233-
m_NetSpawnedObject3.Spawn();
234244

235245
for (int mode = 0; mode < 2; mode++)
236246
{
237247
// get the NetworkObject on a client instance
238-
yield return RefreshNetworkObjects();
248+
yield return WaitForConditionOrTimeOut(RefreshNetworkObjects);
249+
AssertOnTimeout($"Could not refresh all NetworkObjects!");
239250

240251
// check object start visible
241252
yield return CheckVisible(true);
@@ -245,7 +256,8 @@ public IEnumerator NetworkShowHideQuickTest()
245256
Show(mode == 0, true);
246257

247258
yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 5);
248-
yield return RefreshNetworkObjects();
259+
yield return WaitForConditionOrTimeOut(RefreshNetworkObjects);
260+
AssertOnTimeout($"Could not refresh all NetworkObjects!");
249261
yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 5);
250262

251263
// verify they become visible

0 commit comments

Comments
 (0)