Skip to content

Commit a7188d0

Browse files
committed
replace ContainsKey + index lookups by TryGetValue
+ Improve comment clarity
1 parent 17158fa commit a7188d0

1 file changed

Lines changed: 18 additions & 23 deletions

File tree

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

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -995,16 +995,14 @@ private void WriteSynchronizationData<T>(ref BufferSerializer<T> serializer) whe
995995
}
996996
stateHash = nextState.fullPathHash;
997997

998-
// Use the destination state to transition info lookup table to see if this is a transition we can
999-
// synchronize using cross-fading
1000-
if (m_DestinationStateToTransitionInfo.ContainsKey(layer))
998+
// Check if this transition can be synchronized using cross-fading
999+
if (m_DestinationStateToTransitionInfo.TryGetValue(layer, out var layerTransitions))
10011000
{
1002-
if (m_DestinationStateToTransitionInfo[layer].ContainsKey(nextState.shortNameHash))
1001+
if (layerTransitions.TryGetValue(nextState.shortNameHash, out var transitionInfo))
10031002
{
1004-
var destinationInfo = m_DestinationStateToTransitionInfo[layer][nextState.shortNameHash];
1005-
stateHash = destinationInfo.OriginatingState;
1003+
stateHash = transitionInfo.OriginatingState;
10061004
// Set the destination state to cross-fade to from the originating state
1007-
animationState.DestinationStateHash = destinationInfo.DestinationState;
1005+
animationState.DestinationStateHash = transitionInfo.DestinationState;
10081006
}
10091007
}
10101008
}
@@ -1095,12 +1093,13 @@ private void CheckForStateChange(int layer)
10951093
stateChangeDetected = true;
10961094
//Debug.Log($"[Cross-Fade] To-Hash: {nt.fullPathHash} | TI-Duration: ({tt.duration}) | TI-Norm: ({tt.normalizedTime}) | From-Hash: ({m_AnimationHash[layer]}) | SI-FPHash: ({st.fullPathHash}) | SI-Norm: ({st.normalizedTime})");
10971095
}
1098-
// If we are not transitioned into the "any state" and the animator transition isn't a full path hash (layer to layer) and our pre-built destination state to transition does not contain the
1099-
// current layer (i.e. transitioning into a state from another layer) =or= we do contain the layer and the layer contains state to transition to is contained within our pre-built destination
1100-
// state then we can handle this transition as a non-cross fade state transition between layers.
1101-
// Otherwise, if we don't enter into this then this is a "trigger transition to some state that is now being transitioned back to the Idle state via trigger" or "Dual Triggers" IDLE<-->State.
1102-
else if (!tt.anyState && tt.fullPathHash != m_TransitionHash[layer] && (!m_DestinationStateToTransitionInfo.ContainsKey(layer) ||
1103-
(m_DestinationStateToTransitionInfo.ContainsKey(layer) && m_DestinationStateToTransitionInfo[layer].ContainsKey(nt.fullPathHash))))
1096+
// Handle as a non-cross-fade transition when:
1097+
// - not an "any state" transition and this is a new transition on this layer
1098+
// - the layer is either absent from the lookup table (cross-layer transition) or its destination state is present
1099+
// Skipping this block means we are in a "dual trigger" scenario where a trigger transitions
1100+
// to a state that is immediately transitioned back via another trigger (e.g. IDLE <--> State).
1101+
else if (!tt.anyState && tt.fullPathHash != m_TransitionHash[layer] && (!m_DestinationStateToTransitionInfo.TryGetValue(layer, out var layerTransitions) ||
1102+
layerTransitions.ContainsKey(nt.fullPathHash)))
11041103
{
11051104
// first time in this transition for this layer
11061105
m_TransitionHash[layer] = tt.fullPathHash;
@@ -1110,7 +1109,7 @@ private void CheckForStateChange(int layer)
11101109
animState.CrossFade = false;
11111110
animState.Transition = true;
11121111
animState.NormalizedTime = tt.normalizedTime;
1113-
if (m_DestinationStateToTransitionInfo.ContainsKey(layer) && m_DestinationStateToTransitionInfo[layer].ContainsKey(nt.fullPathHash))
1112+
if (layerTransitions != null)
11141113
{
11151114
animState.DestinationStateHash = nt.fullPathHash;
11161115
}
@@ -1454,21 +1453,17 @@ private void UpdateAnimationState(AnimationState animationState)
14541453
// If it is a transition, then we are synchronizing transitions in progress when a client late joins
14551454
if (animationState.Transition && !animationState.CrossFade)
14561455
{
1457-
// We should have all valid entries for any animation state transition update
1458-
// Verify the AnimationState's assigned Layer exists
1459-
if (m_DestinationStateToTransitionInfo.ContainsKey(animationState.Layer))
1456+
// At this point all entries in the lookup table should be valid.
1457+
// Look up the transition info for this layer and destination state.
1458+
if (m_DestinationStateToTransitionInfo.TryGetValue(animationState.Layer, out var layerTransitions))
14601459
{
1461-
// Verify the inner-table has the destination AnimationState name hash
1462-
if (m_DestinationStateToTransitionInfo[animationState.Layer].ContainsKey(animationState.DestinationStateHash))
1460+
if (layerTransitions.TryGetValue(animationState.DestinationStateHash, out var transitionInfo))
14631461
{
14641462
// Make sure we are on the originating/starting state we are going to cross-fade into
14651463
if (currentState.shortNameHash == animationState.StateHash)
14661464
{
1467-
// Get the transition state information
1468-
var transitionStateInfo = m_DestinationStateToTransitionInfo[animationState.Layer][animationState.DestinationStateHash];
1469-
14701465
// Cross-fade from the current to the destination state for the transitions duration while starting at the server's current normalized time of the transition
1471-
m_Animator.CrossFade(transitionStateInfo.DestinationState, transitionStateInfo.TransitionDuration, transitionStateInfo.Layer, 0.0f, animationState.NormalizedTime);
1466+
m_Animator.CrossFade(transitionInfo.DestinationState, transitionInfo.TransitionDuration, transitionInfo.Layer, 0.0f, animationState.NormalizedTime);
14721467
}
14731468
else if (LocalNetworkManager.LogLevel == LogLevel.Developer)
14741469
{

0 commit comments

Comments
 (0)