Skip to content

Commit e294325

Browse files
update - clean up
Cleaning up some areas. Wrapping all of the original network prefab unified work where NGO handles the timing for hybrid prefab registration. (We might remove this prior to release but keeping it for reference purposes in case there are changes on N4E side that require us to use the original approach) Fixing issue with not removing Rigidbody on clients automatically. (for now until we can make it an opt-in)
1 parent c45de18 commit e294325

9 files changed

Lines changed: 72 additions & 29 deletions

File tree

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ namespace Unity.Netcode
66
{
77
/// <summary>
88
/// TODO-UNIFIED: Needs further peer review and exploring alternate ways of handling this.
9+
/// This is a component that is added to the root of all N4E-spawned hybrid prefab instances. It is used to link
10+
/// <see cref="NetworkObject.SerializedObject"/> the N4E-spawned hybrid prefab instances to the incoming <see cref="CreateObjectMessage"/>
11+
/// specific to the N4E-spawned hybrid prefab instance that has the matching <see cref="NetworkObjectId"/>.
912
/// </summary>
10-
/// <remarks>
11-
/// If used, we most likely would make this internal
12-
/// </remarks>
1313
public partial class NetworkObjectBridge : GhostBehaviour
1414
{
15-
1615
#if UNITY_EDITOR
1716
[UnityEngine.HideInInspector]
1817
[UnityEngine.SerializeField]
@@ -38,7 +37,6 @@ private void OnValidate()
3837
}
3938
#endif
4039

41-
4240
/// <summary>
4341
/// This is used to link <see cref="NetworkObject.SerializedObject"/> data to
4442
/// N4E-spawned hybrid prefab instances.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,16 @@ public override bool Initialize(string defaultWorldName)
6464
}
6565

6666
networkManager.NetcodeWorld = (NetcodeWorld)LastCreatedWorld;
67+
#if UNIFIED_NGO_REGISTERS_PREFABS
6768
if (networkManager.NetworkConfig.Prefabs.HasPendingGhostPrefabs)
6869
{
6970
if (networkManager.LogLevel <= LogLevel.Developer)
7071
{
7172
NetworkLog.LogInfo($"[{nameof(UnifiedBootstrap)}] Registering hybrid prefabs...");
7273
}
73-
7474
networkManager.NetworkConfig.Prefabs.RegisterGhostPrefabs(networkManager);
7575
}
76+
#endif
7677
}
7778
else
7879
{

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ public abstract class NetworkRigidbodyBase : NetworkBehaviour
2020
internal bool NetworkRigidbodyBaseExpanded;
2121
#endif
2222

23+
// TODO-UNIFIED:
24+
// Provide an option to automatically remove the NetworkRigidbodyBase component at runtime if it is a hybrid prefab that is spawned since this
25+
// component is primarily used for the Rigidbody interpolation and extrapolation features of NetworkTransform which are not relevant for a hybrid
26+
// prefab that is spawned since it will be using N4E's built in interpolation and extrapolation features. This greatly improves performance on
27+
// the client side. If using N4E prediction or distributed authority mode, then Rigibody and any component derived from this should always be used.
28+
29+
2330
/// <summary>
2431
/// When enabled, the associated <see cref="NetworkTransform"/> will use the Rigidbody/Rigidbody2D to apply and synchronize changes in position, rotation, and
2532
/// allows for the use of Rigidbody interpolation/extrapolation.
@@ -51,7 +58,6 @@ public abstract class NetworkRigidbodyBase : NetworkBehaviour
5158
private bool m_IsRigidbody2D = false;
5259
#endif
5360

54-
5561
private NetworkManager m_LocalNetworkManager;
5662
// Used to cache the authority state of this Rigidbody during the last frame
5763
private bool m_IsAuthority;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ namespace Unity.Netcode.Components
99
/// mode of the <see cref="Rigidbody"/> and disabling it on all peers but the authoritative one.
1010
/// </summary>
1111
[RequireComponent(typeof(NetworkTransform))]
12+
// TODO-UNIFIED: We should not require this for unified and come up with a different way of handling the dependency
13+
#if !UNIFIED_NETCODE
1214
[RequireComponent(typeof(Rigidbody))]
15+
#endif
1316
[AddComponentMenu("Netcode/Network Rigidbody")]
1417
[HelpURL(HelpUrls.NetworkRigidbody)]
1518
public class NetworkRigidbody : NetworkRigidbodyBase

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ namespace Unity.Netcode.Components
99
/// mode of the rigidbody and disabling it on all peers but the authoritative one.
1010
/// </summary>
1111
[RequireComponent(typeof(NetworkTransform))]
12+
// TODO-UNIFIED: We should not require this for unified and come up with a different way of handling the dependency
13+
#if !UNIFIED_NETCODE
1214
[RequireComponent(typeof(Rigidbody2D))]
15+
#endif
1316
[AddComponentMenu("Netcode/Network Rigidbody 2D")]
1417
[HelpURL(HelpUrls.NetworkRigidbody2D)]
1518
public class NetworkRigidbody2D : NetworkRigidbodyBase

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3628,8 +3628,6 @@ protected virtual void Awake()
36283628
}
36293629

36303630
CachedTransform = transform;
3631-
3632-
36333631
}
36343632

36353633
private NetworkObject m_CachedNetworkObject;
@@ -3648,6 +3646,9 @@ internal override void InternalOnNetworkPreSpawn(ref NetworkManager networkManag
36483646
public override void OnNetworkSpawn()
36493647
{
36503648
#if UNIFIED_NETCODE
3649+
// TODO-UNIFIED:
3650+
// Provide a notification to users that NetworkTransform component will be removed at runtime if it is a hybrid prefab that is spawned since
3651+
// it will be using N4E's built in interpolation and extrapolation features.
36513652
if (NetworkObject.HasGhost)
36523653
{
36533654
return;
@@ -3747,8 +3748,10 @@ private void ResetInterpolatedStateToCurrentAuthoritativeState()
37473748
/// <param name="isOwnershipChange"></param>
37483749
internal virtual void InternalInitialization(bool isOwnershipChange = false)
37493750
{
3750-
37513751
#if UNIFIED_NETCODE
3752+
// TODO-UNIFIED:
3753+
// Provide a notification to users that NetworkTransform component will be removed at runtime if it is a hybrid prefab that is spawned since
3754+
// it will be using N4E's built in interpolation and extrapolation features.
37523755
if (NetworkObject.HasGhost)
37533756
{
37543757
return;

com.unity.netcode.gameobjects/Runtime/Configuration/NetworkPrefabs.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class NetworkPrefabs
4747
[NonSerialized]
4848
private List<NetworkPrefab> m_Prefabs = new List<NetworkPrefab>();
4949

50-
#if UNIFIED_NETCODE
50+
#if UNIFIED_NETCODE && UNIFIED_NGO_REGISTERS_PREFABS
5151
[NonSerialized]
5252
internal Dictionary<uint, NetworkPrefab> PrefabTable = new Dictionary<uint, NetworkPrefab>();
5353
#endif
@@ -62,7 +62,7 @@ private void AddTriggeredByNetworkPrefabList(NetworkPrefab networkPrefab)
6262
// Don't add this to m_RuntimeAddedPrefabs
6363
// This prefab is now in the PrefabList, so if we shutdown and initialize again, we'll pick it up from there.
6464
m_Prefabs.Add(networkPrefab);
65-
#if UNIFIED_NETCODE
65+
#if UNIFIED_NETCODE && UNIFIED_NGO_REGISTERS_PREFABS
6666
if (!PrefabTable.ContainsKey(networkPrefab.SourcePrefabGlobalObjectIdHash))
6767
{
6868
PrefabTable.Add(networkPrefab.SourcePrefabGlobalObjectIdHash, networkPrefab);
@@ -74,7 +74,7 @@ private void AddTriggeredByNetworkPrefabList(NetworkPrefab networkPrefab)
7474
private void RemoveTriggeredByNetworkPrefabList(NetworkPrefab networkPrefab)
7575
{
7676
m_Prefabs.Remove(networkPrefab);
77-
#if UNIFIED_NETCODE
77+
#if UNIFIED_NETCODE && UNIFIED_NGO_REGISTERS_PREFABS
7878
PrefabTable.Remove(networkPrefab.SourcePrefabGlobalObjectIdHash);
7979
#endif
8080
}
@@ -109,7 +109,7 @@ public void Initialize(bool warnInvalid = true)
109109
{
110110
m_Prefabs.Clear();
111111
NetworkPrefabsLists.RemoveAll(x => x == null);
112-
#if UNIFIED_NETCODE
112+
#if UNIFIED_NETCODE && UNIFIED_NGO_REGISTERS_PREFABS
113113
PrefabTable.Clear();
114114
#endif
115115
foreach (var list in NetworkPrefabsLists)
@@ -144,7 +144,7 @@ public void Initialize(bool warnInvalid = true)
144144
if (AddPrefabRegistration(networkPrefab))
145145
{
146146
m_Prefabs.Add(networkPrefab);
147-
#if UNIFIED_NETCODE
147+
#if UNIFIED_NETCODE && UNIFIED_NGO_REGISTERS_PREFABS
148148
if (!PrefabTable.ContainsKey(networkPrefab.SourcePrefabGlobalObjectIdHash))
149149
{
150150
PrefabTable.Add(networkPrefab.SourcePrefabGlobalObjectIdHash, networkPrefab);
@@ -154,7 +154,7 @@ public void Initialize(bool warnInvalid = true)
154154
else
155155
{
156156
removeList?.Add(networkPrefab);
157-
#if UNIFIED_NETCODE
157+
#if UNIFIED_NETCODE && UNIFIED_NGO_REGISTERS_PREFABS
158158
if (PrefabTable.ContainsKey(networkPrefab.SourcePrefabGlobalObjectIdHash))
159159
{
160160
PrefabTable.Remove(networkPrefab.SourcePrefabGlobalObjectIdHash);
@@ -168,7 +168,7 @@ public void Initialize(bool warnInvalid = true)
168168
if (AddPrefabRegistration(networkPrefab))
169169
{
170170
m_Prefabs.Add(networkPrefab);
171-
#if UNIFIED_NETCODE
171+
#if UNIFIED_NETCODE && UNIFIED_NGO_REGISTERS_PREFABS
172172
if (!PrefabTable.ContainsKey(networkPrefab.SourcePrefabGlobalObjectIdHash))
173173
{
174174
PrefabTable.Add(networkPrefab.SourcePrefabGlobalObjectIdHash, networkPrefab);
@@ -178,7 +178,7 @@ public void Initialize(bool warnInvalid = true)
178178
else
179179
{
180180
removeList?.Add(networkPrefab);
181-
#if UNIFIED_NETCODE
181+
#if UNIFIED_NETCODE && UNIFIED_NGO_REGISTERS_PREFABS
182182
if (PrefabTable.ContainsKey(networkPrefab.SourcePrefabGlobalObjectIdHash))
183183
{
184184
PrefabTable.Remove(networkPrefab.SourcePrefabGlobalObjectIdHash);
@@ -216,7 +216,7 @@ public bool Add(NetworkPrefab networkPrefab)
216216
{
217217
m_Prefabs.Add(networkPrefab);
218218
m_RuntimeAddedPrefabs.Add(networkPrefab);
219-
#if UNIFIED_NETCODE
219+
#if UNIFIED_NETCODE && UNIFIED_NGO_REGISTERS_PREFABS
220220
if (!PrefabTable.ContainsKey(networkPrefab.SourcePrefabGlobalObjectIdHash))
221221
{
222222
PrefabTable.Add(networkPrefab.SourcePrefabGlobalObjectIdHash, networkPrefab);
@@ -249,7 +249,7 @@ public void Remove(NetworkPrefab prefab)
249249
m_RuntimeAddedPrefabs.Remove(prefab);
250250
OverrideToNetworkPrefab.Remove(prefab.TargetPrefabGlobalObjectIdHash);
251251
NetworkPrefabOverrideLinks.Remove(prefab.SourcePrefabGlobalObjectIdHash);
252-
#if UNIFIED_NETCODE
252+
#if UNIFIED_NETCODE && UNIFIED_NGO_REGISTERS_PREFABS
253253
if (PrefabTable.ContainsKey(prefab.SourcePrefabGlobalObjectIdHash))
254254
{
255255
PrefabTable.Remove(prefab.SourcePrefabGlobalObjectIdHash);
@@ -331,14 +331,15 @@ public bool Contains(NetworkPrefab prefab)
331331
}
332332

333333
#if UNIFIED_NETCODE
334+
internal bool HasGhostPrefabs { get; private set; }
335+
336+
#if UNIFIED_NGO_REGISTERS_PREFABS
334337
/// <summary>
335338
/// TODO: Either keep or remove prior to freeze.
336339
/// Leaving this here in case we have to control when things get registered.
337340
/// </summary>
338341
internal bool HasPendingGhostPrefabs { get; private set; }
339-
internal bool HasGhostPrefabs { get; private set; }
340342
private List<NetworkPrefab> m_PendingGhostRegistration = new List<NetworkPrefab>();
341-
342343
/// <summary>
343344
/// UNIFIED-POC<br />
344345
/// Hybrid NetworkObject-Ghost Prefab Registration<br />
@@ -375,6 +376,7 @@ internal void RegisterGhostPrefabs(NetworkManager networkManager)
375376
HasPendingGhostPrefabs = m_PendingGhostRegistration.Count > 0;
376377
}
377378
#endif
379+
#endif
378380

379381

380382
/// <summary>
@@ -398,9 +400,12 @@ private bool AddPrefabRegistration(NetworkPrefab networkPrefab)
398400
#if UNIFIED_NETCODE
399401
if (networkPrefab.HasGhost)
400402
{
401-
//HasPendingGhostPrefabs = true;
402403
HasGhostPrefabs = true;
403-
//m_PendingGhostRegistration.Add(networkPrefab);
404+
405+
#if UNIFIED_NGO_REGISTERS_PREFABS
406+
HasPendingGhostPrefabs = true;
407+
m_PendingGhostRegistration.Add(networkPrefab);
408+
#endif
404409
}
405410
#endif
406411

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
using Unity.Netcode.Components;
1010
using Unity.Netcode.Logging;
1111
using Unity.Netcode.Runtime;
12+
// TODO-UNIFIED: When:
13+
// - N4E is a dependency of Netcode for GameObjects.
14+
// - TestProject has been updated to include N4E.
15+
// - TestProject and Runtime tests have been updated to use UnifiedHost.
16+
// Remove the conditional compilation and just use the namespace.
1217
#if UNIFIED_NETCODE && OUT_OF_BAND_RPC
1318
using Unity.Netcode.Unified;
1419
#endif
@@ -63,7 +68,7 @@ public class NetworkManager : MonoBehaviour, INetworkUpdateSystem
6368

6469
#pragma warning restore IDE1006 // restore naming rule violation check
6570

66-
#if DEVELOPMENT_BUILD || UNITY_EDITOR
71+
#if DEBUG || UNITY_EDITOR
6772
private static List<Type> s_SerializedType = new List<Type>();
6873
// This is used to control the serialized type not optimized messaging for integration test purposes
6974
internal static bool DisableNotOptimizedSerializedType;
@@ -1261,7 +1266,7 @@ internal void Initialize(bool server)
12611266

12621267
MessageManager.Hook(new NetworkManagerHooks(this));
12631268

1264-
#if DEVELOPMENT_BUILD || UNITY_EDITOR
1269+
#if DEBUG || UNITY_EDITOR
12651270
if (NetworkConfig.NetworkProfilingMetrics)
12661271
{
12671272
MessageManager.Hook(new ProfilingHooks());
@@ -1379,6 +1384,10 @@ internal void InitializeNetcodeWorld()
13791384
DefaultWorldInitialization.Initialize("Default World", false);
13801385
}
13811386

1387+
/// <summary>
1388+
/// Checks to make sure the NetcodeConfig is configured correctly for hybrid mode. Hybrid mode requires a single world to be used for the NetcodeConfig.
1389+
/// </summary>
1390+
/// <returns>True if the configuration is correct; otherwise, false.</returns>
13821391
private bool UnifiedIsConfiguredCorrectly()
13831392
{
13841393
if (NetCodeConfig.Global == null)

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,7 +2686,6 @@ internal static void CheckOrphanChildren()
26862686

26872687
internal void InvokeBehaviourNetworkPreSpawn()
26882688
{
2689-
26902689
var networkManager = NetworkManager;
26912690
InitializeChildNetworkBehaviours();
26922691
foreach (var childBehaviour in ChildNetworkBehaviours.Values)
@@ -2794,6 +2793,13 @@ internal string GenerateDisabledNetworkBehaviourWarning(NetworkBehaviour network
27942793

27952794
internal Dictionary<ushort, NetworkBehaviour> ChildNetworkBehaviours;
27962795

2796+
/// <summary>
2797+
/// TODO-UNIFIED:
2798+
/// We should pre-calculate the index id's in the editor and save out two lists:
2799+
/// - All <see cref="NetworkBehaviour"/> derived components in a pre-determined order.
2800+
/// - All of the identifiers aligned with the above list
2801+
/// Then construct the dictionar during awake.
2802+
/// </summary>
27972803
internal bool InitializeChildNetworkBehaviours()
27982804
{
27992805
ChildNetworkBehaviours = new Dictionary<ushort, NetworkBehaviour>();
@@ -2838,13 +2844,22 @@ internal bool InitializeChildNetworkBehaviours()
28382844
// This allows a user to not have to make direct adjustments until trying out their NGO prefab
28392845
// as a hybrid spawned prefab (optional to completely remove, will eventually become obsolete and
28402846
// automatically removed later).
2841-
if (HasGhost)
2847+
if (HasGhost && !NetworkManager.DistributedAuthorityMode)
28422848
{
2843-
28442849
if (NetworkRigidbodies != null)
28452850
{
28462851
for (int i = NetworkRigidbodies.Count - 1; i >= 0; i--)
28472852
{
2853+
// TODO-UNIFIED: This needs to be updated to make it "opt-in".
2854+
// Only clients remove the rigidbody for performance purposes when running a hybrid spawn client-server topology.
2855+
if (!NetworkManager.IsServer)
2856+
{
2857+
var rigidBody = NetworkRigidbodies[i].gameObject.GetComponent<Rigidbody>();
2858+
if (rigidBody != null)
2859+
{
2860+
Destroy(rigidBody);
2861+
}
2862+
}
28482863
ChildNetworkBehaviours.Remove(NetworkRigidbodies[i].NetworkBehaviourId);
28492864
Destroy(NetworkRigidbodies[i]);
28502865
}

0 commit comments

Comments
 (0)