Skip to content

Commit 42ce1f1

Browse files
committed
Fix test
1 parent ae5c41a commit 42ce1f1

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

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

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ internal void SpawnNetworkObjectLocallyCommon(NetworkObject networkObject, ulong
11601160
// If this the player and the client is the owner, then lock ownership by default
11611161
if (NetworkManager.DistributedAuthorityMode && NetworkManager.LocalClientId == ownerClientId && playerObject)
11621162
{
1163-
networkObject.SetOwnershipLock();
1163+
networkObject.AddOwnershipExtended(NetworkObject.OwnershipStatusExtended.Locked);
11641164
}
11651165

11661166
networkObject.IsSpawned = true;
@@ -1400,46 +1400,53 @@ internal void DespawnAndDestroyNetworkObjects()
14001400
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>();
14011401
#endif
14021402

1403-
for (int i = 0; i < networkObjects.Length; i++)
1403+
foreach (var networkObject in networkObjects)
14041404
{
1405-
if (networkObjects[i].NetworkManager == NetworkManager)
1405+
// We are not the authority of this NetworkObject
1406+
// Mostly used for integration testing
1407+
if (networkObject.NetworkManager != NetworkManager)
14061408
{
1407-
if (NetworkManager.PrefabHandler.ContainsHandler(networkObjects[i]))
1408-
{
1409-
OnDespawnObject(networkObjects[i], false);
1410-
// Leave destruction up to the handler
1411-
NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(networkObjects[i]);
1412-
}
1413-
else
1414-
{
1415-
// If it is an in-scene placed NetworkObject then just despawn and let it be destroyed when the scene
1416-
// is unloaded. Otherwise, despawn and destroy it.
1417-
var shouldDestroy = !(networkObjects[i].IsSceneObject == null || (networkObjects[i].IsSceneObject != null && networkObjects[i].IsSceneObject.Value));
1409+
continue;
1410+
}
1411+
1412+
// The NetworkManagerOwner field must be set before calling OnDespawnObject
1413+
networkObject.NetworkManagerOwner = NetworkManager;
1414+
1415+
if (NetworkManager.PrefabHandler.ContainsHandler(networkObject))
1416+
{
1417+
OnDespawnObject(networkObject, false);
1418+
// Leave destruction up to the handler
1419+
NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(networkObject);
1420+
}
1421+
else
1422+
{
1423+
// If it is an in-scene placed NetworkObject then just despawn and let it be destroyed when the scene
1424+
// is unloaded. Otherwise, despawn and destroy it.
1425+
var shouldDestroy = !(networkObject.IsSceneObject == null || (networkObject.IsSceneObject != null && networkObject.IsSceneObject.Value));
14181426

1419-
// If we are going to destroy this NetworkObject, check for any in-scene placed children that need to be removed
1420-
if (shouldDestroy)
1427+
// If we are going to destroy this NetworkObject, check for any in-scene placed children that need to be removed
1428+
if (shouldDestroy)
1429+
{
1430+
// Check to see if there are any in-scene placed children that are marked to be destroyed with the scene
1431+
var childrenObjects = networkObject.GetComponentsInChildren<NetworkObject>();
1432+
foreach (var childObject in childrenObjects)
14211433
{
1422-
// Check to see if there are any in-scene placed children that are marked to be destroyed with the scene
1423-
var childrenObjects = networkObjects[i].GetComponentsInChildren<NetworkObject>();
1424-
foreach (var childObject in childrenObjects)
1434+
if (childObject == networkObject)
14251435
{
1426-
if (childObject == networkObjects[i])
1427-
{
1428-
continue;
1429-
}
1436+
continue;
1437+
}
14301438

1431-
// If the child is an in-scene placed NetworkObject then remove the child from the parent (which was dynamically spawned)
1432-
// and set its parent to root
1433-
if (childObject.IsSceneObject != null && childObject.IsSceneObject.Value)
1434-
{
1435-
childObject.TryRemoveParent(childObject.WorldPositionStays());
1436-
}
1439+
// If the child is an in-scene placed NetworkObject then remove the child from the parent (which was dynamically spawned)
1440+
// and set its parent to root
1441+
if (childObject.IsSceneObject != null && childObject.IsSceneObject.Value)
1442+
{
1443+
childObject.TryRemoveParent(childObject.WorldPositionStays());
14371444
}
14381445
}
1439-
1440-
//Despawn and potentially destroy.
1441-
OnDespawnObject(networkObjects[i], shouldDestroy);
14421446
}
1447+
1448+
//Despawn and potentially destroy.
1449+
OnDespawnObject(networkObject, shouldDestroy);
14431450
}
14441451
}
14451452
}

0 commit comments

Comments
 (0)