forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: NetworkAnimator Synchronizing transition twice [MTT-3564] (Unity…
…-Technologies#2084) * fix This fixes the issue where transitions were being both triggered and the change in animation state (i.e. transition state) was being synchronized and applied to clients already transitioning which would cause any StateMachineBehaviours to have their OnStateEnter method invoked twice. * fix Making adjustments to account for changes to the Transition state update vs trigger update fix. * test Adding the additional StateMachineBehaviour (CheckStateEnter) to validate this fix. Adjusting two tests for this fix. Includes the animation related assets needed for the test. * style adding additional comments for some changes that were not obvious to the fix. * update removing code used to debug * style MTT-3564 updating changelog. * style correcting grammar in comment * style slight adjustment to the comment * style * update removing more debug related code. * style minor integration test variable rename.
- Loading branch information
1 parent
045cac3
commit 2d9f786
Showing
7 changed files
with
210 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
testproject/Assets/Tests/Manual/NetworkAnimatorTests/CheckStateEnterCount.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using Unity.Netcode; | ||
|
||
namespace TestProject.RuntimeTests | ||
{ | ||
public class CheckStateEnterCount : StateMachineBehaviour | ||
{ | ||
public static Dictionary<ulong, Dictionary<int, List<AnimatorStateInfo>>> OnStateEnterCounter = new Dictionary<ulong, Dictionary<int, List<AnimatorStateInfo>>>(); | ||
public static bool IsIntegrationTest; | ||
public static bool IsManualTestEnabled = true; | ||
|
||
public static void ResetTest(bool isIntegrationTest = true) | ||
{ | ||
IsIntegrationTest = isIntegrationTest; | ||
IsManualTestEnabled = !isIntegrationTest; | ||
OnStateEnterCounter.Clear(); | ||
} | ||
|
||
public static bool AllStatesEnteredMatch(List<ulong> clientIdsToCheck) | ||
{ | ||
if (clientIdsToCheck.Contains(NetworkManager.ServerClientId)) | ||
{ | ||
clientIdsToCheck.Remove(NetworkManager.ServerClientId); | ||
} | ||
|
||
if (!OnStateEnterCounter.ContainsKey(NetworkManager.ServerClientId)) | ||
{ | ||
Debug.Log($"Server has not entered into any states! OnStateEntered Entry Count ({OnStateEnterCounter.Count})"); | ||
return false; | ||
} | ||
|
||
var serverStates = OnStateEnterCounter[NetworkManager.ServerClientId]; | ||
|
||
foreach (var layerEntries in serverStates) | ||
{ | ||
var layerIndex = layerEntries.Key; | ||
var layerStates = layerEntries.Value; | ||
if (layerStates.Count > 1) | ||
{ | ||
Debug.Log($"Server layer ({layerIndex}) state was entered ({layerStates.Count}) times!"); | ||
return false; | ||
} | ||
|
||
foreach (var clientId in clientIdsToCheck) | ||
{ | ||
if (!OnStateEnterCounter.ContainsKey(clientId)) | ||
{ | ||
Debug.Log($"Client-{clientId} never entered into any state for layer index ({layerIndex})!"); | ||
return false; | ||
} | ||
var clientStates = OnStateEnterCounter[clientId]; | ||
if (!clientStates.ContainsKey(layerIndex)) | ||
{ | ||
Debug.Log($"Client-{clientId} never layer ({layerIndex}) state!"); | ||
return false; | ||
} | ||
var clientLayerStateEntries = clientStates[layerIndex]; | ||
if (clientLayerStateEntries.Count > 1) | ||
{ | ||
Debug.Log($"Client-{clientId} layer ({layerIndex}) state was entered ({layerStates.Count}) times!"); | ||
return false; | ||
} | ||
// We should have only entered into the state once on the server | ||
// and all connected clients | ||
var serverAnimStateInfo = layerStates[0]; | ||
var clientAnimStateInfo = clientLayerStateEntries[0]; | ||
// We just need to make sure we are looking at the same state | ||
if (clientAnimStateInfo.fullPathHash != serverAnimStateInfo.fullPathHash) | ||
{ | ||
Debug.Log($"Client-{clientId} full path hash ({clientAnimStateInfo.fullPathHash}) for layer ({layerIndex}) was not the same as the Server full path hash ({serverAnimStateInfo.fullPathHash})!"); | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) | ||
{ | ||
if (IsIntegrationTest) | ||
{ | ||
var networkObject = animator.GetComponent<NetworkObject>(); | ||
var localClientId = networkObject.NetworkManager.IsServer ? NetworkManager.ServerClientId : networkObject.NetworkManager.LocalClientId; | ||
if (!OnStateEnterCounter.ContainsKey(localClientId)) | ||
{ | ||
OnStateEnterCounter.Add(localClientId, new Dictionary<int, List<AnimatorStateInfo>>()); | ||
} | ||
if (!OnStateEnterCounter[localClientId].ContainsKey(layerIndex)) | ||
{ | ||
OnStateEnterCounter[localClientId].Add(layerIndex, new List<AnimatorStateInfo>()); | ||
} | ||
OnStateEnterCounter[localClientId][layerIndex].Add(stateInfo); | ||
} | ||
else if (IsManualTestEnabled) | ||
{ | ||
Debug.Log($"[{layerIndex}][{stateInfo.shortNameHash}][{stateInfo.normalizedTime}][{animator.IsInTransition(layerIndex)}]"); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
testproject/Assets/Tests/Manual/NetworkAnimatorTests/CheckStateEnterCount.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.