-
Notifications
You must be signed in to change notification settings - Fork 459
chore: [Optimize accessors] NetworkTransform #3907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
noellie-velez
merged 12 commits into
develop-2.0.0
from
chore/simplify-accessors-network-transform
Apr 2, 2026
+44
−41
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5aab06d
Use cached network manager instead of property
noellie-velez 17416f7
Update changelog
noellie-velez 555f9f8
comments cleanup
noellie-velez 784530f
Merge branch 'develop-2.0.0' into chore/simplify-accessors-network-tr…
noellie-velez ecb2758
remove static from method
noellie-velez 6a71541
Small update
noellie-velez 279768a
Caching tickRate
noellie-velez 3ba4076
Merge branch 'develop-2.0.0' into chore/simplify-accessors-network-tr…
noellie-velez 016b1f9
Add networkObject.name in NetcodeTransformMessage
noellie-velez 650e674
Merge branch 'develop-2.0.0' into chore/simplify-accessors-network-tr…
EmandM e40f56e
Fix changelog order
noellie-velez e810b45
Address PR review feedback
noellie-velez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -2136,9 +2136,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; | ||
|
|
@@ -2606,7 +2605,7 @@ private void OnNetworkTick(bool isCalledFromParent = false) | |
| } | ||
| else // If we are no longer authority, unsubscribe to the tick event | ||
| { | ||
| DeregisterForTickUpdate(this); | ||
| DeregisterForTickUpdate(); | ||
| } | ||
| } | ||
| #endregion | ||
|
|
@@ -3537,7 +3536,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() | ||
| { | ||
|
|
@@ -3550,7 +3549,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) | ||
|
|
@@ -3578,7 +3577,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(); | ||
| } | ||
|
|
@@ -3620,9 +3619,14 @@ protected virtual void Awake() | |
| CachedTransform = transform; | ||
| } | ||
|
|
||
| private NetworkObject m_CachedNetworkObject; | ||
| private int m_CachedTickRate; | ||
|
|
||
| internal override void InternalOnNetworkPreSpawn(ref NetworkManager networkManager) | ||
| { | ||
| m_CachedNetworkManager = networkManager; | ||
| m_CachedNetworkObject = NetworkObject; | ||
| m_CachedTickRate = (int)networkManager.NetworkConfig.TickRate; | ||
| CachedTransform = transform; | ||
| base.InternalOnNetworkPreSpawn(ref networkManager); | ||
| } | ||
|
|
@@ -3631,14 +3635,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() | ||
|
|
@@ -3651,10 +3655,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; | ||
| } | ||
|
|
||
|
|
@@ -3697,7 +3701,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(); | ||
|
|
@@ -3717,9 +3721,9 @@ private void ResetInterpolatedStateToCurrentAuthoritativeState() | |
|
|
||
| m_ScaleInterpolator.ResetTo(transform.parent, transform.localScale, serverTime); | ||
| } | ||
| 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) | ||
|
|
@@ -3728,10 +3732,9 @@ private void InternalInitialization(bool isOwnershipChange = false) | |
| { | ||
| return; | ||
| } | ||
| 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) | ||
| { | ||
|
|
@@ -3755,9 +3758,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 | ||
|
|
@@ -3793,7 +3796,7 @@ private void InternalInitialization(bool isOwnershipChange = false) | |
| m_InternalCurrentPosition = currentPosition; | ||
| m_LastStateTargetPosition = currentPosition; | ||
|
|
||
| RegisterForTickUpdate(this); | ||
| RegisterForTickUpdate(); | ||
|
|
||
| if (UseHalfFloatPrecision && isOwnershipChange && !IsServerAuthoritative() && Interpolate) | ||
| { | ||
|
|
@@ -3813,7 +3816,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; | ||
|
|
@@ -3941,7 +3944,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; | ||
|
|
@@ -3973,7 +3976,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; | ||
|
|
@@ -4596,7 +4599,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 | ||
|
|
@@ -4653,17 +4656,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; | ||
| } | ||
|
|
@@ -4802,7 +4805,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) | ||
|
|
@@ -4818,36 +4821,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() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch and adjustment! |
||
| { | ||
|
|
||
| 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(); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.