Skip to content

Commit 10220e4

Browse files
committed
- Ensure NetworkManager.Singleton is always the server when there are multiple NetworkManagers
- Ensure UnifiedBootstrap doesn't get replaced when setting up a new NetworkManager, as that clears out the list of network worlds.
1 parent c5b4512 commit 10220e4

4 files changed

Lines changed: 12 additions & 34 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,10 @@ internal static void ResetSingleton()
10921092
/// </summary>
10931093
public void SetSingleton()
10941094
{
1095-
Singleton = this;
1095+
if (!Singleton || !Singleton.IsServer)
1096+
{
1097+
Singleton = this;
1098+
}
10961099

10971100
OnSingletonReady?.Invoke();
10981101
}
@@ -1401,7 +1404,14 @@ internal void InitializeNetcodeWorld()
14011404
/// !! Initialize worlds here !!
14021405
/// Worlds are created here: <see cref="UnifiedBootStrap.Initialize"/>
14031406
UnifiedBootstrap.CurrentNetworkManagerForInitialization = this;
1404-
DefaultWorldInitialization.Initialize("Default World", false);
1407+
if (UnifiedBootstrap.Instance != null)
1408+
{
1409+
UnifiedBootstrap.Instance.Initialize("Default World");
1410+
}
1411+
else
1412+
{
1413+
DefaultWorldInitialization.Initialize("Default World", false);
1414+
}
14051415
}
14061416

14071417
/// <summary>

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -939,14 +939,6 @@ internal NetworkObject GetNetworkObjectToSpawn(uint globalObjectIdHash, ulong ow
939939
/// <returns>the instance of the <see cref="NetworkObject"/></returns>
940940
internal NetworkObject InstantiateNetworkPrefab([NotNull] GameObject networkPrefab, uint prefabGlobalObjectIdHash, Vector3? position, Quaternion? rotation)
941941
{
942-
#if UNIFIED_NETCODE
943-
// TODO-FixMe: NetCode.Netcode.Instance is a singleton and might cause issues
944-
// assigning this.
945-
if (networkPrefab.GetComponent<NetworkObject>().HasGhost)
946-
{
947-
NetCode.Netcode.Instance.m_ActiveWorld = NetworkManager.NetcodeWorld;
948-
}
949-
#endif
950942
var gameObject = Object.Instantiate(networkPrefab);
951943
var networkObject = gameObject.GetComponent<NetworkObject>();
952944
if (networkObject == null)

com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/NetcodeIntegrationTest.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,14 +2449,6 @@ internal void SpawnInstanceWithOwnership(NetworkObject networkObjectToSpawn, Net
24492449
}
24502450
else
24512451
{
2452-
#if UNIFIED_NETCODE
2453-
// TODO-FixMe: NetCode.Netcode.Instance is a singleton and might cause issues
2454-
// assigning this.
2455-
if (networkObjectToSpawn.HasGhost)
2456-
{
2457-
NetCode.Netcode.Instance.m_ActiveWorld = m_ServerNetworkManager.NetcodeWorld;
2458-
}
2459-
#endif
24602452
networkObjectToSpawn.NetworkManagerOwner = m_ServerNetworkManager; // Required to assure the server does the spawning
24612453
if (spawnAuthority == m_ServerNetworkManager)
24622454
{
@@ -2513,14 +2505,6 @@ protected void SpawnObjectInstance(NetworkObject networkObjectToSpawn, NetworkMa
25132505
/// <returns>GameObject instance spawned</returns>
25142506
private GameObject SpawnObject(NetworkObject prefabNetworkObject, NetworkManager owner, bool destroyWithScene = false, bool isPlayerObject = false)
25152507
{
2516-
#if UNIFIED_NETCODE
2517-
// TODO-FixMe: NetCode.Netcode.Instance is a singleton and might cause issues
2518-
// assigning this.
2519-
if (prefabNetworkObject.HasGhost)
2520-
{
2521-
NetCode.Netcode.Instance.m_ActiveWorld = owner.NetcodeWorld;
2522-
}
2523-
#endif
25242508
Assert.IsTrue(prefabNetworkObject.GlobalObjectIdHash > 0, $"{nameof(GameObject)} {prefabNetworkObject.name} has a {nameof(NetworkObject.GlobalObjectIdHash)} value of 0! Make sure to make it a valid prefab before trying to spawn!");
25252509
var newInstance = Object.Instantiate(prefabNetworkObject.gameObject);
25262510
var networkObjectToSpawn = newInstance.GetComponent<NetworkObject>();

testproject/Assets/Tests/Runtime/Support/NetworkObjectSpawnerForTests.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,6 @@ private void SpawnRpc(RpcParams rpcParams = default)
7878

7979
private void Spawn(ulong ownerId)
8080
{
81-
#if UNIFIED_NETCODE
82-
// TODO-FixMe: NetCode.Netcode.Instance is a singleton and might cause issues
83-
// assigning this.
84-
if (ObjectToSpawn.GetComponent<NetworkObject>().HasGhost)
85-
{
86-
Netcode.Instance.m_ActiveWorld = NetworkManager.NetcodeWorld;
87-
}
88-
#endif
8981
var instance = Instantiate(ObjectToSpawn);
9082
var networkObject = instance.GetComponent<NetworkObject>();
9183
networkObject.SpawnWithOwnership(ownerId);

0 commit comments

Comments
 (0)