Skip to content

Commit 3280374

Browse files
Merge branch 'develop-2.0.0' into fix/attachablebehaviours-despawn-errors-scene-changes
2 parents eace6b8 + a31fee7 commit 3280374

File tree

6 files changed

+200
-130
lines changed

6 files changed

+200
-130
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ Additional documentation and release notes are available at [Multiplayer Documen
1313

1414
### Changed
1515

16-
- Improve performance of `NetworkBehaviour`. (#3915)
17-
- Improve performance of `NetworkTransform`. (#3907)
18-
- Improve performance of `NetworkRigidbodyBase`. (#3906)
19-
- Improve performance of `NetworkAnimator`. (#3905)
16+
- Replaced Debug usage by NetcodeLog on `NetworkSpawnManager` and `NetworkObject`. (#3933)
17+
- Improved performance of `NetworkBehaviour`. (#3915)
18+
- Improved performance of `NetworkTransform`. (#3907)
19+
- Improved performance of `NetworkRigidbodyBase`. (#3906)
20+
- Improved performance of `NetworkAnimator`. (#3905)
2021

2122
### Deprecated
2223

2324

2425
### Removed
2526

27+
- Removed un-needed exceptions on `NetworkSpawnManager`. (#3933)
2628

2729
### Fixed
2830

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

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ internal void OnValidate()
297297
if (globalId.identifierType != k_SceneObjectType)
298298
{
299299
// This should never happen, but in the event it does throw and error.
300-
Debug.LogError($"[{gameObject.name}] is detected as an in-scene placed object but its identifier is of type {globalId.identifierType}! **Report this error**");
300+
NetworkLog.LogError($"[{gameObject.name}] is detected as an in-scene placed object but its identifier is of type {globalId.identifierType}! **Report this error**");
301301
}
302302

303303
// 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.
@@ -1806,23 +1806,25 @@ internal void SpawnInternal(bool destroyWithScene, ulong ownerClientId, bool pla
18061806
{
18071807
NetworkLog.LogError($"[{name}] When distributed authority mode is enabled, you can only spawn NetworkObjects that belong to the local instance! Local instance id {NetworkManagerOwner.LocalClientId} is not the same as the assigned owner id: {ownerClientId}!");
18081808
}
1809-
return;
18101809
}
18111810
else
18121811
{
18131812
if (NetworkManagerOwner.LogLevel <= LogLevel.Error)
18141813
{
18151814
NetworkLog.LogError($"[{name}] Only server can spawn {nameof(NetworkObject)}s.");
18161815
}
1817-
return;
18181816
}
1817+
return;
18191818
}
18201819

18211820
if (NetworkManagerOwner.DistributedAuthorityMode)
18221821
{
18231822
if (NetworkManagerOwner.LocalClient == null || !NetworkManagerOwner.IsConnectedClient || !NetworkManagerOwner.ConnectionManager.LocalClient.IsApproved)
18241823
{
1825-
Debug.LogError($"Cannot spawn {name} until the client is fully connected to the session!");
1824+
if (NetworkManagerOwner.LogLevel <= LogLevel.Error)
1825+
{
1826+
NetworkLog.LogError($"Cannot spawn {name} until the client is fully connected to the session!");
1827+
}
18261828
return;
18271829
}
18281830
if (NetworkManagerOwner.NetworkConfig.EnableSceneManagement)
@@ -1878,7 +1880,10 @@ internal void SpawnInternal(bool destroyWithScene, ulong ownerClientId, bool pla
18781880
}
18791881
else
18801882
{
1881-
NetworkLog.LogWarningServer($"[{name}] Ran into unknown conditional check during spawn when determining distributed authority mode or not");
1883+
if (NetworkManagerOwner.LogLevel <= LogLevel.Normal)
1884+
{
1885+
NetworkLog.LogWarningServer($"[{name}] Ran into unknown conditional check during spawn when determining distributed authority mode or not");
1886+
}
18821887
}
18831888
}
18841889

@@ -1900,7 +1905,10 @@ public static NetworkObject InstantiateAndSpawn(GameObject networkPrefab, Networ
19001905
var networkObject = networkPrefab.GetComponent<NetworkObject>();
19011906
if (networkObject == null)
19021907
{
1903-
Debug.LogError($"The {nameof(NetworkPrefab)} {networkPrefab.name} does not have a {nameof(NetworkObject)} component!");
1908+
if (networkManager.LogLevel <= LogLevel.Error)
1909+
{
1910+
NetworkLog.LogError($"The {nameof(NetworkPrefab)} {networkPrefab.name} does not have a {nameof(NetworkObject)} component!");
1911+
}
19041912
return null;
19051913
}
19061914
return networkObject.InstantiateAndSpawn(networkManager, ownerClientId, destroyWithScene, isPlayerObject, forceOverride, position, rotation);
@@ -1922,34 +1930,49 @@ public NetworkObject InstantiateAndSpawn(NetworkManager networkManager, ulong ow
19221930
{
19231931
if (networkManager == null)
19241932
{
1925-
Debug.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NetworkManagerNull]);
1933+
if (NetworkManager.LogLevel <= LogLevel.Error)
1934+
{
1935+
NetworkLog.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NetworkManagerNull]);
1936+
}
19261937
return null;
19271938
}
19281939

19291940
if (!networkManager.IsListening)
19301941
{
1931-
Debug.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NoActiveSession]);
1942+
if (networkManager.LogLevel <= LogLevel.Error)
1943+
{
1944+
NetworkLog.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NoActiveSession]);
1945+
}
19321946
return null;
19331947
}
19341948

19351949
ownerClientId = networkManager.DistributedAuthorityMode ? networkManager.LocalClientId : ownerClientId;
19361950
// We only need to check for authority when running in client-server mode
19371951
if (!networkManager.IsServer && !networkManager.DistributedAuthorityMode)
19381952
{
1939-
Debug.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NotAuthority]);
1953+
if (networkManager.LogLevel <= LogLevel.Error)
1954+
{
1955+
NetworkLog.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NotAuthority]);
1956+
}
19401957
return null;
19411958
}
19421959

19431960
if (networkManager.ShutdownInProgress)
19441961
{
1945-
Debug.LogWarning(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.InvokedWhenShuttingDown]);
1962+
if (networkManager.LogLevel <= LogLevel.Normal)
1963+
{
1964+
NetworkLog.LogWarning(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.InvokedWhenShuttingDown]);
1965+
}
19461966
return null;
19471967
}
19481968

19491969
// Verify it is actually a valid prefab
19501970
if (!networkManager.NetworkConfig.Prefabs.Contains(gameObject))
19511971
{
1952-
Debug.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NotRegisteredNetworkPrefab]);
1972+
if (networkManager.LogLevel <= LogLevel.Error)
1973+
{
1974+
NetworkLog.LogError(NetworkSpawnManager.InstantiateAndSpawnErrors[NetworkSpawnManager.InstantiateAndSpawnErrorTypes.NotRegisteredNetworkPrefab]);
1975+
}
19531976
return null;
19541977
}
19551978

@@ -2005,7 +2028,6 @@ public void Despawn(bool destroy = true)
20052028
{
20062029
NetworkLog.LogErrorServer($"[{name}][Attempted despawn before {nameof(NetworkObject)} was spawned]");
20072030
}
2008-
20092031
return;
20102032
}
20112033

@@ -2055,10 +2077,8 @@ public void ChangeOwnership(ulong newOwnerClientId)
20552077
{
20562078
NetworkLog.LogErrorServer($"[{name}][Attempted ownership change before {nameof(NetworkObject)} was spawned]");
20572079
}
2058-
20592080
return;
20602081
}
2061-
20622082
NetworkManagerOwner.SpawnManager.ChangeOwnership(this, newOwnerClientId, HasAuthority);
20632083
}
20642084

@@ -2074,7 +2094,6 @@ internal void InvokeBehaviourOnOwnershipChanged(ulong originalOwnerClientId, ulo
20742094
{
20752095
NetworkLog.LogErrorServer($"[{name}][Attempted behavior invoke on ownership changed before {nameof(NetworkObject)} was spawned]");
20762096
}
2077-
20782097
return;
20792098
}
20802099

@@ -2105,10 +2124,12 @@ internal void InvokeBehaviourOnOwnershipChanged(ulong originalOwnerClientId, ulo
21052124
{
21062125
if (!childBehaviour.gameObject.activeInHierarchy)
21072126
{
2108-
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!");
2127+
if (NetworkManagerOwner.LogLevel <= LogLevel.Normal)
2128+
{
2129+
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!");
2130+
}
21092131
continue;
21102132
}
2111-
21122133
childBehaviour.InternalOnGainedOwnership();
21132134
}
21142135
}
@@ -2124,7 +2145,10 @@ internal void InvokeOwnershipChanged(ulong previous, ulong next)
21242145
}
21252146
else
21262147
{
2127-
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!");
2148+
if (NetworkManagerOwner.LogLevel <= LogLevel.Normal)
2149+
{
2150+
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!");
2151+
}
21282152
}
21292153
}
21302154
}
@@ -2315,7 +2339,7 @@ internal bool InternalTrySetParent(NetworkObject parent, bool worldPositionStays
23152339
private void OnTransformParentChanged()
23162340
{
23172341
var networkManager = NetworkManager;
2318-
if (!AutoObjectParentSync || networkManager.ShutdownInProgress)
2342+
if (!AutoObjectParentSync || (networkManager != null && networkManager.ShutdownInProgress))
23192343
{
23202344
return;
23212345
}
@@ -2327,18 +2351,9 @@ private void OnTransformParentChanged()
23272351

23282352
if (networkManager == null || !networkManager.IsListening)
23292353
{
2330-
// DANGO-TODO: Review as to whether we want to provide a better way to handle changing parenting of objects when the
2331-
// object is not spawned. Really, we shouldn't care about these types of changes.
2332-
if (networkManager.DistributedAuthorityMode && m_CachedParent != null && transform.parent == null)
2333-
{
2334-
m_CachedParent = null;
2335-
return;
2336-
}
23372354
transform.parent = m_CachedParent;
2338-
if (NetworkManagerOwner.LogLevel <= LogLevel.Error)
2339-
{
2340-
NetworkLog.LogError($"[{name}] {nameof(networkManager)} is not listening, start a server or host before re-parenting.");
2341-
}
2355+
// We want to log at any LogLevel, since we may not have a network manager may here.
2356+
NetworkLog.LogError($"[{name}] {nameof(networkManager)} is not listening, start a server or host before re-parenting.");
23422357
return;
23432358
}
23442359

@@ -2355,7 +2370,7 @@ private void OnTransformParentChanged()
23552370
else
23562371
{
23572372
transform.parent = m_CachedParent;
2358-
if (NetworkManagerOwner.LogLevel <= LogLevel.Error)
2373+
if (networkManager.LogLevel <= LogLevel.Error)
23592374
{
23602375
NetworkLog.LogErrorServer($"[{name}] {nameof(NetworkObject)} can only be re-parented after being spawned!");
23612376
}
@@ -2392,17 +2407,17 @@ private void OnTransformParentChanged()
23922407
{
23932408
transform.parent = m_CachedParent;
23942409
AuthorityAppliedParenting = false;
2395-
if (NetworkManagerOwner.LogLevel <= LogLevel.Error)
2410+
if (networkManager.LogLevel <= LogLevel.Error)
23962411
{
23972412
NetworkLog.LogErrorServer($"[{name}] Invalid parenting, {nameof(NetworkObject)} moved under a non-{nameof(NetworkObject)} parent");
23982413
}
23992414
return;
24002415
}
2401-
else if (!parentObject.IsSpawned)
2416+
if (!parentObject.IsSpawned)
24022417
{
24032418
transform.parent = m_CachedParent;
24042419
AuthorityAppliedParenting = false;
2405-
if (NetworkManagerOwner.LogLevel <= LogLevel.Error)
2420+
if (networkManager.LogLevel <= LogLevel.Error)
24062421
{
24072422
NetworkLog.LogErrorServer($"[{name}] {nameof(NetworkObject)} can only be re-parented under another spawned {nameof(NetworkObject)}.");
24082423
}
@@ -2538,10 +2553,10 @@ internal bool ApplyNetworkParenting(bool removeParent = false, bool ignoreNotSpa
25382553
}
25392554
}
25402555

2541-
// If we are removing the parent or our latest parent is not set, then remove the parent
2556+
// If we are removing the parent or our latest parent is not set, then remove the parent.
25422557
// removeParent is only set when:
25432558
// - The server-side NetworkObject.OnTransformParentChanged is invoked and the parent is being removed
2544-
// - The client-side when handling a ParentSyncMessage
2559+
// - The client-side is handling a ParentSyncMessage
25452560
// When clients are synchronizing only the m_LatestParent.HasValue will not have a value if there is no parent
25462561
// or a parent was removed prior to the client connecting (i.e. in-scene placed NetworkObjects)
25472562
if (removeParent || !m_LatestParent.HasValue)
@@ -2641,7 +2656,10 @@ internal void InvokeBehaviourNetworkSpawn()
26412656
{
26422657
if (!childBehaviour.gameObject.activeInHierarchy)
26432658
{
2644-
Debug.LogWarning($"{GenerateDisabledNetworkBehaviourWarning(childBehaviour)}");
2659+
if (NetworkManager.LogLevel <= LogLevel.Normal)
2660+
{
2661+
NetworkLog.LogWarning($"{GenerateDisabledNetworkBehaviourWarning(childBehaviour)}");
2662+
}
26452663
continue;
26462664
}
26472665
childBehaviour.NetworkSpawn();
@@ -2753,7 +2771,6 @@ internal List<NetworkBehaviour> ChildNetworkBehaviours
27532771
}
27542772
#endif
27552773
}
2756-
27572774
return m_ChildNetworkBehaviours;
27582775
}
27592776
}
@@ -2920,9 +2937,7 @@ internal struct SerializedObject
29202937
public bool HasParent;
29212938
public bool IsSceneObject;
29222939
public bool HasTransform;
2923-
29242940
public bool IsLatestParentSet;
2925-
29262941
public bool WorldPositionStays;
29272942

29282943
/// <summary>
@@ -2932,15 +2947,10 @@ internal struct SerializedObject
29322947
/// to the current active scene when its scene is unloaded. (only for dynamically spawned)
29332948
/// </summary>
29342949
public bool DestroyWithScene;
2935-
29362950
public bool DontDestroyWithOwner;
2937-
29382951
public bool HasOwnershipFlags;
2939-
29402952
public bool SyncObservers;
2941-
29422953
public bool SpawnWithObservers;
2943-
29442954
public bool HasInstantiationData;
29452955

29462956
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -3032,9 +3042,6 @@ public struct TransformData : INetworkSerializeByMemcpy
30323042

30333043
public TransformData Transform;
30343044

3035-
//If(Metadata.IsReparented)
3036-
3037-
//If(IsLatestParentSet)
30383045
public ulong? LatestParent;
30393046

30403047
public NetworkObject OwnerObject;
@@ -3354,11 +3361,8 @@ internal static NetworkObject Deserialize(in SerializedObject serializedObject,
33543361
reader.ReadValueSafe(out instantiationData);
33553362
}
33563363

3357-
33583364
// Attempt to create a local NetworkObject
33593365
var networkObject = networkManager.SpawnManager.CreateLocalNetworkObject(serializedObject, instantiationData);
3360-
3361-
33623366
if (networkObject == null)
33633367
{
33643368
// Log the error that the NetworkObject failed to construct
@@ -3380,7 +3384,6 @@ internal static NetworkObject Deserialize(in SerializedObject serializedObject,
33803384
// We have nothing left to do here.
33813385
return null;
33823386
}
3383-
33843387
networkObject.NetworkManagerOwner = networkManager;
33853388

33863389
// This will get set again when the NetworkObject is spawned locally, but we set it here ahead of spawning
@@ -3669,17 +3672,13 @@ internal uint CheckForGlobalObjectIdHashOverride()
36693672
{
36703673
return PrefabGlobalObjectIdHash;
36713674
}
3672-
else
3675+
// For legacy manual instantiation and spawning, check the OverrideToNetworkPrefab for a possible match
3676+
if (networkManager.NetworkConfig.Prefabs.OverrideToNetworkPrefab.TryGetValue(GlobalObjectIdHash, out var overrideHash))
36733677
{
3674-
// For legacy manual instantiation and spawning, check the OverrideToNetworkPrefab for a possible match
3675-
if (networkManager.NetworkConfig.Prefabs.OverrideToNetworkPrefab.ContainsKey(GlobalObjectIdHash))
3676-
{
3677-
return networkManager.NetworkConfig.Prefabs.OverrideToNetworkPrefab[GlobalObjectIdHash];
3678-
}
3678+
return overrideHash;
36793679
}
36803680
}
36813681
}
3682-
36833682
return GlobalObjectIdHash;
36843683
}
36853684

0 commit comments

Comments
 (0)