Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Additional documentation and release notes are available at [Multiplayer Documen


### Changed

- Improve performance of `NetworkTransform`. (#3907)

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1621,10 +1621,11 @@ internal bool SynchronizeScale
public bool CanCommitToTransform { get; protected set; }

/// <summary>
/// Internally used by <see cref="NetworkTransform"/> to keep track of the <see cref="NetworkManager"/> instance assigned to this
/// Internally used by <see cref="NetworkTransform"/> to keep track of the <see cref="NetworkManager"/> instance assigned to
/// this <see cref="NetworkBehaviour"/> derived class instance.
/// </summary>
protected NetworkManager m_CachedNetworkManager;
private int m_CachedTickRate;

/// <summary>
/// Helper method that returns the space relative position of the transform.
Expand Down Expand Up @@ -2136,9 +2137,8 @@ private bool CheckForStateChange(ref NetworkTransformState networkState, bool is
// We compare against the NetworkTickSystem version since ServerTime is set when updating ticks
if (UseUnreliableDeltas && !isSynchronization && m_DeltaSynch && m_NextTickSync <= CurrentTick)
{
// TODO-CACHE: m_CachedNetworkManager.NetworkConfig.TickRate value
// Increment to the next frame synch tick position for this instance
m_NextTickSync += (int)m_CachedNetworkManager.NetworkConfig.TickRate;
m_NextTickSync += m_CachedTickRate;
// If we are teleporting, we do not need to send a frame synch for this tick slot
// as a "frame synch" really is effectively just a teleport.
isAxisSync = !flagStates.IsTeleportingNextFrame;
Expand Down Expand Up @@ -2606,7 +2606,7 @@ private void OnNetworkTick(bool isCalledFromParent = false)
}
else // If we are no longer authority, unsubscribe to the tick event
{
DeregisterForTickUpdate(this);
DeregisterForTickUpdate();
}
}
#endregion
Expand Down Expand Up @@ -3537,7 +3537,7 @@ private void ApplyPlayerTransformState()

/// <summary>
/// For dynamically spawned NetworkObjects, when the non-authority instance's client is already connected and
/// the SynchronizeState is still pending synchronization then we want to finalize the synchornization at this time.
/// the SynchronizeState is still pending synchronization then we want to finalize the synchronization at this time.
/// </summary>
protected internal override void InternalOnNetworkPostSpawn()
{
Expand All @@ -3550,7 +3550,7 @@ protected internal override void InternalOnNetworkPostSpawn()
// Then we want to:
// - Force the "IsSynchronizing" flag so the NetworkTransform has its state updated properly and runs through the initialization again.
// - Make sure the SynchronizingState is updated to the instantiated prefab's default flags/settings.
if (NetworkManager.IsServer && !NetworkManager.DistributedAuthorityMode && !IsOwner && !OnIsServerAuthoritative() && !SynchronizeState.IsSynchronizing)
if (m_CachedNetworkManager.IsServer && !m_CachedNetworkManager.DistributedAuthorityMode && !IsOwner && !OnIsServerAuthoritative() && !SynchronizeState.IsSynchronizing)
{
// Handle the first/root NetworkTransform slightly differently to have a sequenced synchronization of like authority nested NetworkTransform components
if (m_IsFirstNetworkTransform)
Expand Down Expand Up @@ -3578,7 +3578,7 @@ protected internal override void InternalOnNetworkPostSpawn()
}

// Standard non-authority synchronization is handled here
if (!CanCommitToTransform && NetworkManager.IsConnectedClient && SynchronizeState.IsSynchronizing)
if (!CanCommitToTransform && m_CachedNetworkManager.IsConnectedClient && SynchronizeState.IsSynchronizing)
{
NonAuthorityFinalizeSynchronization();
}
Expand Down Expand Up @@ -3631,14 +3631,14 @@ internal override void InternalOnNetworkPreSpawn(ref NetworkManager networkManag
public override void OnNetworkSpawn()
{
m_ParentedChildren.Clear();
m_CachedNetworkManager = NetworkManager;

Initialize();

if (CanCommitToTransform && !SwitchTransformSpaceWhenParented)
{
SetState(GetSpaceRelativePosition(), GetSpaceRelativeRotation(), GetScale(), false);
}
base.OnNetworkSpawn();
}

private void CleanUpOnDestroyOrDespawn()
Expand All @@ -3651,10 +3651,10 @@ private void CleanUpOnDestroyOrDespawn()
#endif
if (m_CachedNetworkObject != null)
{
NetworkManager?.NetworkTransformRegistration(m_CachedNetworkObject, forUpdate, false);
m_CachedNetworkManager.NetworkTransformRegistration(m_CachedNetworkObject, forUpdate, false);
}

DeregisterForTickUpdate(this);
DeregisterForTickUpdate();
CanCommitToTransform = false;
}

Expand Down Expand Up @@ -3697,7 +3697,7 @@ protected virtual void OnInitialize(ref NetworkVariable<NetworkTransformState> r
/// </summary>
private void ResetInterpolatedStateToCurrentAuthoritativeState()
{
var serverTime = NetworkManager.ServerTime.Time;
var serverTime = m_CachedNetworkManager.ServerTime.Time;
#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
var position = m_UseRigidbodyForMotion ? m_NetworkRigidbodyInternal.GetPosition() : GetSpaceRelativePosition();
var rotation = m_UseRigidbodyForMotion ? m_NetworkRigidbodyInternal.GetRotation() : GetSpaceRelativeRotation();
Expand All @@ -3719,7 +3719,7 @@ private void ResetInterpolatedStateToCurrentAuthoritativeState()
}
private NetworkObject m_CachedNetworkObject;
/// <summary>
/// The internal initialzation method to allow for internal API adjustments
/// The internal initialization method to allow for internal API adjustments
/// </summary>
/// <param name="isOwnershipChange"></param>
private void InternalInitialization(bool isOwnershipChange = false)
Expand All @@ -3729,9 +3729,10 @@ private void InternalInitialization(bool isOwnershipChange = false)
return;
}
m_CachedNetworkObject = NetworkObject;
m_CachedTickRate = (int)m_CachedNetworkManager.NetworkConfig.TickRate;

// Determine if this is the first NetworkTransform in the associated NetworkObject's list
m_IsFirstNetworkTransform = NetworkObject.NetworkTransforms[0] == this;
m_IsFirstNetworkTransform = m_CachedNetworkObject.NetworkTransforms[0] == this;

if (m_CachedNetworkManager && m_CachedNetworkManager.DistributedAuthorityMode)
{
Expand All @@ -3755,9 +3756,9 @@ private void InternalInitialization(bool isOwnershipChange = false)
var currentPosition = GetSpaceRelativePosition();
var currentRotation = GetSpaceRelativeRotation();

if (NetworkManager.DistributedAuthorityMode)
if (m_CachedNetworkManager.DistributedAuthorityMode)
{
RegisterNetworkManagerForTickUpdate(NetworkManager);
RegisterNetworkManagerForTickUpdate(m_CachedNetworkManager);
}

#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
Expand Down Expand Up @@ -3793,7 +3794,7 @@ private void InternalInitialization(bool isOwnershipChange = false)
m_InternalCurrentPosition = currentPosition;
m_LastStateTargetPosition = currentPosition;

RegisterForTickUpdate(this);
RegisterForTickUpdate();

if (UseHalfFloatPrecision && isOwnershipChange && !IsServerAuthoritative() && Interpolate)
{
Expand All @@ -3813,7 +3814,7 @@ private void InternalInitialization(bool isOwnershipChange = false)
// Non-authority needs to be added to updates for interpolation and applying state purposes
m_CachedNetworkManager.NetworkTransformRegistration(NetworkObject, forUpdate, true);
// Remove this instance from the tick update
DeregisterForTickUpdate(this);
DeregisterForTickUpdate();
ResetInterpolatedStateToCurrentAuthoritativeState();
m_InternalCurrentPosition = currentPosition;
m_LastStateTargetPosition = currentPosition;
Expand Down Expand Up @@ -3941,7 +3942,7 @@ internal override void InternalOnNetworkObjectParentChanged(NetworkObject parent
if (LastTickSync == m_LocalAuthoritativeNetworkState.GetNetworkTick())
{
m_InternalCurrentPosition = m_LastStateTargetPosition = GetSpaceRelativePosition();
m_PositionInterpolator.ResetTo(m_PositionInterpolator.Parent, m_InternalCurrentPosition, NetworkManager.ServerTime.Time);
m_PositionInterpolator.ResetTo(m_PositionInterpolator.Parent, m_InternalCurrentPosition, m_CachedNetworkManager.ServerTime.Time);
if (InLocalSpace)
{
transform.localPosition = m_InternalCurrentPosition;
Expand Down Expand Up @@ -3973,7 +3974,7 @@ internal override void InternalOnNetworkObjectParentChanged(NetworkObject parent
{
m_InternalCurrentRotation = GetSpaceRelativeRotation();
m_TargetRotation = m_InternalCurrentRotation.eulerAngles;
m_RotationInterpolator.ResetTo(m_RotationInterpolator.Parent, m_InternalCurrentRotation, NetworkManager.ServerTime.Time);
m_RotationInterpolator.ResetTo(m_RotationInterpolator.Parent, m_InternalCurrentRotation, m_CachedNetworkManager.ServerTime.Time);
if (InLocalSpace)
{
transform.localRotation = m_InternalCurrentRotation;
Expand Down Expand Up @@ -4596,7 +4597,7 @@ internal void TransformStateUpdate()
{
// TODO: Investigate where this state should be applied or just discarded.
// For now, discard the state if we assumed ownership.
// Debug.Log($"[Client-{NetworkManager.LocalClientId}] Ignoring inbound update from Client-{0} and parentUpdated:{isParentingDirective}!");
// Debug.Log($"[Client-{m_CachedNetworkManager.LocalClientId}] Ignoring inbound update from Client-{0} and parentUpdated:{isParentingDirective}!");
return;
}
// Store the previous/old state
Expand Down Expand Up @@ -4653,17 +4654,17 @@ private void UpdateTransformState()
{
continue;
}
if (!NetworkObject.Observers.Contains(clientId))
if (!m_CachedNetworkObject.Observers.Contains(clientId))
{
continue;
}
NetworkManager.MessageManager.SendMessage(ref m_OutboundMessage, networkDelivery, clientId);
m_CachedNetworkManager.MessageManager.SendMessage(ref m_OutboundMessage, networkDelivery, clientId);
}
}
else
{
// Clients (owner authoritative) send messages to the server-host
NetworkManager.MessageManager.SendMessage(ref m_OutboundMessage, networkDelivery, NetworkManager.ServerClientId);
m_CachedNetworkManager.MessageManager.SendMessage(ref m_OutboundMessage, networkDelivery, NetworkManager.ServerClientId);
}
m_LocalAuthoritativeNetworkState.LastSerializedSize = m_OutboundMessage.BytesWritten;
}
Expand Down Expand Up @@ -4802,7 +4803,7 @@ public NetworkTransformTickRegistration(NetworkManager networkManager)
internal void RegisterForTickSynchronization()
{
s_TickSynchPosition++;
m_NextTickSync = NetworkManager.ServerTime.Tick + (s_TickSynchPosition % (int)NetworkManager.NetworkConfig.TickRate);
m_NextTickSync = m_CachedNetworkManager.ServerTime.Tick + (s_TickSynchPosition % m_CachedTickRate);
}

private static void RegisterNetworkManagerForTickUpdate(NetworkManager networkManager)
Expand All @@ -4818,36 +4819,34 @@ private static void RegisterNetworkManagerForTickUpdate(NetworkManager networkMa
/// If a NetworkTransformTickRegistration has not yet been registered for the NetworkManager
/// instance, then create an entry.
/// </summary>
/// <param name="networkTransform"></param>
private static void RegisterForTickUpdate(NetworkTransform networkTransform)
private void RegisterForTickUpdate()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch and adjustment!
:godmode:

{

if (!networkTransform.NetworkManager.DistributedAuthorityMode && !s_NetworkTickRegistration.ContainsKey(networkTransform.NetworkManager))
if (!m_CachedNetworkManager.DistributedAuthorityMode && !s_NetworkTickRegistration.ContainsKey(m_CachedNetworkManager))
{
s_NetworkTickRegistration.Add(networkTransform.NetworkManager, new NetworkTransformTickRegistration(networkTransform.NetworkManager));
s_NetworkTickRegistration.Add(m_CachedNetworkManager, new NetworkTransformTickRegistration(m_CachedNetworkManager));
}

networkTransform.RegisterForTickSynchronization();
s_NetworkTickRegistration[networkTransform.NetworkManager].NetworkTransforms.Add(networkTransform);
RegisterForTickSynchronization();
s_NetworkTickRegistration[m_CachedNetworkManager].NetworkTransforms.Add(this);
}

/// <summary>
/// If a NetworkTransformTickRegistration exists for the NetworkManager instance, then this will
/// remove the NetworkTransform instance from the single tick update entry point.
/// </summary>
/// <param name="networkTransform"></param>
private static void DeregisterForTickUpdate(NetworkTransform networkTransform)
private void DeregisterForTickUpdate()
{
if (networkTransform.NetworkManager == null)
if (m_CachedNetworkManager == null)
{
return;
}
if (s_NetworkTickRegistration.ContainsKey(networkTransform.NetworkManager))
if (s_NetworkTickRegistration.ContainsKey(m_CachedNetworkManager))
{
s_NetworkTickRegistration[networkTransform.NetworkManager].NetworkTransforms.Remove(networkTransform);
if (!networkTransform.NetworkManager.DistributedAuthorityMode && s_NetworkTickRegistration[networkTransform.NetworkManager].NetworkTransforms.Count == 0)
s_NetworkTickRegistration[m_CachedNetworkManager].NetworkTransforms.Remove(this);
if (!m_CachedNetworkManager.DistributedAuthorityMode && s_NetworkTickRegistration[m_CachedNetworkManager].NetworkTransforms.Count == 0)
{
var registrationEntry = s_NetworkTickRegistration[networkTransform.NetworkManager];
var registrationEntry = s_NetworkTickRegistration[m_CachedNetworkManager];
registrationEntry.Remove();
}
}
Expand Down