@@ -34,22 +34,59 @@ public class NetworkSpawnManager
3434
3535 internal readonly Dictionary < ulong , NetworkObject > GhostsPendingSpawn = new Dictionary < ulong , NetworkObject > ( ) ;
3636
37- public void RegisterGhostPendingSpawn ( NetworkObject networkObject , ulong networkObjectId )
37+ internal readonly List < NetworkObject > GhostsPendingNetworkObjectId = new List < NetworkObject > ( ) ;
38+
39+ internal void CheckGhostsPendingNetworkObjectId ( )
40+ {
41+ if ( GhostsPendingNetworkObjectId . Count == 0 )
42+ {
43+ return ;
44+ }
45+
46+ for ( int i = GhostsPendingNetworkObjectId . Count - 1 ; i >= 0 ; i -- )
47+ {
48+ var networkObject = GhostsPendingNetworkObjectId [ i ] ;
49+ if ( networkObject . IsGhostNetworkObjectIdValid ( ) )
50+ {
51+ GhostsPendingNetworkObjectId . Remove ( networkObject ) ;
52+ if ( NetworkManager . LogLevel == LogLevel . Developer )
53+ {
54+ Debug . Log ( $ "[{ nameof ( RegisterGhostPendingSpawn ) } ] { networkObject . name } 's Ghost { nameof ( NetworkObject . NetworkObjectId ) } is valid. Re-registering.") ;
55+ }
56+ networkObject . RegisterGhostBridge ( ) ;
57+ }
58+ }
59+ }
60+
61+ internal void RegisterGhostPendingSpawn ( NetworkObject networkObject , ulong networkObjectId )
3862 {
63+ if ( ! networkObject . IsGhostNetworkObjectIdValid ( ) )
64+ {
65+ GhostsPendingNetworkObjectId . Add ( networkObject ) ;
66+ if ( NetworkManager . LogLevel == LogLevel . Developer )
67+ {
68+ Debug . Log ( $ "[{ nameof ( RegisterGhostPendingSpawn ) } ] { networkObject . name } 's Ghost { nameof ( NetworkObject . NetworkObjectId ) } ({ networkObjectId } ) seems invalid. Adding to the pending NetworkObjectId list.") ;
69+ }
70+ return ;
71+ }
3972 if ( NetworkManager . LogLevel == LogLevel . Developer )
4073 {
4174 Debug . Log ( $ "[{ nameof ( RegisterGhostPendingSpawn ) } ] Registering { networkObject . name } with a { nameof ( NetworkObject . NetworkObjectId ) } of { networkObjectId } .") ;
4275 }
4376 if ( GhostsPendingSpawn . TryAdd ( networkObjectId , networkObject ) )
4477 {
4578 // TODO-UNIFIED: We need a better way to preserve any hybrid instances pending NGO spawn.
46- // For now, move any pending object into the DDOL.
79+ // For now, move any pending object into the DDOL.
4780 UnityEngine . Object . DontDestroyOnLoad ( networkObject . gameObject ) ;
4881 }
4982
5083 NetworkManager . DeferredMessageManager . ProcessTriggers ( IDeferredNetworkMessageManager . TriggerType . OnGhostSpawned , networkObjectId ) ;
5184 if ( GhostsArePendingSynchronization && GhostsPendingSynchronization . ContainsKey ( networkObjectId ) )
5285 {
86+ // TODO-UNIFIED: We need a better way to preserve any hybrid instances pending NGO spawn.
87+ // NOTE: We might be able to use the NetworkSceneHandle to get the associated local scene handle to which we can use to get the targeted scene.
88+ UnityEngine . SceneManagement . SceneManager . MoveGameObjectToScene ( networkObject . gameObject , UnityEngine . SceneManagement . SceneManager . GetActiveScene ( ) ) ;
89+
5390 // When the object is spawned, it will invoke GetGhostNetworkObjectForSpawn below which removes the entry from GhostsPendingSpawn
5491 ProcessGhostPendingSynchronization ( networkObjectId ) ;
5592 }
@@ -64,6 +101,9 @@ internal NetworkObject GetGhostNetworkObjectForSpawn(ulong networkObjectId)
64101 }
65102 var networkObject = GhostsPendingSpawn [ networkObjectId ] ;
66103 GhostsPendingSpawn . Remove ( networkObjectId ) ;
104+ // TODO-UNIFIED: We need a better way to preserve any hybrid instances pending NGO spawn.
105+ // NOTE: We might be able to use the NetworkSceneHandle to get the associated local scene handle to which we can use to get the targeted scene.
106+ UnityEngine . SceneManagement . SceneManager . MoveGameObjectToScene ( networkObject . gameObject , UnityEngine . SceneManagement . SceneManager . GetActiveScene ( ) ) ;
67107 return networkObject ;
68108 }
69109
@@ -100,10 +140,6 @@ internal void ProcessGhostPendingSynchronization(ulong networkObjectId, bool rem
100140 //{
101141 // networkObject.InternalInSceneNetworkObjectsSpawned();
102142 //}
103-
104- // TODO-UNIFIED: We need a better way to preserve any hybrid instances pending NGO spawn.
105- // NOTE: We might be able to use the NetworkSceneHandle to get the associated local scene handle to which we can use to get the targeted scene.
106- UnityEngine . SceneManagement . SceneManager . MoveGameObjectToScene ( networkObject . gameObject , UnityEngine . SceneManagement . SceneManager . GetActiveScene ( ) ) ;
107143 if ( removeUponSpawn )
108144 {
109145 GhostsArePendingSynchronization = GhostsPendingSynchronization . Count > 0 ;
@@ -2020,6 +2056,12 @@ internal NetworkSpawnManager(NetworkManager networkManager)
20202056
20212057 internal void Shutdown ( )
20222058 {
2059+ #if UNIFIED_NETCODE
2060+ GhostsPendingNetworkObjectId . Clear ( ) ;
2061+ GhostsPendingNetworkObjectId . Clear ( ) ;
2062+ GhostsPendingSpawn . Clear ( ) ;
2063+ GhostsPendingSynchronization . Clear ( ) ;
2064+ #endif
20232065 NetworkObjectsToSynchronizeSceneChanges . Clear ( ) ;
20242066 CleanUpDisposedObjects . Clear ( ) ;
20252067 }
0 commit comments