Skip to content

Commit da1beaf

Browse files
update
Fixing hybrid prefab registration and world initialization order.
1 parent 5802407 commit da1beaf

File tree

3 files changed

+48
-31
lines changed

3 files changed

+48
-31
lines changed

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

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#if UNIFIED_NETCODE
22
using System;
3+
using Unity.Entities;
34
using Unity.NetCode;
45

56
namespace Unity.Netcode
@@ -25,15 +26,15 @@ public override void Awake()
2526
{
2627
if (UnifiedBootStrap.Instance != null)
2728
{
28-
Initialize(true);
29+
Initialize();
2930
}
3031
else
3132
{
3233
UnifiedBootStrap.OnInitialized += Initialize;
3334
}
3435
}
3536

36-
private void Initialize(bool initialized)
37+
private void Initialize()
3738
{
3839
UnifiedBootStrap.OnInitialized -= Initialize;
3940
if (gameObject != null)
@@ -71,33 +72,53 @@ public override void OnDestroy()
7172
internal class UnifiedBootStrap : ClientServerBootstrap
7273
{
7374
public static UnifiedBootStrap Instance { get; private set; }
74-
public static Action<bool> OnInitialized;
75+
public static Action OnInitialized;
7576
public static ushort Port = 7979;
7677

78+
public static World World { get; private set; }
79+
7780
public override bool Initialize(string defaultWorldName)
7881
{
7982
var networkManager = NetworkManager.Singleton;
8083
Instance = this;
8184
AutoConnectPort = Port;
82-
if (networkManager.IsServer)
85+
if (base.Initialize(defaultWorldName))
8386
{
84-
CreateSingleWorldHost("ClientAndServerWorld");
85-
if (networkManager.LogLevel <= LogLevel.Developer)
86-
{
87-
UnityEngine.Debug.Log("Creating world: ClientAndServerWorld");
88-
}
87+
UnityEngine.Debug.LogError($"[{nameof(UnifiedBootStrap)}] Auto-bootstrap is enabled!!! This will break the POC!");
88+
return true;
8989
}
90-
else
90+
91+
World = networkManager.IsServer ? CreateSingleWorldHost("ClientAndServerWorld") : CreateClientWorld("ClientWorld");
92+
93+
if (World == null)
94+
{
95+
UnityEngine.Debug.LogError($"[{nameof(UnifiedBootStrap)}] World is null!");
96+
return false;
97+
}
98+
99+
if (!World.IsCreated)
100+
{
101+
UnityEngine.Debug.LogError($"[{nameof(UnifiedBootStrap)}] World was not created!");
102+
return false;
103+
}
104+
105+
if (networkManager.LogLevel <= LogLevel.Developer)
106+
{
107+
NetworkLog.LogInfo($"[{nameof(UnifiedBootStrap)}] Created world: {World.Name}");
108+
}
109+
110+
if (networkManager.NetworkConfig.Prefabs.HasPendingGhostPrefabs)
91111
{
92-
CreateClientWorld("ClientWorld");
93112
if (networkManager.LogLevel <= LogLevel.Developer)
94113
{
95-
UnityEngine.Debug.Log("Creating world: ClientWorld");
114+
NetworkLog.LogInfo($"[{nameof(UnifiedBootStrap)}] Registering hybrid prefabs...");
96115
}
116+
networkManager.NetworkConfig.Prefabs.RegisterGhostPrefabs(networkManager);
97117
}
98-
var initialized = base.Initialize(defaultWorldName);
99-
OnInitialized?.Invoke(initialized);
100-
return initialized;
118+
119+
OnInitialized?.Invoke();
120+
121+
return true;
101122
}
102123

103124
~UnifiedBootStrap()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected override void OnUpdate()
5858
// client has signaled that it has synchronized (or has been sent the synchronization data) we finalize the in-game connection state (or something along those lines).
5959
if (!m_NewConnections.ContainsKey(networkId.Value))
6060
{
61-
var delayTime = isServer ? 0.05f : 0.1f;
61+
var delayTime = isServer ? 0.2f : 0.1f;
6262
var newConnection = new NetcodeConnection { World = World, Entity = entity, NetworkId = networkId.Value, ConnectedTime = UnityEngine.Time.realtimeSinceStartup + delayTime};
6363
m_NewConnections.Add(networkId.Value, newConnection);
6464
}

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,6 @@ public void NetworkUpdate(NetworkUpdateStage updateStage)
341341
{
342342
case NetworkUpdateStage.EarlyUpdate:
343343
{
344-
#if UNIFIED_NETCODE
345-
// Temporary work around for handling the registration
346-
// of hybrid spawned objects.
347-
if (NetworkConfig.Prefabs.HasPendingGhostPrefabs)
348-
{
349-
NetworkConfig.Prefabs.RegisterGhostPrefabs(this);
350-
}
351-
#endif
352-
353344
UpdateTopology();
354345

355346
// Handle processing any new connections or transport events
@@ -1319,14 +1310,22 @@ private bool CanStart(StartType type)
13191310
#if UNIFIED_NETCODE
13201311
private System.Collections.IEnumerator WaitForHybridPrefabRegistration(StartType startType)
13211312
{
1313+
DefaultWorldInitialization.Initialize("Default World", false);
1314+
var waitTime = new WaitForSeconds(0.016f);
1315+
// This should not be needed at this point, but here in the event something changes.
13221316
while (NetworkConfig.Prefabs.HasPendingGhostPrefabs)
13231317
{
1324-
yield return null;
1318+
if (LogLevel <= LogLevel.Developer)
1319+
{
1320+
NetworkLog.LogInfo($"[{nameof(WaitForHybridPrefabRegistration)}] Ghosts are still pending registration!");
1321+
}
1322+
NetworkConfig.Prefabs.RegisterGhostPrefabs(this);
1323+
yield return waitTime;
13251324
}
13261325
if (LogLevel <= LogLevel.Developer)
13271326
{
1328-
Debug.Log("All hybrid prefabs have been registered!");
1329-
Debug.Log("Finalizing NetworkManager start...");
1327+
NetworkLog.LogInfo($"[{nameof(WaitForHybridPrefabRegistration)}] All hybrid prefabs have been registered!");
1328+
NetworkLog.LogInfo($"[{nameof(WaitForHybridPrefabRegistration)}] Finalizing NetworkManager start...");
13301329
}
13311330
switch (startType)
13321331
{
@@ -1413,7 +1412,6 @@ public bool StartServer()
14131412
{
14141413
Debug.Log("Creating world: Default world");
14151414
}
1416-
DefaultWorldInitialization.Initialize("Default World", false);
14171415
StartCoroutine(WaitForHybridPrefabRegistration(StartType.Server));
14181416
return true;
14191417
}
@@ -1501,7 +1499,6 @@ public bool StartClient()
15011499
{
15021500
Debug.Log("Creating world: Default world");
15031501
}
1504-
DefaultWorldInitialization.Initialize("Default World", false);
15051502
StartCoroutine(WaitForHybridPrefabRegistration(StartType.Client));
15061503
// TODO-UNIFIED: Need a way to signal everything completed.
15071504
return true;
@@ -1589,7 +1586,6 @@ public bool StartHost()
15891586
{
15901587
Debug.Log("Creating world: Default world");
15911588
}
1592-
DefaultWorldInitialization.Initialize("Default World", false);
15931589
StartCoroutine(WaitForHybridPrefabRegistration(StartType.Host));
15941590
// TODO-UNIFIED: Need a way to signal everything completed.
15951591
return true;

0 commit comments

Comments
 (0)