22using System . Collections . Generic ;
33using System . Linq ;
44using System . Runtime . CompilerServices ;
5- using Unity . Collections ;
65using Unity . Netcode . Components ;
76#if UNITY_EDITOR
87using UnityEditor ;
@@ -46,13 +45,6 @@ public sealed class NetworkObject : MonoBehaviour
4645 [ SerializeField ]
4746 internal uint InScenePlacedSourceGlobalObjectIdHash ;
4847
49- /// <summary>
50- /// Metadata sent during the instantiation process.
51- /// Retrieved in INetworkCustomSpawnDataSynchronizer before instantiation,
52- /// and available to INetworkPrefabInstanceHandler.Instantiate() for custom handling by user code.
53- /// </summary>
54- internal FastBufferReader InstantiationPayload ;
55-
5648 /// <summary>
5749 /// Gets the Prefab Hash Id of this object if the object is registerd as a prefab otherwise it returns 0
5850 /// </summary>
@@ -1819,8 +1811,6 @@ internal void SpawnInternal(bool destroyWithScene, ulong ownerClientId, bool pla
18191811 }
18201812 }
18211813
1822- NetworkManager . PrefabHandler . InjectInstantiationPayload ( this ) ;
1823-
18241814 NetworkManager . SpawnManager . SpawnNetworkObjectLocally ( this , NetworkManager . SpawnManager . GetNetworkObjectId ( ) , IsSceneObject . HasValue && IsSceneObject . Value , playerObject , ownerClientId , destroyWithScene ) ;
18251815
18261816 if ( ( NetworkManager . DistributedAuthorityMode && NetworkManager . DAHost ) || ( ! NetworkManager . DistributedAuthorityMode && NetworkManager . IsServer ) )
@@ -2822,7 +2812,6 @@ internal struct SceneObject
28222812 public ulong NetworkObjectId ;
28232813 public ulong OwnerClientId ;
28242814 public ushort OwnershipFlags ;
2825- public FastBufferReader InstantiationPayload ;
28262815
28272816 public bool IsPlayerObject
28282817 {
@@ -2965,26 +2954,12 @@ public void Serialize(FastBufferWriter writer)
29652954 var writeSize = 0 ;
29662955 writeSize += HasTransform ? FastBufferWriter . GetWriteSize < TransformData > ( ) : 0 ;
29672956 writeSize += FastBufferWriter . GetWriteSize < int > ( ) ;
2968- if ( HasInstantiationPayload )
2969- {
2970- writeSize += FastBufferWriter . GetWriteSize < int > ( ) ;
2971- writeSize += InstantiationPayload . Length ;
2972- }
29732957
29742958 if ( ! writer . TryBeginWrite ( writeSize ) )
29752959 {
29762960 throw new OverflowException ( "Could not serialize SceneObject: Out of buffer space." ) ;
29772961 }
29782962
2979- if ( HasInstantiationPayload )
2980- {
2981- writer . WriteValueSafe ( InstantiationPayload . Length ) ;
2982- unsafe
2983- {
2984- writer . WriteBytes ( InstantiationPayload . GetUnsafePtr ( ) , InstantiationPayload . Length ) ;
2985- }
2986- }
2987-
29882963 if ( HasTransform )
29892964 {
29902965 writer . WriteValue ( Transform ) ;
@@ -3001,8 +2976,15 @@ public void Serialize(FastBufferWriter writer)
30012976 writer . WriteValue ( OwnerObject . GetSceneOriginHandle ( ) ) ;
30022977 }
30032978
3004- // Synchronize NetworkVariables and NetworkBehaviours
2979+ // Synchronize Payload, NetworkVariables and NetworkBehaviours
30052980 var bufferSerializer = new BufferSerializer < BufferSerializerWriter > ( new BufferSerializerWriter ( writer ) ) ;
2981+
2982+ if ( HasInstantiationPayload )
2983+ {
2984+ if ( NetworkManager . Singleton . PrefabHandler . TryGetPayloadSynchronizer ( Hash , out INetworkInstantiationPayloadSynchronizer synchronizer ) )
2985+ synchronizer . OnSynchronize ( ref bufferSerializer ) ;
2986+ }
2987+
30062988 OwnerObject . SynchronizeNetworkBehaviours ( ref bufferSerializer , TargetClientId ) ;
30072989 }
30082990
@@ -3045,34 +3027,12 @@ public void Deserialize(FastBufferReader reader)
30453027 readSize += HasTransform ? FastBufferWriter . GetWriteSize < TransformData > ( ) : 0 ;
30463028 readSize += FastBufferWriter . GetWriteSize < int > ( ) ;
30473029
3048- int preInstanceDataSize = 0 ;
3049- if ( HasInstantiationPayload )
3050- {
3051- if ( ! reader . TryBeginRead ( FastBufferWriter . GetWriteSize < int > ( ) ) )
3052- {
3053- throw new OverflowException ( $ "Could not deserialize SceneObject: Reading past the end of the buffer ({ nameof ( InstantiationPayload ) } size)") ;
3054- }
3055-
3056- reader . ReadValueSafe ( out preInstanceDataSize ) ;
3057- readSize += FastBufferWriter . GetWriteSize < int > ( ) ;
3058- readSize += preInstanceDataSize ;
3059- }
3060-
30613030 // Try to begin reading the remaining bytes
30623031 if ( ! reader . TryBeginRead ( readSize ) )
30633032 {
30643033 throw new OverflowException ( "Could not deserialize SceneObject: Reading past the end of the buffer" ) ;
30653034 }
30663035
3067- if ( HasInstantiationPayload )
3068- {
3069- unsafe
3070- {
3071- InstantiationPayload = new FastBufferReader ( reader . GetUnsafePtrAtCurrentPosition ( ) , Allocator . Persistent , preInstanceDataSize ) ;
3072- reader . Seek ( reader . Position + preInstanceDataSize ) ;
3073- }
3074- }
3075-
30763036 if ( HasTransform )
30773037 {
30783038 reader . ReadValue ( out Transform ) ;
@@ -3081,6 +3041,15 @@ public void Deserialize(FastBufferReader reader)
30813041 // The NetworkSceneHandle is the server-side relative
30823042 // scene handle that the NetworkObject resides in.
30833043 reader . ReadValue ( out NetworkSceneHandle ) ;
3044+
3045+ if ( HasInstantiationPayload )
3046+ {
3047+ if ( NetworkManager . Singleton . PrefabHandler . TryGetPayloadSynchronizer ( Hash , out INetworkInstantiationPayloadSynchronizer synchronizer ) )
3048+ {
3049+ var instantiationPayloadBufferReader = new BufferSerializer < BufferSerializerReader > ( new BufferSerializerReader ( reader ) ) ;
3050+ synchronizer . OnSynchronize ( ref instantiationPayloadBufferReader ) ;
3051+ }
3052+ }
30843053 }
30853054 }
30863055
@@ -3202,8 +3171,7 @@ internal SceneObject GetMessageSceneObject(ulong targetClientId = NetworkManager
32023171 Hash = CheckForGlobalObjectIdHashOverride ( ) ,
32033172 OwnerObject = this ,
32043173 TargetClientId = targetClientId ,
3205- HasInstantiationPayload = InstantiationPayload . IsInitialized ,
3206- InstantiationPayload = InstantiationPayload
3174+ HasInstantiationPayload = NetworkManager . PrefabHandler . HasPayloadSynchronizer ( GlobalObjectIdHash ) ,
32073175 } ;
32083176
32093177 // Handle Parenting
@@ -3298,11 +3266,6 @@ internal static NetworkObject AddSceneObject(in SceneObject sceneObject, FastBuf
32983266 // in order to be able to determine which NetworkVariables the client will be allowed to read.
32993267 networkObject . OwnerClientId = sceneObject . OwnerClientId ;
33003268
3301- // Even though the Instantiation Payload is typically consumed during the spawn message handling phase,
3302- // we still assign it here to preserve the original spawn metadata for potential inspection, diagnostics,
3303- // or in case future systems want to access it directly without relying on synchronization messages.
3304- networkObject . InstantiationPayload = sceneObject . InstantiationPayload ;
3305-
33063269 // Special Case: Invoke NetworkBehaviour.OnPreSpawn methods here before SynchronizeNetworkBehaviours
33073270 networkObject . InvokeBehaviourNetworkPreSpawn ( ) ;
33083271
0 commit comments