Skip to content

Commit ba19e82

Browse files
update
NetworkRigidbodyBase and NetworkTransform classes are destroyed during runtime initialization of a prefab instance if they still remain and the instance is a hybrid prefab instance (i.e. unified handles transform synchronization and physics related stuff).
1 parent d759121 commit ba19e82

2 files changed

Lines changed: 88 additions & 16 deletions

File tree

com.unity.netcode.gameobjects/Runtime/Components/NetworkRigidBodyBase.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ protected void Initialize(RigidbodyTypes rigidbodyType, NetworkTransform network
193193
#endif
194194

195195
#if COM_UNITY_MODULES_PHYSICS && COM_UNITY_MODULES_PHYSICS2D
196+
#if UNIFIED_NETCODE
197+
// Used to keep track of the original kinematic state upon awake.
198+
// (see OnDestroy below)
199+
private bool m_OriginalKinematicState;
200+
#endif
196201
/// <summary>
197202
/// Initializes the networked Rigidbody based on the <see cref="RigidbodyTypes"/>
198203
/// passed in as a parameter.
@@ -246,9 +251,31 @@ protected void Initialize(RigidbodyTypes rigidbodyType, NetworkTransform network
246251

247252
if (AutoUpdateKinematicState)
248253
{
254+
#if UNIFIED_NETCODE
255+
// Keep track of the original kinematic state. (see OnDestroy)
256+
m_OriginalKinematicState = IsKinematic();
257+
#endif
249258
SetIsKinematic(true);
250259
}
251260
}
261+
262+
#if UNIFIED_NETCODE
263+
public override void OnDestroy()
264+
{
265+
base.OnDestroy();
266+
// If the user has left this component on their prefab and this is a hybrid prefab,
267+
// then we want to set the rigid body back to its original kinematic settings since
268+
// we are automatically destroying these components at runtime when it is a hybrid
269+
// prefab that is spawned.
270+
if (NetworkObject && NetworkObject.HasGhost)
271+
{
272+
if (m_InternalRigidbody || m_InternalRigidbody2D)
273+
{
274+
SetIsKinematic(m_OriginalKinematicState);
275+
}
276+
}
277+
}
278+
#endif
252279
#endif
253280
internal Vector3 GetAdjustedPositionThreshold()
254281
{

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ private void UnifiedValidation()
368368
{
369369
NetworkObjectBridge = gameObject.AddComponent<NetworkObjectBridge>();
370370
HadBridge = true;
371+
// Transform synchronization is handled by unified netcode
372+
SynchronizeTransform = false;
371373
}
372374
}
373375
}
@@ -2696,35 +2698,78 @@ internal List<NetworkBehaviour> ChildNetworkBehaviours
26962698
{
26972699
continue;
26982700
}
2699-
2700-
// Set ourselves as the NetworkObject that this behaviour belongs to and add it to the child list
2701-
var nextIndex = (ushort)m_ChildNetworkBehaviours.Count;
2702-
networkBehaviours[i].SetNetworkObject(this, nextIndex);
2703-
m_ChildNetworkBehaviours.Add(networkBehaviours[i]);
2704-
27052701
var type = networkBehaviours[i].GetType();
2702+
#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
2703+
if (type.IsSubclassOf(typeof(NetworkRigidbodyBase)))
2704+
{
2705+
var networkRigidbody = networkBehaviours[i] as NetworkRigidbodyBase;
2706+
2707+
if (NetworkRigidbodies == null)
2708+
{
2709+
NetworkRigidbodies = new List<NetworkRigidbodyBase>();
2710+
}
2711+
NetworkRigidbodies.Add(networkRigidbody);
2712+
#if UNIFIED_NETCODE
2713+
// For now, we will just destroy these components during runtime since they will not
2714+
// be supported in hybrid mode (don't add to the children).
2715+
if (HasGhost)
2716+
{
2717+
continue;
2718+
}
2719+
#endif
2720+
}
2721+
else
2722+
#endif
27062723
if (type == typeof(NetworkTransform) || type.IsInstanceOfType(typeof(NetworkTransform)) || type.IsSubclassOf(typeof(NetworkTransform)))
27072724
{
2725+
var networkTransform = networkBehaviours[i] as NetworkTransform;
2726+
27082727
if (NetworkTransforms == null)
27092728
{
27102729
NetworkTransforms = new List<NetworkTransform>();
27112730
}
2712-
var networkTransform = networkBehaviours[i] as NetworkTransform;
2731+
27132732
networkTransform.IsNested = i != 0 && networkTransform.gameObject != gameObject;
27142733
NetworkTransforms.Add(networkTransform);
2715-
}
2716-
#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
2717-
else if (type.IsSubclassOf(typeof(NetworkRigidbodyBase)))
2718-
{
2719-
if (NetworkRigidbodies == null)
2734+
#if UNIFIED_NETCODE
2735+
// For now, we will just destroy these components during runtime since they will not
2736+
// be supported in hybrid mode (don't add to the children).
2737+
if (HasGhost)
27202738
{
2721-
NetworkRigidbodies = new List<NetworkRigidbodyBase>();
2739+
continue;
27222740
}
2723-
NetworkRigidbodies.Add(networkBehaviours[i] as NetworkRigidbodyBase);
2724-
}
27252741
#endif
2742+
}
2743+
2744+
// Set ourselves as the NetworkObject that this behaviour belongs to and add it to the child list
2745+
var nextIndex = (ushort)m_ChildNetworkBehaviours.Count;
2746+
networkBehaviours[i].SetNetworkObject(this, nextIndex);
2747+
2748+
// Finally, add the NetworkBehaviour to the list of child NetworkBehaviours
2749+
m_ChildNetworkBehaviours.Add(networkBehaviours[i]);
27262750
}
27272751

2752+
#if UNIFIED_NETCODE
2753+
// For now, cycle through all known NetworkTransform and NetworkRigidbodyBase derived components
2754+
// and destroy them all if this is a hybrid prefab instance.
2755+
// This allows a user to not have to make direct adjustments until trying out their NGO prefab
2756+
// as a hybrid spawned prefab (optional to completely remove, will eventually become obsolete and
2757+
// automatically removed later).
2758+
if (HasGhost)
2759+
{
2760+
for (int i = NetworkRigidbodies.Count - 1; i >= 0; i--)
2761+
{
2762+
Destroy(NetworkRigidbodies[i]);
2763+
}
2764+
for (int i = NetworkTransforms.Count - 1; i >= 0; i--)
2765+
{
2766+
Destroy(NetworkTransforms[i]);
2767+
}
2768+
NetworkRigidbodies.Clear();
2769+
NetworkTransforms.Clear();
2770+
}
2771+
#endif
2772+
27282773
return m_ChildNetworkBehaviours;
27292774
}
27302775
}
@@ -3572,7 +3617,7 @@ private void Start()
35723617
Debug.LogWarning($"[{nameof(NetworkObject)}][{name}] Was not enabled on start! Enabling.");
35733618
enabled = true;
35743619
}
3575-
3620+
35763621
InitGhost();
35773622
}
35783623
[SerializeField]

0 commit comments

Comments
 (0)