Skip to content

Commit 7b528d4

Browse files
update
Part of the hybrid spawned parenting fixes.
1 parent bdf68c5 commit 7b528d4

2 files changed

Lines changed: 50 additions & 17 deletions

File tree

com.unity.netcode.gameobjects/Runtime/Components/Helpers/NetworkObjectBridge.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#if UNIFIED_NETCODE
22
using Unity.NetCode;
3+
#if UNITY_EDITOR
34
using UnityEditor;
5+
#endif
6+
using UnityEngine;
47

58
namespace Unity.Netcode
69
{
@@ -13,12 +16,13 @@ namespace Unity.Netcode
1316
public partial class NetworkObjectBridge : GhostBehaviour
1417
{
1518
#if UNITY_EDITOR
16-
[UnityEngine.HideInInspector]
17-
[UnityEngine.SerializeField]
19+
[HideInInspector]
20+
[SerializeField]
1821
private bool m_Sorted = false;
1922
private void OnValidate()
2023
{
21-
// Sort only once when we have first been added.
24+
// TODO-UNIFIED: GhostAdapter must be above all GhostBehaviours in order to assure the GhostAdapter is initialized before any GhostBehaviour.
25+
// This auto-sorting is required because the GhostBehaviours rely on the GhostAdapter.Awake being invoked before any GhostBehaviour.Awake.
2226
if (!m_Sorted && !EditorApplication.isPlaying)
2327
{
2428
while (UnityEditorInternal.ComponentUtility.MoveComponentUp(this))
@@ -47,6 +51,22 @@ public void SetNetworkObjectId(ulong networkObjectId)
4751
NetworkObjectId.PresetValue(networkObjectId);
4852
NetworkObjectId.Value = networkObjectId;
4953
}
54+
55+
/// <summary>
56+
/// Currently, NGO provides the parenting event handling via <see cref="ParentSyncMessage"/>.
57+
/// Once <see cref="GhostField{InternalTypeT}"/> can provide a form of event notification that
58+
/// the value has changed, we can then invert this flow such that the change in the parent value
59+
/// drives the event.
60+
/// </summary>
61+
/// <param name="scale">We use NGO scale, delivered via ParentSyncMessage, that is applied to this
62+
/// instance's entity's PostTransformMatrix.</param>
63+
internal void HybridParentUpdate(Vector3 scale)
64+
{
65+
var current = Ghost.GetPositionAndRotation();
66+
//Debug.Log($"---- Current LT: {current.Position} | {current.Rotation.eulerAngles}");
67+
//Debug.Log($"---- New LT: {transform.localPosition} | {transform.localRotation}");
68+
Ghost.ApplyPostTransformMatrixScale(scale);
69+
}
5070
}
5171
}
5272
#endif

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ParentSyncMessage.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,29 +118,42 @@ public void Handle(ref NetworkContext context)
118118
// DANGO-TODO: Still determining if we should not apply this change (I am leaning towards not allowing it).
119119
}
120120

121+
121122
networkObject.SetNetworkParenting(LatestParent, WorldPositionStays);
122123
networkObject.ApplyNetworkParenting(RemoveParent);
123124

124-
// This check is primarily for client-server network topologies when the motion model is owner authoritative:
125-
// When SyncOwnerTransformWhenParented is enabled, then always apply the transform values.
126-
// When SyncOwnerTransformWhenParented is disabled, then only synchronize the transform on non-owner instances.
127-
if (networkObject.SyncOwnerTransformWhenParented || (!networkObject.SyncOwnerTransformWhenParented && !networkObject.IsOwner))
125+
#if UNIFIED_NETCODE
126+
if (networkObject.HasGhost)
128127
{
129-
// We set all of the transform values after parenting as they are
130-
// the values of the server-side post-parenting transform values
131-
if (!WorldPositionStays)
132-
{
133-
networkObject.transform.SetLocalPositionAndRotation(Position, Rotation);
134-
}
135-
else
128+
// Handles the GhostAdapter side of things for parenting
129+
networkObject.NetworkObjectBridge.HybridParentUpdate(Scale);
130+
}
131+
else
132+
#endif
133+
{
134+
// This check is primarily for client-server network topologies when the motion model is owner authoritative:
135+
// When SyncOwnerTransformWhenParented is enabled, then always apply the transform values.
136+
// When SyncOwnerTransformWhenParented is disabled, then only synchronize the transform on non-owner instances.
137+
if (networkObject.SyncOwnerTransformWhenParented || (!networkObject.SyncOwnerTransformWhenParented && !networkObject.IsOwner))
136138
{
137-
networkObject.transform.SetPositionAndRotation(Position, Rotation);
139+
140+
// We set all of the transform values after parenting as they are
141+
// the values of the server-side post-parenting transform values
142+
if (!WorldPositionStays)
143+
{
144+
networkObject.transform.SetLocalPositionAndRotation(Position, Rotation);
145+
}
146+
else
147+
{
148+
networkObject.transform.SetPositionAndRotation(Position, Rotation);
149+
}
150+
networkObject.transform.localScale = Scale;
138151
}
139-
networkObject.transform.localScale = Scale;
140152
}
141153

142154
// If in distributed authority mode and we are running a DAHost and this is the DAHost, then forward the parent changed message to any remaining clients
143-
if ((networkManager.DistributedAuthorityMode && !networkManager.CMBServiceConnection && networkManager.DAHost) || (networkObject.AllowOwnerToParent && context.SenderId == networkObject.OwnerClientId && networkManager.IsServer))
155+
if ((networkManager.DistributedAuthorityMode && !networkManager.CMBServiceConnection && networkManager.DAHost)
156+
|| (networkObject.AllowOwnerToParent && context.SenderId == networkObject.OwnerClientId && networkManager.IsServer))
144157
{
145158
var size = 0;
146159
var message = this;

0 commit comments

Comments
 (0)