This repository has been archived on 2025-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
com.unity.netcode.gameobjects/Tests/Runtime/NetworkAnimator/NetworkAnimatorTests.cs
Unity Technologies 60e2dabef4 com.unity.netcode.gameobjects@1.0.0-pre.7
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).

## [1.0.0-pre.7] - 2022-04-01

### Added

- Added editor only check prior to entering into play mode if the currently open and active scene is in the build list and if not displays a dialog box asking the user if they would like to automatically add it prior to entering into play mode. (#1828)
- Added `UnityTransport` implementation and `com.unity.transport` package dependency (#1823)
- Added `NetworkVariableWritePermission` to `NetworkVariableBase` and implemented `Owner` client writable netvars. (#1762)
- `UnityTransport` settings can now be set programmatically. (#1845)
- `FastBufferWriter` and Reader IsInitialized property. (#1859)

### Changed

- Updated `UnityTransport` dependency on `com.unity.transport` to 1.0.0 (#1849)

### Removed

- Removed `SnapshotSystem` (#1852)
- Removed `com.unity.modules.animation`, `com.unity.modules.physics` and `com.unity.modules.physics2d` dependencies from the package (#1812)
- Removed `com.unity.collections` dependency from the package (#1849)

### Fixed
- Fixed in-scene placed NetworkObjects not being found/ignored after a client disconnects and then reconnects. (#1850)
- Fixed issue where `UnityTransport` send queues were not flushed when calling `DisconnectLocalClient` or `DisconnectRemoteClient`. (#1847)
- Fixed NetworkBehaviour dependency verification check for an existing NetworkObject not searching from root parent transform relative GameObject. (#1841)
- Fixed issue where entries were not being removed from the NetworkSpawnManager.OwnershipToObjectsTable. (#1838)
- Fixed ClientRpcs would always send to all connected clients by default as opposed to only sending to the NetworkObject's Observers list by default. (#1836)
- Fixed clarity for NetworkSceneManager client side notification when it receives a scene hash value that does not exist in its local hash table. (#1828)
- Fixed client throws a key not found exception when it times out using UNet or UTP. (#1821)
- Fixed network variable updates are no longer limited to 32,768 bytes when NetworkConfig.EnsureNetworkVariableLengthSafety is enabled. The limits are now determined by what the transport can send in a message. (#1811)
- Fixed in-scene NetworkObjects get destroyed if a client fails to connect and shuts down the NetworkManager. (#1809)
- Fixed user never being notified in the editor that a NetworkBehaviour requires a NetworkObject to function properly. (#1808)
- Fixed PlayerObjects and dynamically spawned NetworkObjects not being added to the NetworkClient's OwnedObjects (#1801)
- Fixed issue where NetworkManager would continue starting even if the NetworkTransport selected failed. (#1780)
- Fixed issue when spawning new player if an already existing player exists it does not remove IsPlayer from the previous player (#1779)
- Fixed lack of notification that NetworkManager and NetworkObject cannot be added to the same GameObject with in-editor notifications (#1777)
- Fixed parenting warning printing for false positives (#1855)
2022-04-01 00:00:00 +00:00

240 lines
12 KiB
C#

#if COM_UNITY_MODULES_ANIMATION
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using Unity.Netcode.Components;
using UnityEngine;
using UnityEngine.TestTools;
using Unity.Netcode.TestHelpers.Runtime;
namespace Unity.Netcode.RuntimeTests
{
[TestFixture(HostOrServer.Host)]
[TestFixture(HostOrServer.Server)]
public class NetworkAnimatorTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 1;
private GameObject m_PlayerOnServer;
private GameObject m_PlayerOnClient;
private Animator m_PlayerOnServerAnimator;
private Animator m_PlayerOnClientAnimator;
public NetworkAnimatorTests(HostOrServer hostOrServer) : base(hostOrServer) { }
protected override void OnCreatePlayerPrefab()
{
// ideally, we would build up the AnimatorController entirely in code and not need an asset,
// but after some attempts this doesn't seem readily doable. Instead, we load a controller
var controller = Resources.Load("TestAnimatorController") as RuntimeAnimatorController;
var animator = m_PlayerPrefab.AddComponent<Animator>();
animator.runtimeAnimatorController = controller;
var networkAnimator = m_PlayerPrefab.AddComponent<NetworkAnimator>();
networkAnimator.Animator = animator;
}
protected override IEnumerator OnServerAndClientsConnected()
{
m_PlayerOnServer = m_PlayerNetworkObjects[m_ServerNetworkManager.LocalClientId][m_ClientNetworkManagers[0].LocalClientId].gameObject;
m_PlayerOnServerAnimator = m_PlayerOnServerAnimator = m_PlayerOnServer.GetComponent<Animator>();
m_PlayerOnClient = m_PlayerNetworkObjects[m_ClientNetworkManagers[0].LocalClientId][m_ClientNetworkManagers[0].LocalClientId].gameObject;
m_PlayerOnClientAnimator = m_PlayerOnClient.GetComponent<Animator>();
return base.OnServerAndClientsConnected();
}
// helper function to scan an animator and verify a given clip is present
private bool HasClip(Animator animator, string clipName)
{
var clips = new List<AnimatorClipInfo>();
animator.GetCurrentAnimatorClipInfo(0, clips);
foreach (var clip in clips)
{
if (clip.clip.name == clipName)
{
return true;
}
}
return false;
}
[UnityTest]
public IEnumerator AnimationTriggerReset([Values(true, false)] bool asHash)
{
// We have "UnboundTrigger" purposely not bound to any animations so we can test resetting.
// If we used a trigger that was bound to a transition, then the trigger would reset as soon as the
// transition happens. This way it will stay stuck on
string triggerString = "UnboundTrigger";
int triggerHash = Animator.StringToHash(triggerString);
// Verify trigger is off
Assert.True(m_PlayerOnServerAnimator.GetBool(triggerString) == false);
Assert.True(m_PlayerOnClientAnimator.GetBool(triggerString) == false);
// trigger.
if (asHash)
{
m_PlayerOnServer.GetComponent<NetworkAnimator>().SetTrigger(triggerHash);
}
else
{
m_PlayerOnServer.GetComponent<NetworkAnimator>().SetTrigger(triggerString);
}
// verify trigger is set for client and server
yield return WaitForConditionOrTimeOut(() => asHash ? m_PlayerOnServerAnimator.GetBool(triggerHash) : m_PlayerOnServerAnimator.GetBool(triggerString));
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out on server trigger set check");
yield return WaitForConditionOrTimeOut(() => asHash ? m_PlayerOnClientAnimator.GetBool(triggerHash) : m_PlayerOnClientAnimator.GetBool(triggerString));
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out on client trigger set check");
// reset the trigger
if (asHash)
{
m_PlayerOnServer.GetComponent<NetworkAnimator>().ResetTrigger(triggerHash);
}
else
{
m_PlayerOnServer.GetComponent<NetworkAnimator>().ResetTrigger(triggerString);
}
// verify trigger is reset for client and server
yield return WaitForConditionOrTimeOut(() => asHash ? m_PlayerOnServerAnimator.GetBool(triggerHash) == false : m_PlayerOnServerAnimator.GetBool(triggerString) == false);
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out on server reset check");
yield return WaitForConditionOrTimeOut(() => asHash ? m_PlayerOnClientAnimator.GetBool(triggerHash) == false : m_PlayerOnClientAnimator.GetBool(triggerString) == false);
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out on client reset check");
}
[UnityTest]
public IEnumerator AnimationStateSyncTest()
{
// check that we have started in the default state
Assert.True(m_PlayerOnServerAnimator.GetCurrentAnimatorStateInfo(0).IsName("DefaultState"));
Assert.True(m_PlayerOnClientAnimator.GetCurrentAnimatorStateInfo(0).IsName("DefaultState"));
// cause a change to the AlphaState state by setting AlphaParameter, which is
// the variable bound to the transition from default to AlphaState (see the TestAnimatorController asset)
m_PlayerOnServerAnimator.SetBool("AlphaParameter", true);
// ...and now we should be in the AlphaState having triggered the AlphaParameter
yield return WaitForConditionOrTimeOut(() => m_PlayerOnServerAnimator.GetCurrentAnimatorStateInfo(0).IsName("AlphaState"));
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Server failed to reach its animation state");
// ...and now the client should also have sync'd and arrived at the correct state
yield return WaitForConditionOrTimeOut(() => m_PlayerOnClientAnimator.GetCurrentAnimatorStateInfo(0).IsName("AlphaState"));
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Client failed to sync its animation state from the server");
}
[UnityTest]
public IEnumerator AnimationLayerStateSyncTest()
{
int layer = 1;
// check that we have started in the default state
Assert.True(m_PlayerOnServerAnimator.GetCurrentAnimatorStateInfo(layer).IsName("DefaultStateLayer2"));
Assert.True(m_PlayerOnClientAnimator.GetCurrentAnimatorStateInfo(layer).IsName("DefaultStateLayer2"));
// cause a change to the AlphaState state by setting AlphaParameter, which is
// the variable bound to the transition from default to AlphaState (see the TestAnimatorController asset)
m_PlayerOnServerAnimator.SetBool("Layer2AlphaParameter", true);
// ...and now we should be in the AlphaState having triggered the AlphaParameter
yield return WaitForConditionOrTimeOut(() => m_PlayerOnServerAnimator.GetCurrentAnimatorStateInfo(layer).IsName("Layer2AlphaState"));
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Server failed to reach its animation state");
// ...and now the client should also have sync'd and arrived at the correct state
yield return WaitForConditionOrTimeOut(() => m_PlayerOnClientAnimator.GetCurrentAnimatorStateInfo(layer).IsName("Layer2AlphaState"));
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Client failed to sync its animation state from the server");
}
[UnityTest]
public IEnumerator AnimationLayerWeightTest()
{
int layer = 1;
float targetWeight = 0.333f;
// check that we have started in the default state
Assert.True(Mathf.Approximately(m_PlayerOnServerAnimator.GetLayerWeight(layer), 1f));
Assert.True(Mathf.Approximately(m_PlayerOnClientAnimator.GetLayerWeight(layer), 1f));
m_PlayerOnServerAnimator.SetLayerWeight(layer, targetWeight);
// ...and now we should be in the AlphaState having triggered the AlphaParameter
yield return WaitForConditionOrTimeOut(() =>
Mathf.Approximately(m_PlayerOnServerAnimator.GetLayerWeight(layer), targetWeight)
);
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Server failed to reach its animation state");
// ...and now the client should also have sync'd and arrived at the correct state
yield return WaitForConditionOrTimeOut(() =>
Mathf.Approximately(m_PlayerOnClientAnimator.GetLayerWeight(layer), targetWeight)
);
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Server failed to reach its animation state");
}
[UnityTest]
public IEnumerator AnimationStateSyncTriggerTest([Values(true, false)] bool asHash)
{
string triggerString = "TestTrigger";
int triggerHash = Animator.StringToHash(triggerString);
// check that we have started in the default state
Assert.True(m_PlayerOnServerAnimator.GetCurrentAnimatorStateInfo(0).IsName("DefaultState"));
Assert.True(m_PlayerOnClientAnimator.GetCurrentAnimatorStateInfo(0).IsName("DefaultState"));
// cause a change to the AlphaState state by setting TestTrigger
// note, we have a special test for triggers because activating triggers via the
// NetworkAnimator is special; for other parameters you set them on the Animator and NetworkAnimator
// listens. But because triggers are super short and transitory, we require users to call
// NetworkAnimator.SetTrigger so we don't miss it
if (asHash)
{
m_PlayerOnServer.GetComponent<NetworkAnimator>().SetTrigger(triggerHash);
}
else
{
m_PlayerOnServer.GetComponent<NetworkAnimator>().SetTrigger(triggerString);
}
// ...and now we should be in the AlphaState having triggered the AlphaParameter
yield return WaitForConditionOrTimeOut(() => m_PlayerOnServerAnimator.GetCurrentAnimatorStateInfo(0).IsName("TriggeredState"));
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Server failed to reach its animation state via trigger");
// ...and now the client should also have sync'd and arrived at the correct state
yield return WaitForConditionOrTimeOut(() => m_PlayerOnClientAnimator.GetCurrentAnimatorStateInfo(0).IsName("TriggeredState"));
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Client failed to sync its animation state from the server via trigger");
}
[UnityTest]
public IEnumerator AnimationStateSyncTestWithOverride()
{
// set up the animation override controller
var overrideController = Resources.Load("TestAnimatorOverrideController") as AnimatorOverrideController;
m_PlayerOnServer.GetComponent<Animator>().runtimeAnimatorController = overrideController;
m_PlayerOnClient.GetComponent<Animator>().runtimeAnimatorController = overrideController;
// in our default state, we should see the OverrideDefaultAnimation clip
Assert.True(HasClip(m_PlayerOnServerAnimator, "OverrideDefaultAnimation"));
Assert.True(HasClip(m_PlayerOnClientAnimator, "OverrideDefaultAnimation"));
// cause a change to the AlphaState state by setting AlphaParameter, which is
// the variable bound to the transition from default to AlphaState (see the TestAnimatorController asset)
m_PlayerOnServerAnimator.SetBool("AlphaParameter", true);
// ...and now we should be in the AlphaState having set the AlphaParameter
yield return WaitForConditionOrTimeOut(() => HasClip(m_PlayerOnServerAnimator, "OverrideAlphaAnimation"));
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Server failed to reach its overriden animation state");
// ...and now the client should also have sync'd and arrived at the correct state
yield return WaitForConditionOrTimeOut(() => HasClip(m_PlayerOnServerAnimator, "OverrideAlphaAnimation"));
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Client failed to reach its overriden animation state");
}
}
}
#endif // COM_UNITY_MODULES_ANIMATION