Skip to content

Commit d759121

Browse files
update
Adding support within NetworkPrefabs and NetworkPrefab to handle the registration of hybrid spawned objects (i.e. no more hacky-way of registering). Moving the hybrid spawn registration into the Start method of the NetworkObject to provide time for everything to run through Awake. Added some script (defined out) that was helping to debug why NetworkObject was being disabled sometimes (moving the initialization to Start resolved the issue).
1 parent dd4a95c commit d759121

5 files changed

Lines changed: 104 additions & 16 deletions

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ public class NetworkPrefab
5757
/// </summary>
5858
public GameObject OverridingTargetPrefab;
5959

60+
#if UNIFIED_NETCODE
61+
/// <summary>
62+
/// Used to determine if this prefab needs to be registered
63+
/// via the unified API.
64+
/// </summary>
65+
internal bool HasGhost { get; private set; }
66+
#endif
67+
6068
/// <summary>
6169
/// Compares this NetworkPrefab with another to determine equality
6270
/// </summary>
@@ -166,6 +174,11 @@ public bool Validate(int index = -1)
166174
return false;
167175
}
168176

177+
#if UNIFIED_NETCODE
178+
// Mark this network prefab as having to be registered via the unified API
179+
HasGhost = networkObject.HasGhost;
180+
#endif
181+
169182
return true;
170183
}
171184

@@ -183,7 +196,6 @@ public bool Validate(int index = -1)
183196

184197
return false;
185198
}
186-
187199
break;
188200
}
189201
case NetworkPrefabOverride.Prefab:

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,48 @@ public bool Contains(NetworkPrefab prefab)
277277
return false;
278278
}
279279

280+
#if UNIFIED_NETCODE
281+
internal bool HasPendingGhostPrefabs { get; private set; }
282+
private List<NetworkPrefab> m_PendingGhostRegistration = new List<NetworkPrefab>();
283+
284+
/// <summary>
285+
/// UNIFIED-POC<br />
286+
/// Hybrid NetworkObject-Ghost Prefab Registration<br />
287+
/// </summary>
288+
/// <remarks>
289+
/// When <see cref="NetworkObject.HasGhost"/> is true, <see cref="NetworkPrefab"/>s
290+
/// will mark themselves as having a ghost during <see cref="NetworkPrefab.Validate(int)"/>.
291+
/// After validation, if a network prefab's <see cref="NetworkPrefab.HasGhost"/> value is
292+
/// set, then it is added to <see cref="m_PendingGhostRegistration"/>.
293+
/// Within <see cref="NetworkManager.NetworkUpdate(NetworkUpdateStage)"/> during the <see cref="NetworkUpdateStage.EarlyUpdate"/>,
294+
/// if <see cref="HasPendingGhostPrefabs"/> is true then <see cref="RegisterGhostPrefabs(NetworkManager)"/> will be invoked.
295+
/// This will repeat until the hosted single world instance is created.
296+
/// </remarks>
297+
/// <param name="networkManager"></param>
298+
internal void RegisterGhostPrefabs(NetworkManager networkManager)
299+
{
300+
if (!HasPendingGhostPrefabs)
301+
{
302+
Debug.LogWarning($"Should not be invoking!");
303+
return;
304+
}
305+
var isHost = networkManager.IsHost;
306+
for (int i = m_PendingGhostRegistration.Count - 1; i >= 0; i--)
307+
{
308+
var networkPrefab = m_PendingGhostRegistration[i];
309+
310+
// Returns false if the single world is not available yet
311+
if (NetCode.Netcode.RegisterPrefabSingleWorld(networkPrefab.Prefab, isHost))
312+
{
313+
Debug.Log($"[{nameof(NetworkPrefabs)}][{nameof(RegisterGhostPrefabs)}] Registered hybrid spawned object: {networkPrefab.Prefab.name}");
314+
m_PendingGhostRegistration.RemoveAt(i);
315+
}
316+
}
317+
HasPendingGhostPrefabs = m_PendingGhostRegistration.Count > 0;
318+
}
319+
#endif
320+
321+
280322
/// <summary>
281323
/// Configures <see cref="NetworkPrefabOverrideLinks"/> for the given <see cref="NetworkPrefab"/>
282324
/// </summary>
@@ -295,6 +337,14 @@ private bool AddPrefabRegistration(NetworkPrefab networkPrefab)
295337
uint source = networkPrefab.SourcePrefabGlobalObjectIdHash;
296338
uint target = networkPrefab.TargetPrefabGlobalObjectIdHash;
297339

340+
#if UNIFIED_NETCODE
341+
if (networkPrefab.HasGhost)
342+
{
343+
HasPendingGhostPrefabs = true;
344+
m_PendingGhostRegistration.Add(networkPrefab);
345+
}
346+
#endif
347+
298348
// Make sure the prefab isn't already registered.
299349
if (NetworkPrefabOverrideLinks.ContainsKey(source))
300350
{

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Runtime.CompilerServices;
43
using Unity.Collections;
54
using UnityEngine;
65

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,15 @@ public void NetworkUpdate(NetworkUpdateStage updateStage)
333333
{
334334
case NetworkUpdateStage.EarlyUpdate:
335335
{
336+
#if UNIFIED_NETCODE
337+
// Temporary work around for handling the registration
338+
// of hybrid spawned objects.
339+
if (NetworkConfig.Prefabs.HasPendingGhostPrefabs)
340+
{
341+
NetworkConfig.Prefabs.RegisterGhostPrefabs(this);
342+
}
343+
#endif
344+
336345
UpdateTopology();
337346

338347
// Handle processing any new connections or transport events

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

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3534,35 +3534,53 @@ private void Awake()
35343534
#endif
35353535
SetCachedParent(transform.parent);
35363536
SceneOrigin = gameObject.scene;
3537-
#if UNIFIED_NETCODE
3538-
InitGhost();
3539-
#endif
35403537

35413538
}
35423539

3543-
//private void OnEnable()
3544-
//{
3545-
// Debug.Log("Enabled!");
3546-
//}
3540+
#if UNIFIED_NETCODE
35473541

3548-
//private void OnDisable()
3549-
//{
3550-
// Debug.Log("Disabled!");
3551-
//}
3542+
#if DEBUG_ENABLE_DISABLE
3543+
private void OnEnable()
3544+
{
3545+
Debug.Log("Enabled!");
3546+
}
35523547

3553-
#if UNIFIED_NETCODE
3548+
private void OnDisable()
3549+
{
3550+
Debug.Log("Disabled!");
3551+
if (IsSpawned)
3552+
{
3553+
enabled = true;
3554+
}
3555+
3556+
try
3557+
{
3558+
throw new Exception("Disabled trap!");
3559+
}
3560+
catch (Exception ex)
3561+
{
3562+
Debug.LogWarning($"[{name}][{ex.Message}] Callstack:\n{ex.StackTrace}");
3563+
}
3564+
}
3565+
#endif
35543566

35553567
private void Start()
35563568
{
3557-
enabled = true;
3569+
// TODO-UNIFIED: Remove once the prefab registration is in place.
3570+
if (!enabled)
3571+
{
3572+
Debug.LogWarning($"[{nameof(NetworkObject)}][{name}] Was not enabled on start! Enabling.");
3573+
enabled = true;
3574+
}
3575+
3576+
InitGhost();
35583577
}
35593578
[SerializeField]
35603579
[HideInInspector]
35613580
internal NetworkObjectBridge NetworkObjectBridge;
35623581

35633582
private void InitGhost()
35643583
{
3565-
enabled = true;
35663584
// All instances with Ghosts are automatically registered
35673585
if (HasGhost && NetworkObjectBridge)
35683586
{

0 commit comments

Comments
 (0)