Skip to content

Commit 54300e1

Browse files
committed
Merge branch 'chore/simplify-accessors-network-object' of https://github.com/Unity-Technologies/com.unity.netcode.gameobjects into chore/simplify-accessors-network-object
2 parents 13be3cc + 72b3f4f commit 54300e1

File tree

2 files changed

+63
-62
lines changed

2 files changed

+63
-62
lines changed

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

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,43 +1750,37 @@ private void OnDestroy()
17501750
return;
17511751
}
17521752

1753-
// An authorized destroy is when done by the authority instance or done due to a scene event and the NetworkObject
1754-
// was marked as destroy pending scene event (which means the destroy with scene property was set).
1755-
if (IsSpawned && !(HasAuthority || networkManager.DAHost || DestroyPendingSceneEvent) && networkManager.IsListening &&
1756-
(IsSceneObject == null || IsSceneObject.Value != true))
1757-
{
1758-
// If we destroyed a GameObject with a NetworkObject component on the non-authority side, handle cleaning up the SceneMigrationSynchronization.
1759-
networkManager.SpawnManager?.RemoveNetworkObjectFromSceneChangedUpdates(this);
1760-
1761-
// Clients should not despawn NetworkObjects while connected to a session, but we don't want to destroy the current call stack
1762-
// if this happens. Instead, we should just generate a network log error and exit early (as long as we are not shutting down).
1763-
if (!networkManager.ShutdownInProgress)
1764-
{
1765-
// Since we still have a session connection, log locally and on the server to inform user of this issue.
1766-
// If the NetworkObject's GameObject is not valid or the scene is no longer valid or loaded, then this was due to the
1767-
// unloading of a scene which is done by the authority...
1768-
if (gameObject != null && gameObject.scene.IsValid() && gameObject.scene.isLoaded)
1753+
// Always attempt to remove from scene changed updates
1754+
networkManager.SpawnManager?.RemoveNetworkObjectFromSceneChangedUpdates(this);
1755+
1756+
if (IsSpawned && !networkManager.ShutdownInProgress)
1757+
{
1758+
// An authorized destroy is when done by the authority instance or done due to a scene event and the NetworkObject
1759+
// was marked as destroy pending scene event (which means the destroy with scene property was set).
1760+
var isAuthorityDestroy = HasAuthority || NetworkManager.DAHost || DestroyPendingSceneEvent;
1761+
1762+
// If the NetworkObject's GameObject is still valid and the scene is still valid and loaded, then we are still valid
1763+
var isStillValid = gameObject != null && gameObject.scene.IsValid() && gameObject.scene.isLoaded;
1764+
1765+
// If we're not the authority and everything is valid and dynamically spawned, then the destroy is not valid.
1766+
if (!isAuthorityDestroy && IsSceneObject == false && isStillValid)
1767+
{
1768+
if (networkManager.LogLevel <= LogLevel.Error)
17691769
{
1770-
if (networkManager.LogLevel <= LogLevel.Error)
1770+
if (networkManager.DistributedAuthorityMode)
17711771
{
1772-
if (networkManager.DistributedAuthorityMode)
1773-
{
1774-
NetworkLog.LogError($"[Invalid Destroy][{gameObject.name}][NetworkObjectId:{NetworkObjectId}] Destroy a spawned {nameof(NetworkObject)} on a non-owner client is not valid during a distributed authority session. Call {nameof(Destroy)} or {nameof(Despawn)} on the client-owner instead.");
1775-
}
1776-
else
1777-
{
1778-
NetworkLog.LogErrorServer($"[Invalid Destroy][{gameObject.name}][NetworkObjectId:{NetworkObjectId}] Destroy a spawned {nameof(NetworkObject)} on a non-host client is not valid. Call {nameof(Destroy)} or {nameof(Despawn)} on the server/host instead.");
1779-
}
1772+
NetworkLog.LogError($"[Invalid Destroy][{gameObject.name}][NetworkObjectId:{NetworkObjectId}] Destroy a spawned {nameof(NetworkObject)} on a non-owner client is not valid during a distributed authority session. Call {nameof(Destroy)} or {nameof(Despawn)} on the client-owner instead.");
1773+
}
1774+
else
1775+
{
1776+
NetworkLog.LogErrorServer($"[Invalid Destroy][{gameObject.name}][NetworkObjectId:{NetworkObjectId}] Destroy a spawned {nameof(NetworkObject)} on a non-host client is not valid. Call {nameof(Destroy)} or {nameof(Despawn)} on the server/host instead.");
17801777
}
1781-
return;
17821778
}
1779+
1780+
return;
17831781
}
1784-
// Otherwise, clients can despawn NetworkObjects while shutting down and should not generate any messages when this happens
17851782
}
17861783

1787-
// Always attempt to remove from scene changed updates
1788-
networkManager.SpawnManager?.RemoveNetworkObjectFromSceneChangedUpdates(this);
1789-
17901784
if (networkManager.SpawnManager != null && networkManager.SpawnManager.SpawnedObjects.TryGetValue(NetworkObjectId, out var networkObject))
17911785
{
17921786
if (this == networkObject)

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)