Skip to content

Commit 942e313

Browse files
fix and style
Fixing several issues with NetworkObjectSceneMigrationTests. Fixing several formatting issues.
1 parent 978aee1 commit 942e313

16 files changed

Lines changed: 195 additions & 158 deletions

File tree

com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,11 @@ public bool NotifyUserOfNestedNetworkManager(NetworkManager networkManager, bool
195195
{
196196
return isParented;
197197
}
198-
else // If we are no longer a child, then we can remove ourself from this list
199-
if (transform.root == gameObject.transform)
200-
{
201-
s_LastKnownNetworkManagerParents.Remove(networkManager);
202-
}
198+
else if (transform.root == gameObject.transform)
199+
{
200+
// If we are no longer a child, then we can remove ourself from this list
201+
s_LastKnownNetworkManagerParents.Remove(networkManager);
202+
}
203203
}
204204
if (!EditorApplication.isUpdating && isParented)
205205
{

com.unity.netcode.gameobjects/Runtime/Components/Interpolator/BufferedLinearInterpolator.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -467,20 +467,20 @@ internal T Update(float deltaTime, double tickLatencyAsTime, double minDeltaTime
467467
InterpolateState.CurrentValue = InterpolateState.NextValue;
468468
}
469469
}
470-
else // If the target is reached and we have no more state updates, we want to check to see if we need to reset.
471-
if (m_BufferQueue.Count == 0)
470+
// If the target is reached and we have no more state updates, we want to check to see if we need to reset.
471+
else if (m_BufferQueue.Count == 0)
472+
{
473+
// When the delta between the time sent and the current tick latency time-window is greater than the max delta time
474+
// plus the minimum delta time (a rough estimate of time to wait before we consider rate of change equal to zero),
475+
// we will want to reset the interpolator with the current known value. This prevents the next received state update's
476+
// time to be calculated against the last calculated time which if there is an extended period of time between the two
477+
// it would cause a large delta time period between the two states (i.e. it stops moving for a second or two and then
478+
// starts moving again).
479+
if ((tickLatencyAsTime - InterpolateState.Target.Value.TimeSent) > InterpolateState.MaxDeltaTime + minDeltaTime)
472480
{
473-
// When the delta between the time sent and the current tick latency time-window is greater than the max delta time
474-
// plus the minimum delta time (a rough estimate of time to wait before we consider rate of change equal to zero),
475-
// we will want to reset the interpolator with the current known value. This prevents the next received state update's
476-
// time to be calculated against the last calculated time which if there is an extended period of time between the two
477-
// it would cause a large delta time period between the two states (i.e. it stops moving for a second or two and then
478-
// starts moving again).
479-
if ((tickLatencyAsTime - InterpolateState.Target.Value.TimeSent) > InterpolateState.MaxDeltaTime + minDeltaTime)
480-
{
481-
InterpolateState.Reset(InterpolateState.CurrentValue);
482-
}
481+
InterpolateState.Reset(InterpolateState.CurrentValue);
483482
}
483+
}
484484
}
485485
m_NbItemsReceivedThisFrame = 0;
486486
return InterpolateState.CurrentValue;
@@ -595,18 +595,18 @@ public T Update(float deltaTime, double renderTime, double serverTime)
595595
}
596596
else // If the target is reached and we have no more state updates, we want to check to see if we need to reset.
597597
if (InterpolateState.TargetReached && m_BufferQueue.Count == 0)
598+
{
599+
// When the delta between the time sent and the current tick latency time-window is greater than the max delta time
600+
// plus the minimum delta time (a rough estimate of time to wait before we consider rate of change equal to zero),
601+
// we will want to reset the interpolator with the current known value. This prevents the next received state update's
602+
// time to be calculated against the last calculated time which if there is an extended period of time between the two
603+
// it would cause a large delta time period between the two states (i.e. it stops moving for a second or two and then
604+
// starts moving again).
605+
if ((renderTime - InterpolateState.Target.Value.TimeSent) > 0.3f) // If we haven't recevied anything within 300ms, assume we stopped motion.
598606
{
599-
// When the delta between the time sent and the current tick latency time-window is greater than the max delta time
600-
// plus the minimum delta time (a rough estimate of time to wait before we consider rate of change equal to zero),
601-
// we will want to reset the interpolator with the current known value. This prevents the next received state update's
602-
// time to be calculated against the last calculated time which if there is an extended period of time between the two
603-
// it would cause a large delta time period between the two states (i.e. it stops moving for a second or two and then
604-
// starts moving again).
605-
if ((renderTime - InterpolateState.Target.Value.TimeSent) > 0.3f) // If we haven't recevied anything within 300ms, assume we stopped motion.
606-
{
607-
InterpolateState.Reset(InterpolateState.CurrentValue);
608-
}
607+
InterpolateState.Reset(InterpolateState.CurrentValue);
609608
}
609+
}
610610
m_NbItemsReceivedThisFrame = 0;
611611
return InterpolateState.CurrentValue;
612612
}

com.unity.netcode.gameobjects/Runtime/Components/NetworkAnimator.cs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,26 +1180,25 @@ internal void CheckForAnimatorChanges()
11801180
{
11811181
SendAnimStateRpc(m_AnimationMessage);
11821182
}
1183+
else if(!IsServer && IsOwner)
1184+
{
1185+
SendServerAnimStateRpc(m_AnimationMessage);
1186+
}
11831187
else
1184-
if (!IsServer && IsOwner)
1185-
{
1186-
SendServerAnimStateRpc(m_AnimationMessage);
1187-
}
1188-
else
1188+
{
1189+
// Just notify all remote clients and not the local server
1190+
m_TargetGroup.Clear();
1191+
foreach (var clientId in LocalNetworkManager.ConnectionManager.ConnectedClientIds)
11891192
{
1190-
// Just notify all remote clients and not the local server
1191-
m_TargetGroup.Clear();
1192-
foreach (var clientId in LocalNetworkManager.ConnectionManager.ConnectedClientIds)
1193+
if (clientId == LocalNetworkManager.LocalClientId || !NetworkObject.Observers.Contains(clientId))
11931194
{
1194-
if (clientId == LocalNetworkManager.LocalClientId || !NetworkObject.Observers.Contains(clientId))
1195-
{
1196-
continue;
1197-
}
1198-
m_TargetGroup.Add(clientId);
1195+
continue;
11991196
}
1200-
m_RpcParams.Send.Target = m_TargetGroup.Target;
1201-
SendClientAnimStateRpc(m_AnimationMessage, m_RpcParams);
1197+
m_TargetGroup.Add(clientId);
12021198
}
1199+
m_RpcParams.Send.Target = m_TargetGroup.Target;
1200+
SendClientAnimStateRpc(m_AnimationMessage, m_RpcParams);
1201+
}
12031202
}
12041203
}
12051204

@@ -1348,23 +1347,23 @@ private unsafe void WriteParameters(ref FastBufferWriter writer)
13481347
}
13491348
else
13501349
if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterBool)
1350+
{
1351+
var valueBool = m_Animator.GetBool(hash);
1352+
fixed (void* value = cacheValue.Value)
13511353
{
1352-
var valueBool = m_Animator.GetBool(hash);
1353-
fixed (void* value = cacheValue.Value)
1354-
{
1355-
UnsafeUtility.WriteArrayElement(value, 0, valueBool);
1356-
BytePacker.WriteValuePacked(writer, valueBool);
1357-
}
1354+
UnsafeUtility.WriteArrayElement(value, 0, valueBool);
1355+
BytePacker.WriteValuePacked(writer, valueBool);
13581356
}
1359-
else if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterFloat)
1357+
}
1358+
else if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterFloat)
1359+
{
1360+
var valueFloat = m_Animator.GetFloat(hash);
1361+
fixed (void* value = cacheValue.Value)
13601362
{
1361-
var valueFloat = m_Animator.GetFloat(hash);
1362-
fixed (void* value = cacheValue.Value)
1363-
{
1364-
UnsafeUtility.WriteArrayElement(value, 0, valueFloat);
1365-
BytePacker.WriteValuePacked(writer, valueFloat);
1366-
}
1363+
UnsafeUtility.WriteArrayElement(value, 0, valueFloat);
1364+
BytePacker.WriteValuePacked(writer, valueFloat);
13671365
}
1366+
}
13681367
}
13691368
}
13701369

com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2515,24 +2515,24 @@ private bool CheckForStateChange(ref NetworkTransformState networkState, bool is
25152515
}
25162516
}
25172517
}
2518-
else // Just apply the full local scale when synchronizing
2519-
if (SynchronizeScale)
2518+
// Just apply the full local scale when synchronizing
2519+
else if (SynchronizeScale)
2520+
{
2521+
var localScale = CachedTransform.localScale;
2522+
if (!UseHalfFloatPrecision)
25202523
{
2521-
var localScale = CachedTransform.localScale;
2522-
if (!UseHalfFloatPrecision)
2523-
{
25242524

2525-
networkState.ScaleX = localScale.x;
2526-
networkState.ScaleY = localScale.y;
2527-
networkState.ScaleZ = localScale.z;
2528-
}
2529-
else
2530-
{
2531-
networkState.Scale = localScale;
2532-
}
2533-
flagStates.MarkChanged(AxialType.Scale, true);
2534-
isScaleDirty = true;
2525+
networkState.ScaleX = localScale.x;
2526+
networkState.ScaleY = localScale.y;
2527+
networkState.ScaleZ = localScale.z;
25352528
}
2529+
else
2530+
{
2531+
networkState.Scale = localScale;
2532+
}
2533+
flagStates.MarkChanged(AxialType.Scale, true);
2534+
isScaleDirty = true;
2535+
}
25362536
isDirty |= isPositionDirty || isRotationDirty || isScaleDirty;
25372537

25382538
if (isDirty)
@@ -3488,12 +3488,12 @@ private void NonAuthorityFinalizeSynchronization()
34883488
child.InternalInitialization();
34893489
}
34903490
}
3491-
else // Otherwise, just run through standard synchronization of this instance
3492-
if (!CanCommitToTransform)
3493-
{
3494-
ApplySynchronization();
3495-
InternalInitialization();
3496-
}
3491+
// Otherwise, just run through standard synchronization of this instance
3492+
else if (!CanCommitToTransform)
3493+
{
3494+
ApplySynchronization();
3495+
InternalInitialization();
3496+
}
34973497
}
34983498
}
34993499

com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -967,19 +967,18 @@ internal void ProcessClientsToDisconnect()
967967
{
968968
return (true, playerPrefabHash.Value);
969969
}
970-
else
971-
if (NetworkManager.NetworkConfig.PlayerPrefab != null)
970+
else if (NetworkManager.NetworkConfig.PlayerPrefab != null)
971+
{
972+
var networkObject = NetworkManager.NetworkConfig.PlayerPrefab.GetComponent<NetworkObject>();
973+
if (networkObject != null)
972974
{
973-
var networkObject = NetworkManager.NetworkConfig.PlayerPrefab.GetComponent<NetworkObject>();
974-
if (networkObject != null)
975-
{
976-
return (true, networkObject.GlobalObjectIdHash);
977-
}
978-
else
979-
{
980-
NetworkManager.Log.Error(new Logging.Context(LogLevel.Error, $"Player prefab {NetworkManager.NetworkConfig.PlayerPrefab.name} has no {nameof(NetworkObject)}!"));
981-
}
975+
return (true, networkObject.GlobalObjectIdHash);
982976
}
977+
else
978+
{
979+
NetworkManager.Log.Error(new Logging.Context(LogLevel.Error, $"Player prefab {NetworkManager.NetworkConfig.PlayerPrefab.name} has no {nameof(NetworkObject)}!"));
980+
}
981+
}
983982
return (false, 0);
984983
}
985984

com.unity.netcode.gameobjects/Runtime/SceneManagement/DefaultSceneManagerHandler.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,14 @@ public void SetClientSynchronizationMode(ref NetworkManager networkManager, Load
359359
}
360360
return;
361361
}
362-
else // Warn users if they are changing this after there are clients already connected and synchronized
363-
if (!networkManager.DistributedAuthorityMode && networkManager.ConnectedClientsIds.Count > (networkManager.IsHost ? 1 : 0) && sceneManager.ClientSynchronizationMode != mode)
362+
// Warn users if they are changing this after there are clients already connected and synchronized
363+
else if (!networkManager.DistributedAuthorityMode && networkManager.ConnectedClientsIds.Count > (networkManager.IsHost ? 1 : 0) && sceneManager.ClientSynchronizationMode != mode)
364+
{
365+
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
364366
{
365-
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
366-
{
367-
NetworkLog.LogWarning("Server is changing client synchronization mode after clients have been synchronized! It is recommended to do this before clients are connected!");
368-
}
367+
NetworkLog.LogWarning("Server is changing client synchronization mode after clients have been synchronized! It is recommended to do this before clients are connected!");
369368
}
369+
}
370370

371371
// For additive client synchronization, we take into consideration scenes
372372
// already loaded.

com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ internal void SetTheSceneBeingSynchronized(NetworkSceneHandle serverSceneHandle)
10341034
// Most common scenario for DontDestroyOnLoad is when NetworkManager is set to not be destroyed
10351035
if (serverSceneHandle == DontDestroyOnLoadScene.handle)
10361036
{
1037-
SceneBeingSynchronized = NetworkManager.gameObject.scene;
1037+
SceneBeingSynchronized = DontDestroyOnLoadScene;
10381038
return;
10391039
}
10401040
else
@@ -2260,20 +2260,13 @@ private void SynchronizeNetworkObjectScene()
22602260
{
22612261
networkObject.SceneOriginHandle = ServerSceneHandleToClientSceneHandle[networkObject.NetworkSceneHandle];
22622262

2263-
2264-
22652263
// If the NetworkObject does not have a parent and is not in the same scene as it is on the server side, then find the right scene
22662264
// and move it to that scene.
22672265
if (networkObject.gameObject.scene.handle != networkObject.SceneOriginHandle && networkObject.transform.parent == null)
22682266
{
22692267
if (ScenesLoaded.ContainsKey(networkObject.SceneOriginHandle))
22702268
{
22712269
var scene = ScenesLoaded[networkObject.SceneOriginHandle];
2272-
if (scene == DontDestroyOnLoadScene)
2273-
{
2274-
Debug.Log($"{networkObject.gameObject.name} migrating into DDOL!");
2275-
}
2276-
22772270
SceneManager.MoveGameObjectToScene(networkObject.gameObject, scene);
22782271
}
22792272
else if (NetworkManager.LogLevel <= LogLevel.Normal)
@@ -2897,6 +2890,13 @@ internal bool IsSceneUnloading(NetworkObject networkObject)
28972890
/// </summary>
28982891
internal void NotifyNetworkObjectSceneChanged(NetworkObject networkObject)
28992892
{
2893+
Debug.Log($"{networkObject.name} changed scene to {networkObject.gameObject.scene.name}!");
2894+
if (networkObject.NetworkManagerOwner != NetworkManager)
2895+
{
2896+
Debug.Log($"!!!!!!!!!!!!! Integration test is registering for scene migration for instances outside of the bounds of this NetworkManager context !!!!!!!!!!!!!");
2897+
return;
2898+
}
2899+
29002900
// Really, this should never happen but in case it does
29012901
if (!networkObject.HasAuthority)
29022902
{
@@ -2920,7 +2920,7 @@ internal void NotifyNetworkObjectSceneChanged(NetworkObject networkObject)
29202920

29212921
// Ignore if the scene is the currently active scene and the NetworkObject is auto synchronizing/migrating
29222922
// to the currently active scene.
2923-
if (networkObject.gameObject.scene == SceneManager.GetActiveScene() && networkObject.ActiveSceneSynchronization)
2923+
if (networkObject.gameObject.scene.name == SceneManager.GetActiveScene().name && networkObject.ActiveSceneSynchronization)
29242924
{
29252925
return;
29262926
}
@@ -2929,6 +2929,13 @@ internal void NotifyNetworkObjectSceneChanged(NetworkObject networkObject)
29292929
// Note: This does not apply to SceneEventType.Synchronize since synchronization isn't a global connected client event.
29302930
if (IsSceneEventInProgress())
29312931
{
2932+
Debug.Log($"{networkObject.name} scene event in progress -- ignoring!");
2933+
return;
2934+
}
2935+
2936+
if (IsSceneUnloading(networkObject))
2937+
{
2938+
Debug.Log($"{networkObject.name} scene unloading in progress -- ignoring!");
29322939
return;
29332940
}
29342941

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,11 @@ internal void HandleNetworkPrefabDestroy(NetworkObject networkObjectInstance)
341341
prefabInstanceHandler.Destroy(networkObjectInstance);
342342
}
343343
}
344-
else // Otherwise the NetworkObject is the source NetworkPrefab
345-
if (m_PrefabAssetToPrefabHandler.TryGetValue(networkObjectInstanceHash, out var prefabInstanceHandler))
346-
{
347-
prefabInstanceHandler.Destroy(networkObjectInstance);
348-
}
344+
// Otherwise the NetworkObject is the source NetworkPrefab
345+
else if (m_PrefabAssetToPrefabHandler.TryGetValue(networkObjectInstanceHash, out var prefabInstanceHandler))
346+
{
347+
prefabInstanceHandler.Destroy(networkObjectInstance);
348+
}
349349
}
350350

351351
/// <summary>

0 commit comments

Comments
 (0)