Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -14,7 +14,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
- `NetworkTransport.EarlyUpdate` and `NetworkTransport.PostLateUpdate` are now public. For the vast majority of users, there's really no point in ever calling those methods directly (the `NetworkManager` handles it). It's only useful if wrapping transports outside of NGO. (#3890)

### Changed

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

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1621,7 +1621,7 @@ 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;
Expand Down Expand Up @@ -1852,6 +1852,7 @@ private bool ShouldSynchronizeHalfFloat(ulong targetClientId)
if (!IsServerAuthoritative() && NetworkObject.OwnerClientId == targetClientId)
{
// In distributed authority mode we want to synchronize the half float if we are the owner.
// TODO do we have a cached NetworkManager here? Should we create one?
return (!NetworkManager.DistributedAuthorityMode && NetworkObject.IsOwnedByServer) || (NetworkManager.DistributedAuthorityMode);
}
return true;
Expand Down Expand Up @@ -3537,7 +3538,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 +3551,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 +3579,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 +3632,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,7 +3652,7 @@ private void CleanUpOnDestroyOrDespawn()
#endif
if (m_CachedNetworkObject != null)
{
NetworkManager?.NetworkTransformRegistration(m_CachedNetworkObject, forUpdate, false);
m_CachedNetworkManager.NetworkTransformRegistration(m_CachedNetworkObject, forUpdate, false);
}

DeregisterForTickUpdate(this);
Expand Down Expand Up @@ -3697,7 +3698,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 +3720,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 @@ -3731,7 +3732,7 @@ private void InternalInitialization(bool isOwnershipChange = false)
m_CachedNetworkObject = NetworkObject;

// 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 @@ -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 % (int)NetworkManager.NetworkConfig.TickRate);
}

private static void RegisterNetworkManagerForTickUpdate(NetworkManager networkManager)
Expand All @@ -4821,14 +4822,14 @@ private static void RegisterNetworkManagerForTickUpdate(NetworkManager networkMa
/// <param name="networkTransform"></param>
private static void RegisterForTickUpdate(NetworkTransform networkTransform)
{

if (!networkTransform.NetworkManager.DistributedAuthorityMode && !s_NetworkTickRegistration.ContainsKey(networkTransform.NetworkManager))
var networkManager = networkTransform.NetworkManager;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we are in static context, does it mean we are using NetworkManager.Singleton? If that's so, shouldn't we use that one instead of networkTransform.NetworkManager?

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

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

/// <summary>
Expand All @@ -4838,16 +4839,17 @@ private static void RegisterForTickUpdate(NetworkTransform networkTransform)
/// <param name="networkTransform"></param>
private static void DeregisterForTickUpdate(NetworkTransform networkTransform)
{
if (networkTransform.NetworkManager == null)
var networkManager = networkTransform.NetworkManager;
Copy link
Collaborator Author

@noellie-velez noellie-velez Mar 18, 2026

Choose a reason for hiding this comment

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

Same here:
If we are in static context, does it mean we are using NetworkManager.Singleton? If that's so, shouldn't we use that one instead of networkTransform.NetworkManager?

if (!networkManager)
{
return;
}
if (s_NetworkTickRegistration.ContainsKey(networkTransform.NetworkManager))
if (s_NetworkTickRegistration.ContainsKey(networkManager))
{
s_NetworkTickRegistration[networkTransform.NetworkManager].NetworkTransforms.Remove(networkTransform);
if (!networkTransform.NetworkManager.DistributedAuthorityMode && s_NetworkTickRegistration[networkTransform.NetworkManager].NetworkTransforms.Count == 0)
s_NetworkTickRegistration[networkManager].NetworkTransforms.Remove(networkTransform);
if (!networkManager.DistributedAuthorityMode && s_NetworkTickRegistration[networkManager].NetworkTransforms.Count == 0)
{
var registrationEntry = s_NetworkTickRegistration[networkTransform.NetworkManager];
var registrationEntry = s_NetworkTickRegistration[networkManager];
registrationEntry.Remove();
}
}
Expand Down
Loading