Skip to content

Commit f19e6ef

Browse files
fix: reject spawning a hybrid instance in a session without hybrid prefabs
Only prefab registration rejected hybrid prefabs. An unregistered hybrid prefab instance, for example one spawned through a prefab handler, spawned into a session that never entered hybrid mode and ran its ghost spawn, ownership and bridge code without a unified world. The spawn authority now logs an error and refuses the spawn.
1 parent 0c35f08 commit f19e6ef

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ public bool Contains(NetworkPrefab prefab)
368368
#if UNIFIED_NETCODE
369369
internal const string DistributedAuthorityHybridPrefabError = "Distributed authority does not support hybrid prefabs.";
370370
internal const string HybridPrefabAfterStartError = "Hybrid prefabs must be registered before the NetworkManager starts.";
371+
internal const string HybridSpawnWithoutHybridPrefabsError = "Hybrid prefab instances can only be spawned in a session that started with a hybrid prefab registered.";
371372

372373
internal bool HasGhostPrefabs { get; private set; }
373374

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,18 @@ internal bool AuthorityLocalSpawn([NotNull] NetworkObject networkObject, ulong n
11711171
return false;
11721172
}
11731173

1174+
#if UNIFIED_NETCODE
1175+
// Only registering a hybrid prefab before the session starts puts the NetworkManager in hybrid mode.
1176+
if (networkObject.HasGhost && !NetworkManager.NetworkConfig.Prefabs.HasGhostPrefabs)
1177+
{
1178+
if (NetworkManager.LogLevel <= LogLevel.Error)
1179+
{
1180+
NetworkLog.LogError($"{NetworkPrefabs.HybridSpawnWithoutHybridPrefabsError} {networkObject.name} was not spawned.");
1181+
}
1182+
return false;
1183+
}
1184+
#endif
1185+
11741186
if (playerObject && networkObject.InScenePlaced)
11751187
{
11761188
NetworkLog.LogError(new Context(LogLevel.Developer, "Player prefab is marked as belonging to a scene. This may cause issues.").AddNetworkObject(networkObject).AddInfo("SceneName", networkObject.SceneOrigin.name));

‎com.unity.netcode.gameobjects/Tests/Runtime/NetworkObject/UnifiedHybridPrefabValidationTests.cs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace Unity.Netcode.RuntimeTests
1414
/// Validates the hybrid prefab registrations that are rejected:<br />
1515
/// - Any hybrid prefab in a distributed authority session. The start fails, and one added during the session is not registered.<br />
1616
/// - A hybrid prefab added during a session that started without hybrid prefabs. It is not registered.<br />
17+
/// - A hybrid prefab instance spawned during a session that started without hybrid prefabs. It is not spawned.<br />
1718
/// - A hybrid player prefab counts toward hybrid mode before the session starts, so distributed authority rejects it.<br />
1819
/// - A NetworkPrefab override with a hybrid source or target prefab, in any topology. The entry is ignored.<br />
1920
/// </summary>
@@ -152,6 +153,29 @@ public IEnumerator HybridPrefabAddedAfterStartIsRejected()
152153
Assert.IsTrue(prefabs.Contains(m_HybridPrefab), "The hybrid prefab was rejected after the session ended!");
153154
}
154155

156+
/// <summary>
157+
/// Validates that an unregistered hybrid prefab instance, for example one spawned through a prefab handler, is not spawned in a session that started without hybrid prefabs.
158+
/// </summary>
159+
[UnityTest]
160+
public IEnumerator HybridSpawnWithoutHybridPrefabsIsRejected()
161+
{
162+
var networkManager = CreateNetworkManager(NetworkTopologyTypes.ClientServer);
163+
Assert.IsTrue(networkManager.StartHost(), "Failed to start the session!");
164+
165+
var instance = UnityEngine.Object.Instantiate(m_HybridPrefab).GetComponent<NetworkObject>();
166+
instance.NetworkManagerOwner = networkManager;
167+
LogAssert.Expect(LogType.Error, new Regex($"{Regex.Escape(NetworkPrefabs.HybridSpawnWithoutHybridPrefabsError)}.*{Regex.Escape(instance.name)}"));
168+
instance.Spawn();
169+
var isSpawned = instance.IsSpawned;
170+
var isInSpawnedObjects = networkManager.SpawnManager.SpawnedObjectsList.Contains(instance);
171+
172+
// Destroyed prior to asserting, so a failed assert leaves no instance behind and an error logged by its destruction fails this test.
173+
UnityEngine.Object.Destroy(instance.gameObject);
174+
yield return null;
175+
Assert.IsFalse(isSpawned, "The hybrid instance was spawned in a session that started without hybrid prefabs!");
176+
Assert.IsFalse(isInSpawnedObjects, "The hybrid instance was added to the spawned objects!");
177+
}
178+
155179
/// <summary>
156180
/// A hybrid <see cref="NetworkConfig.PlayerPrefab"/> that is in no prefab list still decides hybrid mode before the session starts.
157181
/// </summary>

0 commit comments

Comments
 (0)