Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Changed

- Remove exceptions and replaced Debug usage by NetcodeLog on `NetworkSpawnManager`. (#3933)
- Improve performance of `NetworkBehaviour`. (#3915)
- Improve performance of `NetworkTransform`. (#3907)
- Improve performance of `NetworkRigidbodyBase`. (#3906)
Expand All @@ -26,6 +27,7 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Fixed

- Early return when `ChangeOwnership` is called while not spawned. (#3933)
- Fixed issue where attempts to use `NetworkLog` when there is no `NetworkManager` instance would result in an exception. (#3917)

### Security
Expand Down
84 changes: 60 additions & 24 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ internal void OnValidate()
if (globalId.identifierType != k_SceneObjectType)
{
// This should never happen, but in the event it does throw and error.
Debug.LogError($"[{gameObject.name}] is detected as an in-scene placed object but its identifier is of type {globalId.identifierType}! **Report this error**");
NetworkLog.LogError($"[{gameObject.name}] is detected as an in-scene placed object but its identifier is of type {globalId.identifierType}! **Report this error**");
}

// If this is a prefab instance, then we want to mark it as having been updated in order for the udpated GlobalObjectIdHash value to be saved.
Expand Down Expand Up @@ -1778,7 +1778,10 @@ internal void SpawnInternal(bool destroyWithScene, ulong ownerClientId, bool pla
{
if (NetworkManagerOwner.LocalClient == null || !NetworkManagerOwner.IsConnectedClient || !NetworkManagerOwner.ConnectionManager.LocalClient.IsApproved)
{
Debug.LogError($"Cannot spawn {name} until the client is fully connected to the session!");
if (NetworkManagerOwner.LogLevel <= LogLevel.Error)
{
NetworkLog.LogError($"Cannot spawn {name} until the client is fully connected to the session!");
}
return;
}
if (NetworkManagerOwner.NetworkConfig.EnableSceneManagement)
Expand Down Expand Up @@ -1856,7 +1859,11 @@ public static NetworkObject InstantiateAndSpawn(GameObject networkPrefab, Networ
var networkObject = networkPrefab.GetComponent<NetworkObject>();
if (networkObject == null)
{
Debug.LogError($"The {nameof(NetworkPrefab)} {networkPrefab.name} does not have a {nameof(NetworkObject)} component!");
if (networkManager.LogLevel <= LogLevel.Error)
{
NetworkLog.LogError($"The {nameof(NetworkPrefab)} {networkPrefab.name} does not have a {nameof(NetworkObject)} component!");
}

return null;
}
return networkObject.InstantiateAndSpawn(networkManager, ownerClientId, destroyWithScene, isPlayerObject, forceOverride, position, rotation);
Expand All @@ -1878,34 +1885,49 @@ public NetworkObject InstantiateAndSpawn(NetworkManager networkManager, ulong ow
{
if (networkManager == null)
{
Debug.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NetworkManagerNull]);
if (NetworkManager.LogLevel <= LogLevel.Error)
{
NetworkLog.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NetworkManagerNull]);
}
return null;
}

if (!networkManager.IsListening)
{
Debug.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NoActiveSession]);
if (networkManager.LogLevel <= LogLevel.Error)
{
NetworkLog.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NoActiveSession]);
}
return null;
}

ownerClientId = networkManager.DistributedAuthorityMode ? networkManager.LocalClientId : ownerClientId;
// We only need to check for authority when running in client-server mode
if (!networkManager.IsServer && !networkManager.DistributedAuthorityMode)
{
Debug.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NotAuthority]);
if (networkManager.LogLevel <= LogLevel.Error)
{
NetworkLog.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NotAuthority]);
}
return null;
}

if (networkManager.ShutdownInProgress)
{
Debug.LogWarning(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.InvokedWhenShuttingDown]);
if (networkManager.LogLevel <= LogLevel.Normal)
{
NetworkLog.LogWarning(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.InvokedWhenShuttingDown]);
}
return null;
}

// Verify it is actually a valid prefab
if (!networkManager.NetworkConfig.Prefabs.Contains(gameObject))
{
Debug.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NotRegisteredNetworkPrefab]);
if (networkManager.LogLevel <= LogLevel.Error)
{
NetworkLog.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NotRegisteredNetworkPrefab]);
}
return null;
}

Expand Down Expand Up @@ -2061,7 +2083,11 @@ internal void InvokeBehaviourOnOwnershipChanged(ulong originalOwnerClientId, ulo
{
if (!childBehaviour.gameObject.activeInHierarchy)
{
Debug.LogWarning($"[{name}] {childBehaviour.gameObject.name} is disabled! Netcode for GameObjects does not support disabled NetworkBehaviours! The {childBehaviour.GetType().Name} component was skipped during ownership assignment!");
if (NetworkManagerOwner.LogLevel <= LogLevel.Normal)
{
NetworkLog.LogWarning($"[{name}] {childBehaviour.gameObject.name} is disabled! Netcode for GameObjects does not support disabled NetworkBehaviours! The {childBehaviour.GetType().Name} component was skipped during ownership assignment!");
}

continue;
}

Expand All @@ -2080,7 +2106,10 @@ internal void InvokeOwnershipChanged(ulong previous, ulong next)
}
else
{
Debug.LogWarning($"[{name}] {ChildNetworkBehaviours[i].gameObject.name} is disabled! Netcode for GameObjects does not support disabled NetworkBehaviours! The {ChildNetworkBehaviours[i].GetType().Name} component was skipped during ownership assignment!");
if (NetworkManagerOwner.LogLevel <= LogLevel.Normal)
{
NetworkLog.LogWarning($"[{name}] {ChildNetworkBehaviours[i].gameObject.name} is disabled! Netcode for GameObjects does not support disabled NetworkBehaviours! The {ChildNetworkBehaviours[i].GetType().Name} component was skipped during ownership assignment!");
}
}
}
}
Expand Down Expand Up @@ -2291,7 +2320,7 @@ private void OnTransformParentChanged()
return;
}
transform.parent = m_CachedParent;
if (NetworkManagerOwner.LogLevel <= LogLevel.Error)
if (networkManager.LogLevel <= LogLevel.Error)
{
NetworkLog.LogError($"[{name}] {nameof(networkManager)} is not listening, start a server or host before re-parenting.");
}
Expand All @@ -2311,7 +2340,7 @@ private void OnTransformParentChanged()
else
{
transform.parent = m_CachedParent;
if (NetworkManagerOwner.LogLevel <= LogLevel.Error)
if (networkManager.LogLevel <= LogLevel.Error)
{
NetworkLog.LogErrorServer($"[{name}] {nameof(NetworkObject)} can only be re-parented after being spawned!");
}
Expand All @@ -2326,9 +2355,9 @@ private void OnTransformParentChanged()
if (!isParentingAuthority)
{
transform.parent = m_CachedParent;
if (networkManager.LogLevel <= LogLevel.Error)
if (NetworkManagerOwner.LogLevel <= LogLevel.Error)
{
if (networkManager.DistributedAuthorityMode)
if (NetworkManagerOwner.DistributedAuthorityMode)
{
NetworkLog.LogError($"[{name}][Not Owner] Only the owner-authority of child {gameObject.name}'s {nameof(NetworkObject)} component can re-parent it!");
}
Expand All @@ -2354,7 +2383,7 @@ private void OnTransformParentChanged()
}
return;
}
else if (!parentObject.IsSpawned)
if (!parentObject.IsSpawned)
{
transform.parent = m_CachedParent;
AuthorityAppliedParenting = false;
Expand Down Expand Up @@ -2399,28 +2428,28 @@ private void OnTransformParentChanged()
}

// If we're not the server, we should tell the server about this parent change
if (!networkManager.IsServer)
if (!NetworkManagerOwner.IsServer)
{
// Don't send a message in DA mode if we're the only observers of this object (we're the only authority).
if (networkManager.DistributedAuthorityMode && Observers.Count <= 1)
if (NetworkManagerOwner.DistributedAuthorityMode && Observers.Count <= 1)
{
return;
}

networkManager.ConnectionManager.SendMessage(ref message, MessageDeliveryType<ParentSyncMessage>.DefaultDelivery, NetworkManager.ServerClientId);
NetworkManagerOwner.ConnectionManager.SendMessage(ref message, MessageDeliveryType<ParentSyncMessage>.DefaultDelivery, NetworkManager.ServerClientId);
return;
}

// Otherwise we are a Server (client-server or DAHost). Send to all observers
foreach (var clientId in networkManager.ConnectionManager.ConnectedClientIds)
foreach (var clientId in NetworkManagerOwner.ConnectionManager.ConnectedClientIds)
{
if (clientId == NetworkManager.ServerClientId)
{
continue;
}
if (Observers.Contains(clientId))
{
networkManager.ConnectionManager.SendMessage(ref message, MessageDeliveryType<ParentSyncMessage>.DefaultDelivery, clientId);
NetworkManagerOwner.ConnectionManager.SendMessage(ref message, MessageDeliveryType<ParentSyncMessage>.DefaultDelivery, clientId);
}
}
}
Expand Down Expand Up @@ -2494,10 +2523,10 @@ internal bool ApplyNetworkParenting(bool removeParent = false, bool ignoreNotSpa
}
}

// If we are removing the parent or our latest parent is not set, then remove the parent
// If we are removing the parent or our latest parent is not set, then remove the parent.
// removeParent is only set when:
// - The server-side NetworkObject.OnTransformParentChanged is invoked and the parent is being removed
// - The client-side when handling a ParentSyncMessage
// - The client-side is handling a ParentSyncMessage
// When clients are synchronizing only the m_LatestParent.HasValue will not have a value if there is no parent
// or a parent was removed prior to the client connecting (i.e. in-scene placed NetworkObjects)
if (removeParent || !m_LatestParent.HasValue)
Expand Down Expand Up @@ -2597,7 +2626,10 @@ internal void InvokeBehaviourNetworkSpawn()
{
if (!childBehaviour.gameObject.activeInHierarchy)
{
Debug.LogWarning($"{GenerateDisabledNetworkBehaviourWarning(childBehaviour)}");
if (NetworkManager.LogLevel <= LogLevel.Normal)
{
NetworkLog.LogWarning($"{GenerateDisabledNetworkBehaviourWarning(childBehaviour)}");
}
continue;
}
childBehaviour.NetworkSpawn();
Expand Down Expand Up @@ -3356,7 +3388,11 @@ internal static NetworkObject Deserialize(in SerializedObject serializedObject,
// Ensure that the buffer is completely reset
if (reader.Position != endOfSynchronizationData)
{
Debug.LogWarning($"[Size mismatch] Expected: {endOfSynchronizationData} Currently At: {reader.Position}!");
if (networkManager.LogLevel <= LogLevel.Normal)
{
NetworkLog.LogWarning($"[Size mismatch] Expected: {endOfSynchronizationData} Currently At: {reader.Position}!");
}

reader.Seek(endOfSynchronizationData);
}
}
Expand Down
Loading
Loading