@@ -14,9 +14,11 @@ namespace Unity.Netcode.RuntimeTests
14
14
internal class NetworkVisibilityTests : NetcodeIntegrationTest
15
15
{
16
16
17
- protected override int NumberOfClients => 1 ;
17
+ protected override int NumberOfClients => 2 ;
18
18
private GameObject m_TestNetworkPrefab ;
19
19
private bool m_SceneManagementEnabled ;
20
+ private GameObject m_SpawnedObject ;
21
+ private NetworkManager m_SessionOwner ;
20
22
21
23
public NetworkVisibilityTests ( SceneManagementState sceneManagementState , NetworkTopologyTypes networkTopologyType ) : base ( networkTopologyType )
22
24
{
@@ -27,7 +29,11 @@ protected override void OnServerAndClientsCreated()
27
29
{
28
30
m_TestNetworkPrefab = CreateNetworkObjectPrefab ( "Object" ) ;
29
31
m_TestNetworkPrefab . AddComponent < NetworkVisibilityComponent > ( ) ;
30
- m_ServerNetworkManager . NetworkConfig . EnableSceneManagement = m_SceneManagementEnabled ;
32
+ if ( ! UseCMBService ( ) )
33
+ {
34
+ m_ServerNetworkManager . NetworkConfig . EnableSceneManagement = m_SceneManagementEnabled ;
35
+ }
36
+
31
37
foreach ( var clientNetworkManager in m_ClientNetworkManagers )
32
38
{
33
39
clientNetworkManager . NetworkConfig . EnableSceneManagement = m_SceneManagementEnabled ;
@@ -38,21 +44,58 @@ protected override void OnServerAndClientsCreated()
38
44
39
45
protected override IEnumerator OnServerAndClientsConnected ( )
40
46
{
41
- SpawnObject ( m_TestNetworkPrefab , m_ServerNetworkManager ) ;
47
+ m_SessionOwner = UseCMBService ( ) ? m_ClientNetworkManagers [ 0 ] : m_ServerNetworkManager ;
48
+ m_SpawnedObject = SpawnObject ( m_TestNetworkPrefab , m_SessionOwner ) ;
42
49
43
50
yield return base . OnServerAndClientsConnected ( ) ;
44
51
}
45
52
46
53
[ UnityTest ]
47
54
public IEnumerator HiddenObjectsTest ( )
48
55
{
56
+ var expectedCount = UseCMBService ( ) ? 2 : 3 ;
49
57
#if UNITY_2023_1_OR_NEWER
50
- yield return WaitForConditionOrTimeOut ( ( ) => Object . FindObjectsByType < NetworkVisibilityComponent > ( FindObjectsSortMode . None ) . Where ( ( c ) => c . IsSpawned ) . Count ( ) == 2 ) ;
58
+ yield return WaitForConditionOrTimeOut ( ( ) => Object . FindObjectsByType < NetworkVisibilityComponent > ( FindObjectsSortMode . None ) . Where ( ( c ) => c . IsSpawned ) . Count ( ) == expectedCount ) ;
51
59
#else
52
- yield return WaitForConditionOrTimeOut ( ( ) => Object . FindObjectsOfType < NetworkVisibilityComponent > ( ) . Where ( ( c ) => c . IsSpawned ) . Count ( ) == 2 ) ;
60
+ yield return WaitForConditionOrTimeOut ( ( ) => Object . FindObjectsOfType < NetworkVisibilityComponent > ( ) . Where ( ( c ) => c . IsSpawned ) . Count ( ) == expectedCount ) ;
53
61
#endif
54
62
55
63
Assert . IsFalse ( s_GlobalTimeoutHelper . TimedOut , "Timed out waiting for the visible object count to equal 2!" ) ;
56
64
}
65
+
66
+ [ UnityTest ]
67
+ public IEnumerator HideShowAndDeleteTest ( )
68
+ {
69
+ var expectedCount = UseCMBService ( ) ? 2 : 3 ;
70
+ #if UNITY_2023_1_OR_NEWER
71
+ yield return WaitForConditionOrTimeOut ( ( ) => Object . FindObjectsByType < NetworkVisibilityComponent > ( FindObjectsSortMode . None ) . Where ( ( c ) => c . IsSpawned ) . Count ( ) == expectedCount ) ;
72
+ #else
73
+ yield return WaitForConditionOrTimeOut ( ( ) => Object . FindObjectsOfType < NetworkVisibilityComponent > ( ) . Where ( ( c ) => c . IsSpawned ) . Count ( ) == expectedCount ) ;
74
+ #endif
75
+ AssertOnTimeout ( "Timed out waiting for the visible object count to equal 2!" ) ;
76
+
77
+ var sessionOwnerNetworkObject = m_SpawnedObject . GetComponent < NetworkObject > ( ) ;
78
+ var clientIndex = UseCMBService ( ) ? 1 : 0 ;
79
+ sessionOwnerNetworkObject . NetworkHide ( m_ClientNetworkManagers [ clientIndex ] . LocalClientId ) ;
80
+ #if UNITY_2023_1_OR_NEWER
81
+ yield return WaitForConditionOrTimeOut ( ( ) => Object . FindObjectsByType < NetworkVisibilityComponent > ( FindObjectsSortMode . None ) . Where ( ( c ) => c . IsSpawned ) . Count ( ) == expectedCount - 1 ) ;
82
+ #else
83
+ yield return WaitForConditionOrTimeOut ( ( ) => Object . FindObjectsOfType < NetworkVisibilityComponent > ( ) . Where ( ( c ) => c . IsSpawned ) . Count ( ) == expectedCount - 1 ) ;
84
+ #endif
85
+ AssertOnTimeout ( $ "Timed out waiting for { m_SpawnedObject . name } to be hidden from client!") ;
86
+ var networkObjectId = sessionOwnerNetworkObject . NetworkObjectId ;
87
+ sessionOwnerNetworkObject . NetworkShow ( m_ClientNetworkManagers [ clientIndex ] . LocalClientId ) ;
88
+ sessionOwnerNetworkObject . Despawn ( true ) ;
89
+
90
+ // Expect no exceptions
91
+ yield return s_DefaultWaitForTick ;
92
+
93
+ // Now force a scenario where it normally would have caused an exception
94
+ m_SessionOwner . SpawnManager . ObjectsToShowToClient . Add ( m_ClientNetworkManagers [ clientIndex ] . LocalClientId , new System . Collections . Generic . List < NetworkObject > ( ) ) ;
95
+ m_SessionOwner . SpawnManager . ObjectsToShowToClient [ m_ClientNetworkManagers [ clientIndex ] . LocalClientId ] . Add ( null ) ;
96
+
97
+ // Expect no exceptions
98
+ yield return s_DefaultWaitForTick ;
99
+ }
57
100
}
58
101
}
0 commit comments