|
| 1 | +#if COM_UNITY_MODULES_ANIMATION && UNITY_EDITOR |
| 2 | +using System.Collections; |
| 3 | +using System.Text; |
| 4 | +using Unity.Netcode.Components; |
| 5 | +using Unity.Netcode.TestHelpers.Runtime; |
| 6 | +using UnityEditor; |
| 7 | +using UnityEditor.Animations; |
| 8 | +using UnityEngine; |
| 9 | +using UnityEngine.TestTools; |
| 10 | + |
| 11 | +namespace Unity.Netcode.RuntimeTests |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Verifies a client that joins while the authority is mid-transition from a state other than |
| 15 | + /// the controller's default state still cross-fades into the transition's destination state. |
| 16 | + /// </summary> |
| 17 | + internal class NetworkAnimatorLateJoinTransitionTests : NetcodeIntegrationTest |
| 18 | + { |
| 19 | + private const string k_ControllerPath = "Assets/NetworkAnimatorLateJoinTransitionTest.controller"; |
| 20 | + private const string k_TriggerName = "ToB"; |
| 21 | + |
| 22 | + protected override int NumberOfClients => 1; |
| 23 | + |
| 24 | + private AnimatorController m_Controller; |
| 25 | + private AnimatorState m_DefaultState; |
| 26 | + private AnimatorState m_SourceState; |
| 27 | + private AnimatorState m_DestinationState; |
| 28 | + private GameObject m_Prefab; |
| 29 | + |
| 30 | + protected override void OnServerAndClientsCreated() |
| 31 | + { |
| 32 | + m_Controller = AnimatorController.CreateAnimatorControllerAtPath(k_ControllerPath); |
| 33 | + m_Controller.AddParameter(k_TriggerName, AnimatorControllerParameterType.Trigger); |
| 34 | + |
| 35 | + var rootStateMachine = m_Controller.layers[0].stateMachine; |
| 36 | + m_DefaultState = rootStateMachine.AddState("State A"); |
| 37 | + m_SourceState = rootStateMachine.AddState("State C"); |
| 38 | + m_DestinationState = rootStateMachine.AddState("State B"); |
| 39 | + rootStateMachine.defaultState = m_DefaultState; |
| 40 | + |
| 41 | + // Both states transition into the same destination, and the transitions are long enough to still be running when the client joins |
| 42 | + foreach (var state in new[] { m_DefaultState, m_SourceState }) |
| 43 | + { |
| 44 | + var transition = state.AddTransition(m_DestinationState); |
| 45 | + transition.hasExitTime = false; |
| 46 | + transition.duration = 10.0f; |
| 47 | + transition.AddCondition(AnimatorConditionMode.If, 0.0f, k_TriggerName); |
| 48 | + } |
| 49 | + AssetDatabase.SaveAssets(); |
| 50 | + |
| 51 | + m_Prefab = CreateNetworkObjectPrefab("Animated"); |
| 52 | + var animator = m_Prefab.AddComponent<Animator>(); |
| 53 | + animator.runtimeAnimatorController = m_Controller; |
| 54 | + var networkAnimator = m_Prefab.AddComponent<TestNetworkAnimator>(); |
| 55 | + networkAnimator.Animator = animator; |
| 56 | + networkAnimator.AnimatorParameterEntries = new NetworkAnimator.AnimatorParametersListContainer(); |
| 57 | + networkAnimator.InvokeOnValidate(); |
| 58 | + networkAnimator.OnAfterDeserialize(); |
| 59 | + |
| 60 | + base.OnServerAndClientsCreated(); |
| 61 | + } |
| 62 | + |
| 63 | + protected override IEnumerator OnTearDown() |
| 64 | + { |
| 65 | + AssetDatabase.DeleteAsset(k_ControllerPath); |
| 66 | + yield return base.OnTearDown(); |
| 67 | + } |
| 68 | + |
| 69 | + [UnityTest] |
| 70 | + public IEnumerator LateJoinFromNonDefaultStateCrossFadesToDestination() |
| 71 | + { |
| 72 | + var authority = GetAuthorityNetworkManager(); |
| 73 | + var authorityInstance = SpawnObject(m_Prefab, authority).GetComponent<NetworkObject>(); |
| 74 | + var authorityAnimator = authorityInstance.GetComponent<Animator>(); |
| 75 | + |
| 76 | + authorityAnimator.Play(m_SourceState.nameHash, 0); |
| 77 | + yield return WaitForConditionOrTimeOut(_ => authorityAnimator.GetCurrentAnimatorStateInfo(0).shortNameHash == m_SourceState.nameHash); |
| 78 | + AssertOnTimeout($"Authority did not enter {m_SourceState.name}!"); |
| 79 | + |
| 80 | + authorityAnimator.SetTrigger(k_TriggerName); |
| 81 | + yield return WaitForConditionOrTimeOut(_ => authorityAnimator.IsInTransition(0) && |
| 82 | + authorityAnimator.GetNextAnimatorStateInfo(0).shortNameHash == m_DestinationState.nameHash); |
| 83 | + AssertOnTimeout($"Authority did not start transitioning from {m_SourceState.name} to {m_DestinationState.name}!"); |
| 84 | + |
| 85 | + yield return CreateAndStartNewClient(); |
| 86 | + var lateJoinClient = m_ClientNetworkManagers[m_ClientNetworkManagers.Length - 1]; |
| 87 | + |
| 88 | + yield return WaitForConditionOrTimeOut(_ => lateJoinClient.SpawnManager.SpawnedObjects.ContainsKey(authorityInstance.NetworkObjectId)); |
| 89 | + AssertOnTimeout($"Late-joining client did not spawn {authorityInstance.name}!"); |
| 90 | + var clientAnimator = lateJoinClient.SpawnManager.SpawnedObjects[authorityInstance.NetworkObjectId].GetComponent<Animator>(); |
| 91 | + |
| 92 | + yield return WaitForConditionOrTimeOut(errorLog => ReachingDestination(clientAnimator, errorLog)); |
| 93 | + AssertOnTimeout($"Late-joining client did not cross-fade to {m_DestinationState.name}!"); |
| 94 | + } |
| 95 | + |
| 96 | + private bool ReachingDestination(Animator animator, StringBuilder errorLog) |
| 97 | + { |
| 98 | + var current = animator.GetCurrentAnimatorStateInfo(0).shortNameHash; |
| 99 | + if (current == m_DestinationState.nameHash || (animator.IsInTransition(0) && animator.GetNextAnimatorStateInfo(0).shortNameHash == m_DestinationState.nameHash)) |
| 100 | + { |
| 101 | + return true; |
| 102 | + } |
| 103 | + errorLog.Append($"Client current state hash {current}, in transition: {animator.IsInTransition(0)}"); |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + private class TestNetworkAnimator : NetworkAnimator |
| 108 | + { |
| 109 | + public void InvokeOnValidate() |
| 110 | + { |
| 111 | + base.OnValidate(); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | +#endif |
0 commit comments