@@ -897,11 +897,22 @@ protected NetworkObject GetNetworkObject(ulong networkId)
897
897
/// is in read mode or write mode.
898
898
/// </typeparam>
899
899
/// <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
901
901
{
902
902
903
903
}
904
904
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
+
905
916
/// <summary>
906
917
/// Internal method that determines if a NetworkBehaviour has additional synchronization data to
907
918
/// 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
913
924
/// <returns>true if it wrote synchronization data and false if it did not</returns>
914
925
internal bool Synchronize < T > ( ref BufferSerializer < T > serializer , ulong targetClientId = 0 ) where T : IReaderWriter
915
926
{
927
+ m_TargetIdBeingSynchronized = targetClientId ;
916
928
if ( serializer . IsWriter )
917
929
{
918
930
// 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
931
943
var threwException = false ;
932
944
try
933
945
{
934
- OnSynchronize ( ref serializer , targetClientId ) ;
946
+ OnSynchronize ( ref serializer ) ;
935
947
}
936
948
catch ( Exception ex )
937
949
{
@@ -947,6 +959,8 @@ internal bool Synchronize<T>(ref BufferSerializer<T> serializer, ulong targetCli
947
959
}
948
960
var finalPosition = writer . Position ;
949
961
962
+ // Reset before exiting
963
+ m_TargetIdBeingSynchronized = default ;
950
964
// If we wrote nothing then skip writing anything for this NetworkBehaviour
951
965
if ( finalPosition == positionBeforeSynchronize || threwException )
952
966
{
@@ -1000,6 +1014,9 @@ internal bool Synchronize<T>(ref BufferSerializer<T> serializer, ulong targetCli
1000
1014
synchronizationError = true ;
1001
1015
}
1002
1016
1017
+ // Reset before exiting
1018
+ m_TargetIdBeingSynchronized = default ;
1019
+
1003
1020
// Skip over the entry if deserialization fails
1004
1021
if ( synchronizationError )
1005
1022
{
0 commit comments