1
- using System ;
2
1
using System . Collections ;
3
2
using System . Collections . Generic ;
3
+ using System . Linq ;
4
4
using NUnit . Framework ;
5
5
using UnityEngine ;
6
6
using UnityEngine . TestTools ;
7
7
using Unity . Netcode . TestHelpers . Runtime ;
8
8
9
9
namespace Unity . Netcode . RuntimeTests
10
10
{
11
- public class NetworkShowHideTest : NetworkBehaviour
11
+ public class NetworkShowHideTestComponent : NetworkBehaviour
12
12
{
13
13
14
14
}
15
15
16
16
public class ShowHideObject : NetworkBehaviour
17
17
{
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
+
18
51
public NetworkVariable < int > MyNetworkVariable ;
19
52
20
53
private void Start ( )
@@ -27,7 +60,6 @@ public void Changed(int before, int after)
27
60
{
28
61
Debug . Log ( $ "Value changed from { before } to { after } ") ;
29
62
}
30
-
31
63
}
32
64
33
65
public class NetworkShowHideTests : NetcodeIntegrationTest
@@ -46,30 +78,17 @@ public class NetworkShowHideTests : NetcodeIntegrationTest
46
78
47
79
protected override void OnCreatePlayerPrefab ( )
48
80
{
49
- var networkTransform = m_PlayerPrefab . AddComponent < NetworkShowHideTest > ( ) ;
81
+ var networkTransform = m_PlayerPrefab . AddComponent < NetworkShowHideTestComponent > ( ) ;
50
82
}
51
83
52
84
protected override void OnServerAndClientsCreated ( )
53
85
{
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 > ( ) ;
69
88
}
70
89
71
90
// Check that the first client see them, or not, as expected
72
- private IEnumerator CheckVisible ( bool target )
91
+ private IEnumerator CheckVisible ( bool isVisible )
73
92
{
74
93
int count = 0 ;
75
94
do
@@ -83,21 +102,31 @@ private IEnumerator CheckVisible(bool target)
83
102
Assert . Fail ( "timeout waiting for object to reach the expect visibility" ) ;
84
103
break ;
85
104
}
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
92
111
) ;
93
112
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 ) ;
97
120
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
+ }
101
130
}
102
131
103
132
// Set the 3 objects visibility
@@ -136,59 +165,43 @@ private void Show(bool individually, bool visibility)
136
165
}
137
166
}
138
167
139
- private IEnumerator RefreshNetworkObjects ( )
168
+ private bool RefreshNetworkObjects ( )
140
169
{
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 ;
163
181
}
164
182
165
183
166
184
[ UnityTest ]
167
185
public IEnumerator NetworkShowHideTest ( )
168
186
{
169
187
m_ClientId0 = m_ClientNetworkManagers [ 0 ] . LocalClientId ;
170
-
171
- // create 3 objects
188
+ ShowHideObject . ClientTargetedNetworkObjects . Clear ( ) ;
189
+ ShowHideObject . ClientIdToTarget = m_ClientId0 ;
172
190
173
191
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 ) ;
177
196
m_NetSpawnedObject1 = spawnedObject1 . GetComponent < NetworkObject > ( ) ;
178
197
m_NetSpawnedObject2 = spawnedObject2 . GetComponent < NetworkObject > ( ) ;
179
198
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
-
187
199
188
200
for ( int mode = 0 ; mode < 2 ; mode ++ )
189
201
{
190
202
// get the NetworkObject on a client instance
191
- yield return RefreshNetworkObjects ( ) ;
203
+ yield return WaitForConditionOrTimeOut ( RefreshNetworkObjects ) ;
204
+ AssertOnTimeout ( $ "Could not refresh all NetworkObjects!") ;
192
205
193
206
// check object start visible
194
207
yield return CheckVisible ( true ) ;
@@ -207,7 +220,8 @@ public IEnumerator NetworkShowHideTest()
207
220
208
221
// show them to that client
209
222
Show ( mode == 0 , true ) ;
210
- yield return RefreshNetworkObjects ( ) ;
223
+ yield return WaitForConditionOrTimeOut ( RefreshNetworkObjects ) ;
224
+ AssertOnTimeout ( $ "Could not refresh all NetworkObjects!") ;
211
225
212
226
// verify they become visible
213
227
yield return CheckVisible ( true ) ;
@@ -218,24 +232,21 @@ public IEnumerator NetworkShowHideTest()
218
232
public IEnumerator NetworkShowHideQuickTest ( )
219
233
{
220
234
m_ClientId0 = m_ClientNetworkManagers [ 0 ] . LocalClientId ;
235
+ ShowHideObject . ClientTargetedNetworkObjects . Clear ( ) ;
236
+ ShowHideObject . ClientIdToTarget = m_ClientId0 ;
221
237
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 ) ;
225
241
m_NetSpawnedObject1 = spawnedObject1 . GetComponent < NetworkObject > ( ) ;
226
242
m_NetSpawnedObject2 = spawnedObject2 . GetComponent < NetworkObject > ( ) ;
227
243
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 ( ) ;
234
244
235
245
for ( int mode = 0 ; mode < 2 ; mode ++ )
236
246
{
237
247
// get the NetworkObject on a client instance
238
- yield return RefreshNetworkObjects ( ) ;
248
+ yield return WaitForConditionOrTimeOut ( RefreshNetworkObjects ) ;
249
+ AssertOnTimeout ( $ "Could not refresh all NetworkObjects!") ;
239
250
240
251
// check object start visible
241
252
yield return CheckVisible ( true ) ;
@@ -245,7 +256,8 @@ public IEnumerator NetworkShowHideQuickTest()
245
256
Show ( mode == 0 , true ) ;
246
257
247
258
yield return NetcodeIntegrationTestHelpers . WaitForTicks ( m_ServerNetworkManager , 5 ) ;
248
- yield return RefreshNetworkObjects ( ) ;
259
+ yield return WaitForConditionOrTimeOut ( RefreshNetworkObjects ) ;
260
+ AssertOnTimeout ( $ "Could not refresh all NetworkObjects!") ;
249
261
yield return NetcodeIntegrationTestHelpers . WaitForTicks ( m_ServerNetworkManager , 5 ) ;
250
262
251
263
// verify they become visible
0 commit comments