@@ -16,13 +16,19 @@ public class NetworkObjectDontDestroyWithOwnerTests : NetcodeIntegrationTest
1616 protected override int NumberOfClients => 1 ;
1717
1818 protected GameObject m_PrefabToSpawn ;
19+ private GameObject m_PrefabNoObserversSpawn ;
1920
2021 public NetworkObjectDontDestroyWithOwnerTests ( HostOrServer hostOrServer ) : base ( hostOrServer ) { }
2122
2223 protected override void OnServerAndClientsCreated ( )
2324 {
2425 m_PrefabToSpawn = CreateNetworkObjectPrefab ( "ClientOwnedObject" ) ;
2526 m_PrefabToSpawn . GetComponent < NetworkObject > ( ) . DontDestroyWithOwner = true ;
27+
28+ m_PrefabNoObserversSpawn = CreateNetworkObjectPrefab ( "NoObserversObject" ) ;
29+ var prefabNoObserversNetworkObject = m_PrefabNoObserversSpawn . GetComponent < NetworkObject > ( ) ;
30+ prefabNoObserversNetworkObject . SpawnWithObservers = false ;
31+ prefabNoObserversNetworkObject . DontDestroyWithOwner = true ;
2632 }
2733
2834 [ UnityTest ]
@@ -51,5 +57,38 @@ public IEnumerator DontDestroyWithOwnerTest()
5157 Assert . That ( networkObject . OwnerClientId == m_ServerNetworkManager . LocalClientId ) ;
5258 }
5359 }
60+
61+ /// <summary>
62+ /// Validates that when a <see cref="NetworkObject"/> is spawned with no observers
63+ /// and has the <see cref="NetworkObject.DontDestroyWithOwner"/> property set, that
64+ /// upon showing the <see cref="NetworkObject"/> to a client and changing the owner
65+ /// to the client that upon the client disconnecting the <see cref="NetworkObject"/>
66+ /// continues to persist in the network session.
67+ /// </summary>
68+ /// <returns><see cref="IEnumerator"/></returns>
69+ [ UnityTest ]
70+ public IEnumerator NetworkShowThenClientDisconnects ( )
71+ {
72+ var authorityManager = m_ServerNetworkManager ;
73+ var networkObject = SpawnObject ( m_PrefabNoObserversSpawn , authorityManager ) . GetComponent < NetworkObject > ( ) ;
74+ var longWait = new WaitForSeconds ( 0.25f ) ;
75+ yield return longWait ;
76+ var nonAuthorityManager = m_ClientNetworkManagers [ 0 ] ;
77+ Assert . False ( nonAuthorityManager . SpawnManager . SpawnedObjects . ContainsKey ( networkObject . NetworkObjectId ) , $ "[Client-{ nonAuthorityManager . LocalClientId } ] " +
78+ $ "Already has an instance of { networkObject . name } when it should not!") ;
79+ networkObject . NetworkShow ( nonAuthorityManager . LocalClientId ) ;
80+ networkObject . ChangeOwnership ( nonAuthorityManager . LocalClientId ) ;
81+
82+ yield return WaitForConditionOrTimeOut ( ( ) => nonAuthorityManager . SpawnManager . SpawnedObjects . ContainsKey ( networkObject . NetworkObjectId )
83+ && nonAuthorityManager . SpawnManager . SpawnedObjects [ networkObject . NetworkObjectId ] . OwnerClientId == nonAuthorityManager . LocalClientId ) ;
84+ AssertOnTimeout ( $ "[Client-{ nonAuthorityManager . LocalClientId } ] Failed to spawn { networkObject . name } when it was shown!") ;
85+
86+ yield return s_DefaultWaitForTick ;
87+
88+ nonAuthorityManager . Shutdown ( ) ;
89+
90+ yield return longWait ;
91+ Assert . True ( networkObject . IsSpawned , $ "The spawned test prefab was despawned on the authority side when it shouldn't have been!") ;
92+ }
5493 }
5594}
0 commit comments