@@ -897,11 +897,22 @@ protected NetworkObject GetNetworkObject(ulong networkId)
897897 /// is in read mode or write mode.
898898 /// </typeparam>
899899 /// <param name="targetClientId">the relative client identifier being synchronized</param>
900- protected virtual void OnSynchronize < T > ( ref BufferSerializer < T > serializer , ulong targetClientId = 0 ) where T : IReaderWriter
900+ protected virtual void OnSynchronize < T > ( ref BufferSerializer < T > serializer ) where T : IReaderWriter
901901 {
902902
903903 }
904904
905+ /// <summary>
906+ /// The relative client identifier targeted for the serialization of this <see cref="NetworkBehaviour"/> instance.
907+ /// </summary>
908+ /// <remarks>
909+ /// This value will be set prior to <see cref="OnSynchronize{T}(ref BufferSerializer{T})"/> being invoked.
910+ /// For writing (server-side), this is useful to know which client will receive the serialized data.
911+ /// For reading (client-side), this will be the <see cref="NetworkManager.LocalClientId"/>.
912+ /// When synchronization of this instance is complete, this value will be reset to 0
913+ /// </remarks>
914+ protected ulong m_TargetIdBeingSynchronized { get ; private set ; }
915+
905916 /// <summary>
906917 /// Internal method that determines if a NetworkBehaviour has additional synchronization data to
907918 /// be synchronized when first instantiated prior to its associated NetworkObject being spawned.
@@ -913,6 +924,7 @@ protected virtual void OnSynchronize<T>(ref BufferSerializer<T> serializer, ulon
913924 /// <returns>true if it wrote synchronization data and false if it did not</returns>
914925 internal bool Synchronize < T > ( ref BufferSerializer < T > serializer , ulong targetClientId = 0 ) where T : IReaderWriter
915926 {
927+ m_TargetIdBeingSynchronized = targetClientId ;
916928 if ( serializer . IsWriter )
917929 {
918930 // Get the writer to handle seeking and determining how many bytes were written
@@ -931,7 +943,7 @@ internal bool Synchronize<T>(ref BufferSerializer<T> serializer, ulong targetCli
931943 var threwException = false ;
932944 try
933945 {
934- OnSynchronize ( ref serializer , targetClientId ) ;
946+ OnSynchronize ( ref serializer ) ;
935947 }
936948 catch ( Exception ex )
937949 {
@@ -947,6 +959,8 @@ internal bool Synchronize<T>(ref BufferSerializer<T> serializer, ulong targetCli
947959 }
948960 var finalPosition = writer . Position ;
949961
962+ // Reset before exiting
963+ m_TargetIdBeingSynchronized = default ;
950964 // If we wrote nothing then skip writing anything for this NetworkBehaviour
951965 if ( finalPosition == positionBeforeSynchronize || threwException )
952966 {
@@ -1000,6 +1014,9 @@ internal bool Synchronize<T>(ref BufferSerializer<T> serializer, ulong targetCli
10001014 synchronizationError = true ;
10011015 }
10021016
1017+ // Reset before exiting
1018+ m_TargetIdBeingSynchronized = default ;
1019+
10031020 // Skip over the entry if deserialization fails
10041021 if ( synchronizationError )
10051022 {
0 commit comments