Skip to content

Commit c5b4512

Browse files
committed
Fix broken hybrid spawn tests
1 parent 122db34 commit c5b4512

5 files changed

Lines changed: 36 additions & 8 deletions

File tree

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3736,7 +3736,13 @@ private void Start()
37363736

37373737
private void InitGhost()
37383738
{
3739-
if (!NetworkManager.IsListening)
3739+
if (!HasGhost || !NetworkObjectBridge || GhostAdapter.IsPrefab())
3740+
{
3741+
// Nothing to register
3742+
return;
3743+
}
3744+
3745+
if (!NetworkManager || !NetworkManager.IsListening)
37403746
{
37413747
if (NetworkManager.LogLevel == LogLevel.Developer)
37423748
{
@@ -3745,12 +3751,6 @@ private void InitGhost()
37453751
return;
37463752
}
37473753

3748-
if (!HasGhost || !NetworkObjectBridge || GhostAdapter.IsPrefab())
3749-
{
3750-
// Nothing to register
3751-
return;
3752-
}
3753-
37543754
// All instances with Ghosts are automatically registered
37553755
if (NetworkManager.LogLevel == LogLevel.Developer)
37563756
{

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,14 @@ 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
942950
var gameObject = Object.Instantiate(networkPrefab);
943951
var networkObject = gameObject.GetComponent<NetworkObject>();
944952
if (networkObject == null)

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,6 +2513,14 @@ protected void SpawnObjectInstance(NetworkObject networkObjectToSpawn, NetworkMa
25132513
/// <returns>GameObject instance spawned</returns>
25142514
private GameObject SpawnObject(NetworkObject prefabNetworkObject, NetworkManager owner, bool destroyWithScene = false, bool isPlayerObject = false)
25152515
{
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
25162524
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!");
25172525
var newInstance = Object.Instantiate(prefabNetworkObject.gameObject);
25182526
var networkObjectToSpawn = newInstance.GetComponent<NetworkObject>();

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System.Collections.Generic;
22
using Unity.Netcode;
33
using UnityEngine;
4+
#if UNIFIED_NETCODE
5+
using Unity.NetCode;
6+
#endif
47

58
namespace TestProject.RuntimeTests
69
{
@@ -75,6 +78,14 @@ private void SpawnRpc(RpcParams rpcParams = default)
7578

7679
private void Spawn(ulong ownerId)
7780
{
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
7889
var instance = Instantiate(ObjectToSpawn);
7990
var networkObject = instance.GetComponent<NetworkObject>();
8091
networkObject.SpawnWithOwnership(ownerId);

testproject/Assets/Tests/Runtime/TestProject.Runtime.Tests.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"Unity.Mathematics",
1616
"UnityEngine.TestRunner",
1717
"UnityEditor.TestRunner",
18-
"Unity.Netcode.Runtime.Tests"
18+
"Unity.Netcode.Runtime.Tests",
19+
"Unity.NetCode"
1920
],
2021
"includePlatforms": [],
2122
"excludePlatforms": [],

0 commit comments

Comments
 (0)