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). ## [2.0.0-pre.4] - 2024-08-21 ### Added - Added `NetworkVariable.CheckDirtyState` that is to be used in tandem with collections in order to detect whether the collection or an item within the collection has changed. (#3004) ### Fixed - Fixed issue where nested `NetworkTransform` components were not getting updated. (#3016) - Fixed issue by adding null checks in `NetworkVariableBase.CanClientRead` and `NetworkVariableBase.CanClientWrite` methods to ensure safe access to `NetworkBehaviour`. (#3012) - Fixed issue where `FixedStringSerializer<T>` was using `NetworkVariableSerialization<byte>.AreEqual` to determine if two bytes were equal causes an exception to be thrown due to no byte serializer having been defined. (#3009) - Fixed Issue where a state with dual triggers, inbound and outbound, could cause a false layer to layer state transition message to be sent to non-authority `NetworkAnimator` instances and cause a warning message to be logged. (#3008) - Fixed issue using collections within `NetworkVariable` where the collection would not detect changes to items or nested items. (#3004) - Fixed issue where `List`, `Dictionary`, and `HashSet` collections would not uniquely duplicate nested collections. (#3004) - Fixed issue where `NotAuthorityTarget` would include the service observer in the list of targets to send the RPC to as opposed to excluding the service observer as it should. (#3000) - Fixed issue where `ProxyRpcTargetGroup` could attempt to send a message if there were no targets to send to. (#3000) ### Changed - Changed `NetworkAnimator` to automatically switch to owner authoritative mode when using a distributed authority network topology. (#3021) - Changed permissions exception thrown in `NetworkList` to exiting early with a logged error that is now a unified permissions message within `NetworkVariableBase`. (#3004) - Changed permissions exception thrown in `NetworkVariable.Value` to exiting early with a logged error that is now a unified permissions message within `NetworkVariableBase`. (#3004)
139 lines
5.5 KiB
C#
139 lines
5.5 KiB
C#
using NUnit.Framework;
|
|
using Unity.Netcode.TestHelpers.Runtime;
|
|
using UnityEngine;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace Unity.Netcode.RuntimeTests
|
|
{
|
|
internal class NetworkVariableTraitsComponent : NetworkBehaviour
|
|
{
|
|
public NetworkVariable<float> TheVariable = new NetworkVariable<float>();
|
|
}
|
|
|
|
internal class NetworkVariableTraitsTests : NetcodeIntegrationTest
|
|
{
|
|
protected override int NumberOfClients => 2;
|
|
|
|
protected override bool m_EnableTimeTravel => true;
|
|
protected override bool m_SetupIsACoroutine => false;
|
|
protected override bool m_TearDownIsACoroutine => false;
|
|
|
|
protected override void OnPlayerPrefabGameObjectCreated()
|
|
{
|
|
m_PlayerPrefab.AddComponent<NetworkVariableTraitsComponent>();
|
|
}
|
|
|
|
public NetworkVariableTraitsComponent GetTestComponent()
|
|
{
|
|
return m_ClientNetworkManagers[0].LocalClient.PlayerObject.GetComponent<NetworkVariableTraitsComponent>();
|
|
}
|
|
|
|
public NetworkVariableTraitsComponent GetServerComponent()
|
|
{
|
|
foreach (var obj in Object.FindObjectsByType<NetworkVariableTraitsComponent>(FindObjectsSortMode.None))
|
|
{
|
|
if (obj.NetworkManager == m_ServerNetworkManager && obj.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId)
|
|
{
|
|
return obj;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
[Test]
|
|
public void WhenNewValueIsLessThanThreshold_VariableIsNotSerialized()
|
|
{
|
|
var serverComponent = GetServerComponent();
|
|
var testComponent = GetTestComponent();
|
|
serverComponent.TheVariable.CheckExceedsDirtinessThreshold = (in float value, in float newValue) => Mathf.Abs(newValue - value) >= 0.1;
|
|
|
|
serverComponent.TheVariable.Value = 0.05f;
|
|
|
|
TimeTravel(2, 120);
|
|
|
|
Assert.AreEqual(0.05f, serverComponent.TheVariable.Value); ;
|
|
Assert.AreEqual(0, testComponent.TheVariable.Value); ;
|
|
}
|
|
[Test]
|
|
public void WhenNewValueIsGreaterThanThreshold_VariableIsSerialized()
|
|
{
|
|
var serverComponent = GetServerComponent();
|
|
var testComponent = GetTestComponent();
|
|
serverComponent.TheVariable.CheckExceedsDirtinessThreshold = (in float value, in float newValue) => Mathf.Abs(newValue - value) >= 0.1;
|
|
|
|
serverComponent.TheVariable.Value = 0.15f;
|
|
|
|
TimeTravel(2, 120);
|
|
|
|
Assert.AreEqual(0.15f, serverComponent.TheVariable.Value); ;
|
|
Assert.AreEqual(0.15f, testComponent.TheVariable.Value); ;
|
|
}
|
|
|
|
[Test]
|
|
public void WhenNewValueIsLessThanThresholdButMaxTimeHasPassed_VariableIsSerialized()
|
|
{
|
|
var serverComponent = GetServerComponent();
|
|
var testComponent = GetTestComponent();
|
|
serverComponent.TheVariable.CheckExceedsDirtinessThreshold = (in float value, in float newValue) => Mathf.Abs(newValue - value) >= 0.1;
|
|
serverComponent.TheVariable.SetUpdateTraits(new NetworkVariableUpdateTraits { MaxSecondsBetweenUpdates = 2 });
|
|
serverComponent.TheVariable.LastUpdateSent = m_ServerNetworkManager.NetworkTimeSystem.LocalTime;
|
|
|
|
serverComponent.TheVariable.Value = 0.05f;
|
|
|
|
TimeTravel(1 / 60f * 119, 119);
|
|
|
|
Assert.AreEqual(0.05f, serverComponent.TheVariable.Value); ;
|
|
Assert.AreEqual(0, testComponent.TheVariable.Value); ;
|
|
|
|
TimeTravel(1 / 60f * 4, 4);
|
|
|
|
Assert.AreEqual(0.05f, serverComponent.TheVariable.Value); ;
|
|
Assert.AreEqual(0.05f, testComponent.TheVariable.Value); ;
|
|
}
|
|
|
|
[Test]
|
|
public void WhenNewValueIsGreaterThanThresholdButMinTimeHasNotPassed_VariableIsNotSerialized()
|
|
{
|
|
var serverComponent = GetServerComponent();
|
|
var testComponent = GetTestComponent();
|
|
serverComponent.TheVariable.CheckExceedsDirtinessThreshold = (in float value, in float newValue) => Mathf.Abs(newValue - value) >= 0.1;
|
|
serverComponent.TheVariable.SetUpdateTraits(new NetworkVariableUpdateTraits { MinSecondsBetweenUpdates = 2 });
|
|
serverComponent.TheVariable.LastUpdateSent = m_ServerNetworkManager.NetworkTimeSystem.LocalTime;
|
|
|
|
serverComponent.TheVariable.Value = 0.15f;
|
|
|
|
TimeTravel(1 / 60f * 119, 119);
|
|
|
|
Assert.AreEqual(0.15f, serverComponent.TheVariable.Value); ;
|
|
Assert.AreEqual(0, testComponent.TheVariable.Value); ;
|
|
|
|
TimeTravel(1 / 60f * 4, 4);
|
|
|
|
Assert.AreEqual(0.15f, serverComponent.TheVariable.Value); ;
|
|
Assert.AreEqual(0.15f, testComponent.TheVariable.Value); ;
|
|
}
|
|
|
|
[Test]
|
|
public void WhenNoThresholdIsSetButMinTimeHasNotPassed_VariableIsNotSerialized()
|
|
{
|
|
var serverComponent = GetServerComponent();
|
|
var testComponent = GetTestComponent();
|
|
serverComponent.TheVariable.SetUpdateTraits(new NetworkVariableUpdateTraits { MinSecondsBetweenUpdates = 2 });
|
|
serverComponent.TheVariable.LastUpdateSent = m_ServerNetworkManager.NetworkTimeSystem.LocalTime;
|
|
|
|
serverComponent.TheVariable.Value = 0.15f;
|
|
|
|
TimeTravel(1 / 60f * 119, 119);
|
|
|
|
Assert.AreEqual(0.15f, serverComponent.TheVariable.Value); ;
|
|
Assert.AreEqual(0, testComponent.TheVariable.Value); ;
|
|
|
|
TimeTravel(1 / 60f * 4, 4);
|
|
|
|
Assert.AreEqual(0.15f, serverComponent.TheVariable.Value); ;
|
|
Assert.AreEqual(0.15f, testComponent.TheVariable.Value); ;
|
|
}
|
|
}
|
|
}
|