com.unity.netcode.gameobjects@2.0.0-pre.2
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.2] - 2024-06-17 ### Added - Added `AnticipatedNetworkVariable<T>`, which adds support for client anticipation of `NetworkVariable` values, allowing for more responsive gameplay. (#2957) - Added `AnticipatedNetworkTransform`, which adds support for client anticipation of NetworkTransforms. (#2957) - Added `NetworkVariableBase.ExceedsDirtinessThreshold` to allow network variables to throttle updates by only sending updates when the difference between the current and previous values exceeds a threshold. (This is exposed in `NetworkVariable<T>` with the callback `NetworkVariable<T>.CheckExceedsDirtinessThreshold`). (#2957) - Added `NetworkVariableUpdateTraits`, which add additional throttling support: `MinSecondsBetweenUpdates` will prevent the `NetworkVariable` from sending updates more often than the specified time period (even if it exceeds the dirtiness threshold), while `MaxSecondsBetweenUpdates` will force a dirty `NetworkVariable` to send an update after the specified time period even if it has not yet exceeded the dirtiness threshold. (#2957) - Added virtual method `NetworkVariableBase.OnInitialize` which can be used by `NetworkVariable` subclasses to add initialization code. (#2957) - Added `NetworkTime.TickWithPartial`, which represents the current tick as a double that includes the fractional/partial tick value. (#2957) - Added `NetworkTickSystem.AnticipationTick`, which can be helpful with implementation of client anticipation. This value represents the tick the current local client was at at the beginning of the most recent network round trip, which enables it to correlate server update ticks with the client tick that may have triggered them. (#2957) - Added event `NetworkManager.OnSessionOwnerPromoted` that is invoked when a new session owner promotion occurs. (#2948) - Added `NetworkRigidBodyBase.GetLinearVelocity` and `NetworkRigidBodyBase.SetLinearVelocity` convenience/helper methods. (#2948) - Added `NetworkRigidBodyBase.GetAngularVelocity` and `NetworkRigidBodyBase.SetAngularVelocity` convenience/helper methods. (#2948) ### Fixed - Fixed issue when `NetworkTransform` half float precision is enabled and ownership changes the current base position was not being synchronized. (#2948) - Fixed issue where `OnClientConnected` not being invoked on the session owner when connecting to a new distributed authority session. (#2948) - Fixed issue where Rigidbody micro-motion (i.e. relatively small velocities) would result in non-authority instances slightly stuttering as the body would come to a rest (i.e. no motion). Now, the threshold value can increase at higher velocities and can decrease slightly below the provided threshold to account for this. (#2948) ### Changed - Changed `NetworkAnimator` no longer requires the `Animator` component to exist on the same `GameObject`. (#2957) - Changed `NetworkObjectReference` and `NetworkBehaviourReference` to allow null references when constructing and serializing. (#2957) - Changed the client's owned objects is now returned (`NetworkClient` and `NetworkSpawnManager`) as an array as opposed to a list for performance purposes. (#2948) - Changed `NetworkTransfrom.TryCommitTransformToServer` to be internal as it will be removed by the final 2.0.0 release. (#2948) - Changed `NetworkTransformEditor.OnEnable` to a virtual method to be able to customize a `NetworkTransform` derived class by creating a derived editor control from `NetworkTransformEditor`. (#2948)
This commit is contained in:
@@ -4,6 +4,7 @@ using NUnit.Framework;
|
||||
using Unity.Netcode.TestHelpers.Runtime;
|
||||
using UnityEngine;
|
||||
using UnityEngine.TestTools;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Unity.Netcode.RuntimeTests
|
||||
{
|
||||
@@ -35,7 +36,7 @@ namespace Unity.Netcode.RuntimeTests
|
||||
|
||||
m_ServerManager.OnServerStopped += onServerStopped;
|
||||
m_ServerManager.Shutdown();
|
||||
UnityEngine.Object.DestroyImmediate(gameObject);
|
||||
Object.DestroyImmediate(gameObject);
|
||||
|
||||
yield return WaitUntilManagerShutsdown();
|
||||
|
||||
@@ -92,7 +93,7 @@ namespace Unity.Netcode.RuntimeTests
|
||||
m_ServerManager.OnServerStopped += onServerStopped;
|
||||
m_ServerManager.OnClientStopped += onClientStopped;
|
||||
m_ServerManager.Shutdown();
|
||||
UnityEngine.Object.DestroyImmediate(gameObject);
|
||||
Object.DestroyImmediate(gameObject);
|
||||
|
||||
yield return WaitUntilManagerShutsdown();
|
||||
|
||||
@@ -228,6 +229,18 @@ namespace Unity.Netcode.RuntimeTests
|
||||
public virtual IEnumerator Teardown()
|
||||
{
|
||||
NetcodeIntegrationTestHelpers.Destroy();
|
||||
if (m_ServerManager != null)
|
||||
{
|
||||
m_ServerManager.ShutdownInternal();
|
||||
Object.DestroyImmediate(m_ServerManager);
|
||||
m_ServerManager = null;
|
||||
}
|
||||
if (m_ClientManager != null)
|
||||
{
|
||||
m_ClientManager.ShutdownInternal();
|
||||
Object.DestroyImmediate(m_ClientManager);
|
||||
m_ClientManager = null;
|
||||
}
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
521
Tests/Runtime/NetworkTransformAnticipationTests.cs
Normal file
521
Tests/Runtime/NetworkTransformAnticipationTests.cs
Normal file
@@ -0,0 +1,521 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using Unity.Netcode.Components;
|
||||
using Unity.Netcode.TestHelpers.Runtime;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Unity.Netcode.RuntimeTests
|
||||
{
|
||||
internal class NetworkTransformAnticipationComponent : NetworkBehaviour
|
||||
{
|
||||
[Rpc(SendTo.Server)]
|
||||
public void MoveRpc(Vector3 newPosition)
|
||||
{
|
||||
transform.position = newPosition;
|
||||
}
|
||||
|
||||
[Rpc(SendTo.Server)]
|
||||
public void ScaleRpc(Vector3 newScale)
|
||||
{
|
||||
transform.localScale = newScale;
|
||||
}
|
||||
|
||||
[Rpc(SendTo.Server)]
|
||||
public void RotateRpc(Quaternion newRotation)
|
||||
{
|
||||
transform.rotation = newRotation;
|
||||
}
|
||||
|
||||
public bool ShouldSmooth = false;
|
||||
public bool ShouldMove = false;
|
||||
|
||||
public override void OnReanticipate(double lastRoundTripTime)
|
||||
{
|
||||
var transform_ = GetComponent<AnticipatedNetworkTransform>();
|
||||
if (transform_.ShouldReanticipate)
|
||||
{
|
||||
if (ShouldSmooth)
|
||||
{
|
||||
transform_.Smooth(transform_.PreviousAnticipatedState, transform_.AuthoritativeState, 1);
|
||||
}
|
||||
|
||||
if (ShouldMove)
|
||||
{
|
||||
transform_.AnticipateMove(transform_.AuthoritativeState.Position + new Vector3(0, 5, 0));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class NetworkTransformAnticipationTests : 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<AnticipatedNetworkTransform>();
|
||||
m_PlayerPrefab.AddComponent<NetworkTransformAnticipationComponent>();
|
||||
}
|
||||
|
||||
protected override void OnTimeTravelServerAndClientsConnected()
|
||||
{
|
||||
var serverComponent = GetServerComponent();
|
||||
var testComponent = GetTestComponent();
|
||||
var otherClientComponent = GetOtherClientComponent();
|
||||
|
||||
serverComponent.transform.position = Vector3.zero;
|
||||
serverComponent.transform.localScale = Vector3.one;
|
||||
serverComponent.transform.rotation = Quaternion.LookRotation(Vector3.forward);
|
||||
testComponent.transform.position = Vector3.zero;
|
||||
testComponent.transform.localScale = Vector3.one;
|
||||
testComponent.transform.rotation = Quaternion.LookRotation(Vector3.forward);
|
||||
otherClientComponent.transform.position = Vector3.zero;
|
||||
otherClientComponent.transform.localScale = Vector3.one;
|
||||
otherClientComponent.transform.rotation = Quaternion.LookRotation(Vector3.forward);
|
||||
}
|
||||
|
||||
public AnticipatedNetworkTransform GetTestComponent()
|
||||
{
|
||||
return m_ClientNetworkManagers[0].LocalClient.PlayerObject.GetComponent<AnticipatedNetworkTransform>();
|
||||
}
|
||||
|
||||
public AnticipatedNetworkTransform GetServerComponent()
|
||||
{
|
||||
foreach (var obj in Object.FindObjectsByType<AnticipatedNetworkTransform>(FindObjectsSortMode.None))
|
||||
{
|
||||
if (obj.NetworkManager == m_ServerNetworkManager && obj.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId)
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public AnticipatedNetworkTransform GetOtherClientComponent()
|
||||
{
|
||||
foreach (var obj in Object.FindObjectsByType<AnticipatedNetworkTransform>(FindObjectsSortMode.None))
|
||||
{
|
||||
if (obj.NetworkManager == m_ClientNetworkManagers[1] && obj.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId)
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenAnticipating_ValueChangesImmediately()
|
||||
{
|
||||
var testComponent = GetTestComponent();
|
||||
|
||||
testComponent.AnticipateMove(new Vector3(0, 1, 2));
|
||||
testComponent.AnticipateScale(new Vector3(1, 2, 3));
|
||||
testComponent.AnticipateRotate(Quaternion.LookRotation(new Vector3(2, 3, 4)));
|
||||
|
||||
Assert.AreEqual(new Vector3(0, 1, 2), testComponent.transform.position);
|
||||
Assert.AreEqual(new Vector3(1, 2, 3), testComponent.transform.localScale);
|
||||
Assert.AreEqual(Quaternion.LookRotation(new Vector3(2, 3, 4)), testComponent.transform.rotation);
|
||||
|
||||
Assert.AreEqual(new Vector3(0, 1, 2), testComponent.AnticipatedState.Position);
|
||||
Assert.AreEqual(new Vector3(1, 2, 3), testComponent.AnticipatedState.Scale);
|
||||
Assert.AreEqual(Quaternion.LookRotation(new Vector3(2, 3, 4)), testComponent.AnticipatedState.Rotation);
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenAnticipating_AuthoritativeValueDoesNotChange()
|
||||
{
|
||||
var testComponent = GetTestComponent();
|
||||
|
||||
var startPosition = testComponent.transform.position;
|
||||
var startScale = testComponent.transform.localScale;
|
||||
var startRotation = testComponent.transform.rotation;
|
||||
|
||||
testComponent.AnticipateMove(new Vector3(0, 1, 2));
|
||||
testComponent.AnticipateScale(new Vector3(1, 2, 3));
|
||||
testComponent.AnticipateRotate(Quaternion.LookRotation(new Vector3(2, 3, 4)));
|
||||
|
||||
Assert.AreEqual(startPosition, testComponent.AuthoritativeState.Position);
|
||||
Assert.AreEqual(startScale, testComponent.AuthoritativeState.Scale);
|
||||
Assert.AreEqual(startRotation, testComponent.AuthoritativeState.Rotation);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenAnticipating_ServerDoesNotChange()
|
||||
{
|
||||
var testComponent = GetTestComponent();
|
||||
|
||||
var startPosition = testComponent.transform.position;
|
||||
var startScale = testComponent.transform.localScale;
|
||||
var startRotation = testComponent.transform.rotation;
|
||||
|
||||
testComponent.AnticipateMove(new Vector3(0, 1, 2));
|
||||
testComponent.AnticipateScale(new Vector3(1, 2, 3));
|
||||
testComponent.AnticipateRotate(Quaternion.LookRotation(new Vector3(2, 3, 4)));
|
||||
|
||||
var serverComponent = GetServerComponent();
|
||||
|
||||
Assert.AreEqual(startPosition, serverComponent.AuthoritativeState.Position);
|
||||
Assert.AreEqual(startScale, serverComponent.AuthoritativeState.Scale);
|
||||
Assert.AreEqual(startRotation, serverComponent.AuthoritativeState.Rotation);
|
||||
Assert.AreEqual(startPosition, serverComponent.AnticipatedState.Position);
|
||||
Assert.AreEqual(startScale, serverComponent.AnticipatedState.Scale);
|
||||
Assert.AreEqual(startRotation, serverComponent.AnticipatedState.Rotation);
|
||||
|
||||
TimeTravel(2, 120);
|
||||
|
||||
Assert.AreEqual(startPosition, serverComponent.AuthoritativeState.Position);
|
||||
Assert.AreEqual(startScale, serverComponent.AuthoritativeState.Scale);
|
||||
Assert.AreEqual(startRotation, serverComponent.AuthoritativeState.Rotation);
|
||||
Assert.AreEqual(startPosition, serverComponent.AnticipatedState.Position);
|
||||
Assert.AreEqual(startScale, serverComponent.AnticipatedState.Scale);
|
||||
Assert.AreEqual(startRotation, serverComponent.AnticipatedState.Rotation);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenAnticipating_OtherClientDoesNotChange()
|
||||
{
|
||||
var testComponent = GetTestComponent();
|
||||
|
||||
var startPosition = testComponent.transform.position;
|
||||
var startScale = testComponent.transform.localScale;
|
||||
var startRotation = testComponent.transform.rotation;
|
||||
|
||||
testComponent.AnticipateMove(new Vector3(0, 1, 2));
|
||||
testComponent.AnticipateScale(new Vector3(1, 2, 3));
|
||||
testComponent.AnticipateRotate(Quaternion.LookRotation(new Vector3(2, 3, 4)));
|
||||
|
||||
var otherClientComponent = GetOtherClientComponent();
|
||||
|
||||
Assert.AreEqual(startPosition, otherClientComponent.AuthoritativeState.Position);
|
||||
Assert.AreEqual(startScale, otherClientComponent.AuthoritativeState.Scale);
|
||||
Assert.AreEqual(startRotation, otherClientComponent.AuthoritativeState.Rotation);
|
||||
Assert.AreEqual(startPosition, otherClientComponent.AnticipatedState.Position);
|
||||
Assert.AreEqual(startScale, otherClientComponent.AnticipatedState.Scale);
|
||||
Assert.AreEqual(startRotation, otherClientComponent.AnticipatedState.Rotation);
|
||||
|
||||
TimeTravel(2, 120);
|
||||
|
||||
Assert.AreEqual(startPosition, otherClientComponent.AuthoritativeState.Position);
|
||||
Assert.AreEqual(startScale, otherClientComponent.AuthoritativeState.Scale);
|
||||
Assert.AreEqual(startRotation, otherClientComponent.AuthoritativeState.Rotation);
|
||||
Assert.AreEqual(startPosition, otherClientComponent.AnticipatedState.Position);
|
||||
Assert.AreEqual(startScale, otherClientComponent.AnticipatedState.Scale);
|
||||
Assert.AreEqual(startRotation, otherClientComponent.AnticipatedState.Rotation);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenServerChangesSnapValue_ValuesAreUpdated()
|
||||
{
|
||||
var testComponent = GetTestComponent();
|
||||
var serverComponent = GetServerComponent();
|
||||
serverComponent.Interpolate = false;
|
||||
|
||||
testComponent.AnticipateMove(new Vector3(0, 1, 2));
|
||||
testComponent.AnticipateScale(new Vector3(1, 2, 3));
|
||||
testComponent.AnticipateRotate(Quaternion.LookRotation(new Vector3(2, 3, 4)));
|
||||
|
||||
var rpcComponent = testComponent.GetComponent<NetworkTransformAnticipationComponent>();
|
||||
rpcComponent.MoveRpc(new Vector3(2, 3, 4));
|
||||
|
||||
WaitForMessageReceivedWithTimeTravel<RpcMessage>(new List<NetworkManager> { m_ServerNetworkManager });
|
||||
var otherClientComponent = GetOtherClientComponent();
|
||||
|
||||
WaitForConditionOrTimeOutWithTimeTravel(() => testComponent.AuthoritativeState.Position == serverComponent.transform.position && otherClientComponent.AuthoritativeState.Position == serverComponent.transform.position);
|
||||
|
||||
Assert.AreEqual(serverComponent.transform.position, testComponent.transform.position);
|
||||
Assert.AreEqual(serverComponent.transform.position, testComponent.AnticipatedState.Position);
|
||||
Assert.AreEqual(serverComponent.transform.position, testComponent.AuthoritativeState.Position);
|
||||
|
||||
Assert.AreEqual(serverComponent.transform.position, otherClientComponent.transform.position);
|
||||
Assert.AreEqual(serverComponent.transform.position, otherClientComponent.AnticipatedState.Position);
|
||||
Assert.AreEqual(serverComponent.transform.position, otherClientComponent.AuthoritativeState.Position);
|
||||
}
|
||||
|
||||
public void AssertQuaternionsAreEquivalent(Quaternion a, Quaternion b)
|
||||
{
|
||||
var aAngles = a.eulerAngles;
|
||||
var bAngles = b.eulerAngles;
|
||||
Assert.AreEqual(aAngles.x, bAngles.x, 0.001, $"Quaternions were not equal. Expected: {a}, but was {b}");
|
||||
Assert.AreEqual(aAngles.y, bAngles.y, 0.001, $"Quaternions were not equal. Expected: {a}, but was {b}");
|
||||
Assert.AreEqual(aAngles.z, bAngles.z, 0.001, $"Quaternions were not equal. Expected: {a}, but was {b}");
|
||||
}
|
||||
public void AssertVectorsAreEquivalent(Vector3 a, Vector3 b)
|
||||
{
|
||||
Assert.AreEqual(a.x, b.x, 0.001, $"Vectors were not equal. Expected: {a}, but was {b}");
|
||||
Assert.AreEqual(a.y, b.y, 0.001, $"Vectors were not equal. Expected: {a}, but was {b}");
|
||||
Assert.AreEqual(a.z, b.z, 0.001, $"Vectors were not equal. Expected: {a}, but was {b}");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenServerChangesSmoothValue_ValuesAreLerped()
|
||||
{
|
||||
var testComponent = GetTestComponent();
|
||||
var otherClientComponent = GetOtherClientComponent();
|
||||
|
||||
testComponent.StaleDataHandling = StaleDataHandling.Ignore;
|
||||
otherClientComponent.StaleDataHandling = StaleDataHandling.Ignore;
|
||||
|
||||
var serverComponent = GetServerComponent();
|
||||
serverComponent.Interpolate = false;
|
||||
|
||||
testComponent.GetComponent<NetworkTransformAnticipationComponent>().ShouldSmooth = true;
|
||||
otherClientComponent.GetComponent<NetworkTransformAnticipationComponent>().ShouldSmooth = true;
|
||||
|
||||
var startPosition = testComponent.transform.position;
|
||||
var startScale = testComponent.transform.localScale;
|
||||
var startRotation = testComponent.transform.rotation;
|
||||
var anticipePosition = new Vector3(0, 1, 2);
|
||||
var anticipeScale = new Vector3(1, 2, 3);
|
||||
var anticipeRotation = Quaternion.LookRotation(new Vector3(2, 3, 4));
|
||||
var serverSetPosition = new Vector3(3, 4, 5);
|
||||
var serverSetScale = new Vector3(4, 5, 6);
|
||||
var serverSetRotation = Quaternion.LookRotation(new Vector3(5, 6, 7));
|
||||
|
||||
testComponent.AnticipateMove(anticipePosition);
|
||||
testComponent.AnticipateScale(anticipeScale);
|
||||
testComponent.AnticipateRotate(anticipeRotation);
|
||||
|
||||
var rpcComponent = testComponent.GetComponent<NetworkTransformAnticipationComponent>();
|
||||
rpcComponent.MoveRpc(serverSetPosition);
|
||||
rpcComponent.RotateRpc(serverSetRotation);
|
||||
rpcComponent.ScaleRpc(serverSetScale);
|
||||
|
||||
WaitForMessagesReceivedWithTimeTravel(new List<Type>
|
||||
{
|
||||
typeof(RpcMessage),
|
||||
typeof(RpcMessage),
|
||||
typeof(RpcMessage),
|
||||
}, new List<NetworkManager> { m_ServerNetworkManager });
|
||||
|
||||
WaitForMessageReceivedWithTimeTravel<NetworkTransformMessage>(m_ClientNetworkManagers.ToList());
|
||||
|
||||
var percentChanged = 1f / 60f;
|
||||
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(anticipePosition, serverSetPosition, percentChanged), testComponent.transform.position);
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(anticipeScale, serverSetScale, percentChanged), testComponent.transform.localScale);
|
||||
AssertQuaternionsAreEquivalent(Quaternion.Slerp(anticipeRotation, serverSetRotation, percentChanged), testComponent.transform.rotation);
|
||||
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(anticipePosition, serverSetPosition, percentChanged), testComponent.AnticipatedState.Position);
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(anticipeScale, serverSetScale, percentChanged), testComponent.AnticipatedState.Scale);
|
||||
AssertQuaternionsAreEquivalent(Quaternion.Slerp(anticipeRotation, serverSetRotation, percentChanged), testComponent.AnticipatedState.Rotation);
|
||||
|
||||
AssertVectorsAreEquivalent(serverSetPosition, testComponent.AuthoritativeState.Position);
|
||||
AssertVectorsAreEquivalent(serverSetScale, testComponent.AuthoritativeState.Scale);
|
||||
AssertQuaternionsAreEquivalent(serverSetRotation, testComponent.AuthoritativeState.Rotation);
|
||||
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(startPosition, serverSetPosition, percentChanged), otherClientComponent.transform.position);
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(startScale, serverSetScale, percentChanged), otherClientComponent.transform.localScale);
|
||||
AssertQuaternionsAreEquivalent(Quaternion.Slerp(startRotation, serverSetRotation, percentChanged), otherClientComponent.transform.rotation);
|
||||
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(startPosition, serverSetPosition, percentChanged), otherClientComponent.AnticipatedState.Position);
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(startScale, serverSetScale, percentChanged), otherClientComponent.AnticipatedState.Scale);
|
||||
AssertQuaternionsAreEquivalent(Quaternion.Slerp(startRotation, serverSetRotation, percentChanged), otherClientComponent.AnticipatedState.Rotation);
|
||||
|
||||
AssertVectorsAreEquivalent(serverSetPosition, otherClientComponent.AuthoritativeState.Position);
|
||||
AssertVectorsAreEquivalent(serverSetScale, otherClientComponent.AuthoritativeState.Scale);
|
||||
AssertQuaternionsAreEquivalent(serverSetRotation, otherClientComponent.AuthoritativeState.Rotation);
|
||||
|
||||
for (var i = 1; i < 60; ++i)
|
||||
{
|
||||
TimeTravel(1f / 60f, 1);
|
||||
percentChanged = 1f / 60f * (i + 1);
|
||||
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(anticipePosition, serverSetPosition, percentChanged), testComponent.transform.position);
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(anticipeScale, serverSetScale, percentChanged), testComponent.transform.localScale);
|
||||
AssertQuaternionsAreEquivalent(Quaternion.Slerp(anticipeRotation, serverSetRotation, percentChanged), testComponent.transform.rotation);
|
||||
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(anticipePosition, serverSetPosition, percentChanged), testComponent.AnticipatedState.Position);
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(anticipeScale, serverSetScale, percentChanged), testComponent.AnticipatedState.Scale);
|
||||
AssertQuaternionsAreEquivalent(Quaternion.Slerp(anticipeRotation, serverSetRotation, percentChanged), testComponent.AnticipatedState.Rotation);
|
||||
|
||||
AssertVectorsAreEquivalent(serverSetPosition, testComponent.AuthoritativeState.Position);
|
||||
AssertVectorsAreEquivalent(serverSetScale, testComponent.AuthoritativeState.Scale);
|
||||
AssertQuaternionsAreEquivalent(serverSetRotation, testComponent.AuthoritativeState.Rotation);
|
||||
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(startPosition, serverSetPosition, percentChanged), otherClientComponent.transform.position);
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(startScale, serverSetScale, percentChanged), otherClientComponent.transform.localScale);
|
||||
AssertQuaternionsAreEquivalent(Quaternion.Slerp(startRotation, serverSetRotation, percentChanged), otherClientComponent.transform.rotation);
|
||||
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(startPosition, serverSetPosition, percentChanged), otherClientComponent.AnticipatedState.Position);
|
||||
AssertVectorsAreEquivalent(Vector3.Lerp(startScale, serverSetScale, percentChanged), otherClientComponent.AnticipatedState.Scale);
|
||||
AssertQuaternionsAreEquivalent(Quaternion.Slerp(startRotation, serverSetRotation, percentChanged), otherClientComponent.AnticipatedState.Rotation);
|
||||
|
||||
AssertVectorsAreEquivalent(serverSetPosition, otherClientComponent.AuthoritativeState.Position);
|
||||
AssertVectorsAreEquivalent(serverSetScale, otherClientComponent.AuthoritativeState.Scale);
|
||||
AssertQuaternionsAreEquivalent(serverSetRotation, otherClientComponent.AuthoritativeState.Rotation);
|
||||
}
|
||||
TimeTravel(1f / 60f, 1);
|
||||
|
||||
AssertVectorsAreEquivalent(serverSetPosition, testComponent.transform.position);
|
||||
AssertVectorsAreEquivalent(serverSetScale, testComponent.transform.localScale);
|
||||
AssertQuaternionsAreEquivalent(serverSetRotation, testComponent.transform.rotation);
|
||||
|
||||
AssertVectorsAreEquivalent(serverSetPosition, testComponent.AnticipatedState.Position);
|
||||
AssertVectorsAreEquivalent(serverSetScale, testComponent.AnticipatedState.Scale);
|
||||
AssertQuaternionsAreEquivalent(serverSetRotation, testComponent.AnticipatedState.Rotation);
|
||||
|
||||
AssertVectorsAreEquivalent(serverSetPosition, testComponent.AuthoritativeState.Position);
|
||||
AssertVectorsAreEquivalent(serverSetScale, testComponent.AuthoritativeState.Scale);
|
||||
AssertQuaternionsAreEquivalent(serverSetRotation, testComponent.AuthoritativeState.Rotation);
|
||||
|
||||
AssertVectorsAreEquivalent(serverSetPosition, otherClientComponent.transform.position);
|
||||
AssertVectorsAreEquivalent(serverSetScale, otherClientComponent.transform.localScale);
|
||||
AssertQuaternionsAreEquivalent(serverSetRotation, otherClientComponent.transform.rotation);
|
||||
|
||||
AssertVectorsAreEquivalent(serverSetPosition, otherClientComponent.AnticipatedState.Position);
|
||||
AssertVectorsAreEquivalent(serverSetScale, otherClientComponent.AnticipatedState.Scale);
|
||||
AssertQuaternionsAreEquivalent(serverSetRotation, otherClientComponent.AnticipatedState.Rotation);
|
||||
|
||||
AssertVectorsAreEquivalent(serverSetPosition, otherClientComponent.AuthoritativeState.Position);
|
||||
AssertVectorsAreEquivalent(serverSetScale, otherClientComponent.AuthoritativeState.Scale);
|
||||
AssertQuaternionsAreEquivalent(serverSetRotation, otherClientComponent.AuthoritativeState.Rotation);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenServerChangesReanticipeValue_ValuesAreReanticiped()
|
||||
{
|
||||
var testComponent = GetTestComponent();
|
||||
var otherClientComponent = GetOtherClientComponent();
|
||||
|
||||
testComponent.GetComponent<NetworkTransformAnticipationComponent>().ShouldMove = true;
|
||||
otherClientComponent.GetComponent<NetworkTransformAnticipationComponent>().ShouldMove = true;
|
||||
|
||||
var serverComponent = GetServerComponent();
|
||||
serverComponent.Interpolate = false;
|
||||
serverComponent.transform.position = new Vector3(0, 1, 2);
|
||||
var rpcComponent = testComponent.GetComponent<NetworkTransformAnticipationComponent>();
|
||||
rpcComponent.MoveRpc(new Vector3(0, 1, 2));
|
||||
|
||||
WaitForMessageReceivedWithTimeTravel<RpcMessage>(new List<NetworkManager> { m_ServerNetworkManager });
|
||||
|
||||
WaitForMessageReceivedWithTimeTravel<NetworkTransformMessage>(m_ClientNetworkManagers.ToList());
|
||||
|
||||
Assert.AreEqual(new Vector3(0, 6, 2), testComponent.transform.position);
|
||||
Assert.AreEqual(new Vector3(0, 6, 2), testComponent.AnticipatedState.Position);
|
||||
Assert.AreEqual(new Vector3(0, 1, 2), testComponent.AuthoritativeState.Position);
|
||||
|
||||
Assert.AreEqual(new Vector3(0, 6, 2), otherClientComponent.transform.position);
|
||||
Assert.AreEqual(new Vector3(0, 6, 2), otherClientComponent.AnticipatedState.Position);
|
||||
Assert.AreEqual(new Vector3(0, 1, 2), otherClientComponent.AuthoritativeState.Position);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenStaleDataArrivesToIgnoreVariable_ItIsIgnored([Values(10u, 30u, 60u)] uint tickRate, [Values(0u, 1u, 2u)] uint skipFrames)
|
||||
{
|
||||
m_ServerNetworkManager.NetworkConfig.TickRate = tickRate;
|
||||
m_ServerNetworkManager.NetworkTickSystem.TickRate = tickRate;
|
||||
|
||||
for (var i = 0; i < skipFrames; ++i)
|
||||
{
|
||||
TimeTravel(1 / 60f, 1);
|
||||
}
|
||||
|
||||
var serverComponent = GetServerComponent();
|
||||
serverComponent.Interpolate = false;
|
||||
|
||||
var testComponent = GetTestComponent();
|
||||
testComponent.StaleDataHandling = StaleDataHandling.Ignore;
|
||||
testComponent.Interpolate = false;
|
||||
|
||||
var otherClientComponent = GetOtherClientComponent();
|
||||
otherClientComponent.StaleDataHandling = StaleDataHandling.Ignore;
|
||||
otherClientComponent.Interpolate = false;
|
||||
|
||||
var rpcComponent = testComponent.GetComponent<NetworkTransformAnticipationComponent>();
|
||||
rpcComponent.MoveRpc(new Vector3(1, 2, 3));
|
||||
|
||||
WaitForMessageReceivedWithTimeTravel<RpcMessage>(new List<NetworkManager> { m_ServerNetworkManager });
|
||||
|
||||
testComponent.AnticipateMove(new Vector3(0, 5, 0));
|
||||
rpcComponent.MoveRpc(new Vector3(4, 5, 6));
|
||||
|
||||
// Depending on tick rate, one of these two things will happen.
|
||||
// The assertions are different based on this... either the tick rate is slow enough that the second RPC is received
|
||||
// before the next update and we move to 4, 5, 6, or the tick rate is fast enough that the next update is sent out
|
||||
// before the RPC is received and we get the update for the move to 1, 2, 3. Both are valid, what we want to assert
|
||||
// here is that the anticipated state never becomes 1, 2, 3.
|
||||
WaitForConditionOrTimeOutWithTimeTravel(() => testComponent.AuthoritativeState.Position == new Vector3(1, 2, 3) || testComponent.AuthoritativeState.Position == new Vector3(4, 5, 6));
|
||||
|
||||
if (testComponent.AnticipatedState.Position == new Vector3(4, 5, 6))
|
||||
{
|
||||
// Anticiped client received this data for a time earlier than its anticipation, and should have prioritized the anticiped value
|
||||
Assert.AreEqual(new Vector3(4, 5, 6), testComponent.transform.position);
|
||||
Assert.AreEqual(new Vector3(4, 5, 6), testComponent.AnticipatedState.Position);
|
||||
// However, the authoritative value still gets updated
|
||||
Assert.AreEqual(new Vector3(4, 5, 6), testComponent.AuthoritativeState.Position);
|
||||
|
||||
// Other client got the server value and had made no anticipation, so it applies it to the anticiped value as well.
|
||||
Assert.AreEqual(new Vector3(4, 5, 6), otherClientComponent.transform.position);
|
||||
Assert.AreEqual(new Vector3(4, 5, 6), otherClientComponent.AnticipatedState.Position);
|
||||
Assert.AreEqual(new Vector3(4, 5, 6), otherClientComponent.AuthoritativeState.Position);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Anticiped client received this data for a time earlier than its anticipation, and should have prioritized the anticiped value
|
||||
Assert.AreEqual(new Vector3(0, 5, 0), testComponent.transform.position);
|
||||
Assert.AreEqual(new Vector3(0, 5, 0), testComponent.AnticipatedState.Position);
|
||||
// However, the authoritative value still gets updated
|
||||
Assert.AreEqual(new Vector3(1, 2, 3), testComponent.AuthoritativeState.Position);
|
||||
|
||||
// Other client got the server value and had made no anticipation, so it applies it to the anticiped value as well.
|
||||
Assert.AreEqual(new Vector3(1, 2, 3), otherClientComponent.transform.position);
|
||||
Assert.AreEqual(new Vector3(1, 2, 3), otherClientComponent.AnticipatedState.Position);
|
||||
Assert.AreEqual(new Vector3(1, 2, 3), otherClientComponent.AuthoritativeState.Position);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void WhenNonStaleDataArrivesToIgnoreVariable_ItIsNotIgnored([Values(10u, 30u, 60u)] uint tickRate, [Values(0u, 1u, 2u)] uint skipFrames)
|
||||
{
|
||||
m_ServerNetworkManager.NetworkConfig.TickRate = tickRate;
|
||||
m_ServerNetworkManager.NetworkTickSystem.TickRate = tickRate;
|
||||
|
||||
for (var i = 0; i < skipFrames; ++i)
|
||||
{
|
||||
TimeTravel(1 / 60f, 1);
|
||||
}
|
||||
|
||||
var serverComponent = GetServerComponent();
|
||||
serverComponent.Interpolate = false;
|
||||
|
||||
var testComponent = GetTestComponent();
|
||||
testComponent.StaleDataHandling = StaleDataHandling.Ignore;
|
||||
testComponent.Interpolate = false;
|
||||
|
||||
var otherClientComponent = GetOtherClientComponent();
|
||||
otherClientComponent.StaleDataHandling = StaleDataHandling.Ignore;
|
||||
otherClientComponent.Interpolate = false;
|
||||
|
||||
testComponent.AnticipateMove(new Vector3(0, 5, 0));
|
||||
var rpcComponent = testComponent.GetComponent<NetworkTransformAnticipationComponent>();
|
||||
rpcComponent.MoveRpc(new Vector3(1, 2, 3));
|
||||
|
||||
WaitForMessageReceivedWithTimeTravel<RpcMessage>(new List<NetworkManager> { m_ServerNetworkManager });
|
||||
|
||||
WaitForConditionOrTimeOutWithTimeTravel(() => testComponent.AuthoritativeState.Position == serverComponent.transform.position && otherClientComponent.AuthoritativeState.Position == serverComponent.transform.position);
|
||||
|
||||
// Anticiped client received this data for a time earlier than its anticipation, and should have prioritized the anticiped value
|
||||
Assert.AreEqual(new Vector3(1, 2, 3), testComponent.transform.position);
|
||||
Assert.AreEqual(new Vector3(1, 2, 3), testComponent.AnticipatedState.Position);
|
||||
// However, the authoritative value still gets updated
|
||||
Assert.AreEqual(new Vector3(1, 2, 3), testComponent.AuthoritativeState.Position);
|
||||
|
||||
// Other client got the server value and had made no anticipation, so it applies it to the anticiped value as well.
|
||||
Assert.AreEqual(new Vector3(1, 2, 3), otherClientComponent.transform.position);
|
||||
Assert.AreEqual(new Vector3(1, 2, 3), otherClientComponent.AnticipatedState.Position);
|
||||
Assert.AreEqual(new Vector3(1, 2, 3), otherClientComponent.AuthoritativeState.Position);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Tests/Runtime/NetworkTransformAnticipationTests.cs.meta
Normal file
2
Tests/Runtime/NetworkTransformAnticipationTests.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ceb074b080c27184a9f669cd68355955
|
||||
420
Tests/Runtime/NetworkVariableAnticipationTests.cs
Normal file
420
Tests/Runtime/NetworkVariableAnticipationTests.cs
Normal file
@@ -0,0 +1,420 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using Unity.Netcode.TestHelpers.Runtime;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Unity.Netcode.RuntimeTests
|
||||
{
|
||||
internal class NetworkVariableAnticipationComponent : NetworkBehaviour
|
||||
{
|
||||
public AnticipatedNetworkVariable<int> SnapOnAnticipationFailVariable = new AnticipatedNetworkVariable<int>(0, StaleDataHandling.Ignore);
|
||||
public AnticipatedNetworkVariable<float> SmoothOnAnticipationFailVariable = new AnticipatedNetworkVariable<float>(0, StaleDataHandling.Reanticipate);
|
||||
public AnticipatedNetworkVariable<float> ReanticipateOnAnticipationFailVariable = new AnticipatedNetworkVariable<float>(0, StaleDataHandling.Reanticipate);
|
||||
|
||||
public override void OnReanticipate(double lastRoundTripTime)
|
||||
{
|
||||
if (SmoothOnAnticipationFailVariable.ShouldReanticipate)
|
||||
{
|
||||
if (Mathf.Abs(SmoothOnAnticipationFailVariable.AuthoritativeValue - SmoothOnAnticipationFailVariable.PreviousAnticipatedValue) > Mathf.Epsilon)
|
||||
{
|
||||
SmoothOnAnticipationFailVariable.Smooth(SmoothOnAnticipationFailVariable.PreviousAnticipatedValue, SmoothOnAnticipationFailVariable.AuthoritativeValue, 1, Mathf.Lerp);
|
||||
}
|
||||
}
|
||||
|
||||
if (ReanticipateOnAnticipationFailVariable.ShouldReanticipate)
|
||||
{
|
||||
// Would love to test some stuff about anticipation based on time, but that is difficult to test accurately.
|
||||
// This reanticipating variable will just always anticipate a value 5 higher than the server value.
|
||||
ReanticipateOnAnticipationFailVariable.Anticipate(ReanticipateOnAnticipationFailVariable.AuthoritativeValue + 5);
|
||||
}
|
||||
}
|
||||
|
||||
public bool SnapRpcResponseReceived = false;
|
||||
|
||||
[Rpc(SendTo.Server)]
|
||||
public void SetSnapValueRpc(int i, RpcParams rpcParams = default)
|
||||
{
|
||||
SnapOnAnticipationFailVariable.AuthoritativeValue = i;
|
||||
SetSnapValueResponseRpc(RpcTarget.Single(rpcParams.Receive.SenderClientId, RpcTargetUse.Temp));
|
||||
}
|
||||
|
||||
[Rpc(SendTo.SpecifiedInParams)]
|
||||
public void SetSnapValueResponseRpc(RpcParams rpcParams)
|
||||
{
|
||||
SnapRpcResponseReceived = true;
|
||||
}
|
||||
|
||||
[Rpc(SendTo.Server)]
|
||||
public void SetSmoothValueRpc(float f)
|
||||
{
|
||||
SmoothOnAnticipationFailVariable.AuthoritativeValue = f;
|
||||
}
|
||||
|
||||
[Rpc(SendTo.Server)]
|
||||
public void SetReanticipateValueRpc(float f)
|
||||
{
|
||||
ReanticipateOnAnticipationFailVariable.AuthoritativeValue = f;
|
||||
}
|
||||
}
|
||||
|
||||
internal class NetworkVariableAnticipationTests : 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<NetworkVariableAnticipationComponent>();
|
||||
}
|
||||
|
||||
public NetworkVariableAnticipationComponent GetTestComponent()
|
||||
{
|
||||
return m_ClientNetworkManagers[0].LocalClient.PlayerObject.GetComponent<NetworkVariableAnticipationComponent>();
|
||||
}
|
||||
|
||||
public NetworkVariableAnticipationComponent GetServerComponent()
|
||||
{
|
||||
foreach (var obj in Object.FindObjectsByType<NetworkVariableAnticipationComponent>(FindObjectsSortMode.None))
|
||||
{
|
||||
if (obj.NetworkManager == m_ServerNetworkManager && obj.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId)
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public NetworkVariableAnticipationComponent GetOtherClientComponent()
|
||||
{
|
||||
foreach (var obj in Object.FindObjectsByType<NetworkVariableAnticipationComponent>(FindObjectsSortMode.None))
|
||||
{
|
||||
if (obj.NetworkManager == m_ClientNetworkManagers[1] && obj.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId)
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenAnticipating_ValueChangesImmediately()
|
||||
{
|
||||
var testComponent = GetTestComponent();
|
||||
|
||||
testComponent.SnapOnAnticipationFailVariable.Anticipate(10);
|
||||
testComponent.SmoothOnAnticipationFailVariable.Anticipate(15);
|
||||
testComponent.ReanticipateOnAnticipationFailVariable.Anticipate(20);
|
||||
|
||||
Assert.AreEqual(10, testComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(15, testComponent.SmoothOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(20, testComponent.ReanticipateOnAnticipationFailVariable.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenAnticipating_AuthoritativeValueDoesNotChange()
|
||||
{
|
||||
var testComponent = GetTestComponent();
|
||||
|
||||
testComponent.SnapOnAnticipationFailVariable.Anticipate(10);
|
||||
testComponent.SmoothOnAnticipationFailVariable.Anticipate(15);
|
||||
testComponent.ReanticipateOnAnticipationFailVariable.Anticipate(20);
|
||||
|
||||
Assert.AreEqual(0, testComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
Assert.AreEqual(0, testComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue);
|
||||
Assert.AreEqual(0, testComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenAnticipating_ServerDoesNotChange()
|
||||
{
|
||||
var testComponent = GetTestComponent();
|
||||
|
||||
testComponent.SnapOnAnticipationFailVariable.Anticipate(10);
|
||||
testComponent.SmoothOnAnticipationFailVariable.Anticipate(15);
|
||||
testComponent.ReanticipateOnAnticipationFailVariable.Anticipate(20);
|
||||
|
||||
var serverComponent = GetServerComponent();
|
||||
|
||||
Assert.AreEqual(0, serverComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
Assert.AreEqual(0, serverComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue);
|
||||
Assert.AreEqual(0, serverComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue);
|
||||
Assert.AreEqual(0, serverComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(0, serverComponent.SmoothOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(0, serverComponent.ReanticipateOnAnticipationFailVariable.Value);
|
||||
|
||||
TimeTravel(2, 120);
|
||||
|
||||
Assert.AreEqual(0, serverComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
Assert.AreEqual(0, serverComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue);
|
||||
Assert.AreEqual(0, serverComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue);
|
||||
Assert.AreEqual(0, serverComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(0, serverComponent.SmoothOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(0, serverComponent.ReanticipateOnAnticipationFailVariable.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenAnticipating_OtherClientDoesNotChange()
|
||||
{
|
||||
var testComponent = GetTestComponent();
|
||||
|
||||
testComponent.SnapOnAnticipationFailVariable.Anticipate(10);
|
||||
testComponent.SmoothOnAnticipationFailVariable.Anticipate(15);
|
||||
testComponent.ReanticipateOnAnticipationFailVariable.Anticipate(20);
|
||||
|
||||
var otherClientComponent = GetOtherClientComponent();
|
||||
|
||||
Assert.AreEqual(0, otherClientComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
Assert.AreEqual(0, otherClientComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue);
|
||||
Assert.AreEqual(0, otherClientComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue);
|
||||
Assert.AreEqual(0, otherClientComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(0, otherClientComponent.SmoothOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(0, otherClientComponent.ReanticipateOnAnticipationFailVariable.Value);
|
||||
|
||||
TimeTravel(2, 120);
|
||||
|
||||
Assert.AreEqual(0, otherClientComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
Assert.AreEqual(0, otherClientComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue);
|
||||
Assert.AreEqual(0, otherClientComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue);
|
||||
Assert.AreEqual(0, otherClientComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(0, otherClientComponent.SmoothOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(0, otherClientComponent.ReanticipateOnAnticipationFailVariable.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenServerChangesSnapValue_ValuesAreUpdated()
|
||||
{
|
||||
var testComponent = GetTestComponent();
|
||||
|
||||
testComponent.SnapOnAnticipationFailVariable.Anticipate(10);
|
||||
|
||||
Assert.AreEqual(10, testComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(0, testComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
|
||||
testComponent.SetSnapValueRpc(10);
|
||||
|
||||
WaitForMessageReceivedWithTimeTravel<RpcMessage>(
|
||||
new List<NetworkManager> { m_ServerNetworkManager }
|
||||
);
|
||||
|
||||
var serverComponent = GetServerComponent();
|
||||
Assert.AreEqual(10, serverComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(10, serverComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
|
||||
var otherClientComponent = GetOtherClientComponent();
|
||||
Assert.AreEqual(0, otherClientComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(0, otherClientComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
|
||||
WaitForMessageReceivedWithTimeTravel<NetworkVariableDeltaMessage>(m_ClientNetworkManagers.ToList());
|
||||
|
||||
Assert.AreEqual(10, testComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(10, testComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
|
||||
Assert.AreEqual(10, otherClientComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(10, otherClientComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenServerChangesSmoothValue_ValuesAreLerped()
|
||||
{
|
||||
var testComponent = GetTestComponent();
|
||||
|
||||
testComponent.SmoothOnAnticipationFailVariable.Anticipate(15);
|
||||
|
||||
Assert.AreEqual(15, testComponent.SmoothOnAnticipationFailVariable.Value, Mathf.Epsilon);
|
||||
Assert.AreEqual(0, testComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon);
|
||||
|
||||
// Set to a different value to simulate a anticipation failure - will lerp between the anticipated value
|
||||
// and the actual one
|
||||
testComponent.SetSmoothValueRpc(20);
|
||||
|
||||
WaitForMessageReceivedWithTimeTravel<RpcMessage>(
|
||||
new List<NetworkManager> { m_ServerNetworkManager }
|
||||
);
|
||||
|
||||
var serverComponent = GetServerComponent();
|
||||
Assert.AreEqual(20, serverComponent.SmoothOnAnticipationFailVariable.Value, Mathf.Epsilon);
|
||||
Assert.AreEqual(20, serverComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon);
|
||||
|
||||
var otherClientComponent = GetOtherClientComponent();
|
||||
Assert.AreEqual(0, otherClientComponent.SmoothOnAnticipationFailVariable.Value, Mathf.Epsilon);
|
||||
Assert.AreEqual(0, otherClientComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon);
|
||||
|
||||
WaitForMessageReceivedWithTimeTravel<NetworkVariableDeltaMessage>(m_ClientNetworkManagers.ToList());
|
||||
|
||||
Assert.AreEqual(15 + 1f / 60f * 5, testComponent.SmoothOnAnticipationFailVariable.Value, Mathf.Epsilon);
|
||||
Assert.AreEqual(20, testComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon);
|
||||
|
||||
Assert.AreEqual(0 + 1f / 60f * 20, otherClientComponent.SmoothOnAnticipationFailVariable.Value, Mathf.Epsilon);
|
||||
Assert.AreEqual(20, otherClientComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon);
|
||||
|
||||
for (var i = 1; i < 60; ++i)
|
||||
{
|
||||
TimeTravel(1f / 60f, 1);
|
||||
|
||||
Assert.AreEqual(15 + 1f / 60f * 5 * (i + 1), testComponent.SmoothOnAnticipationFailVariable.Value, 0.00001);
|
||||
Assert.AreEqual(20, testComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon);
|
||||
|
||||
Assert.AreEqual(0 + 1f / 60f * 20 * (i + 1), otherClientComponent.SmoothOnAnticipationFailVariable.Value, 0.00001);
|
||||
Assert.AreEqual(20, otherClientComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon);
|
||||
}
|
||||
TimeTravel(1f / 60f, 1);
|
||||
Assert.AreEqual(20, testComponent.SmoothOnAnticipationFailVariable.Value, Mathf.Epsilon);
|
||||
Assert.AreEqual(20, otherClientComponent.SmoothOnAnticipationFailVariable.Value, Mathf.Epsilon);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenServerChangesReanticipateValue_ValuesAreReanticipated()
|
||||
{
|
||||
var testComponent = GetTestComponent();
|
||||
|
||||
testComponent.ReanticipateOnAnticipationFailVariable.Anticipate(15);
|
||||
|
||||
Assert.AreEqual(15, testComponent.ReanticipateOnAnticipationFailVariable.Value, Mathf.Epsilon);
|
||||
Assert.AreEqual(0, testComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon);
|
||||
|
||||
// Set to a different value to simulate a anticipation failure - will lerp between the anticipated value
|
||||
// and the actual one
|
||||
testComponent.SetReanticipateValueRpc(20);
|
||||
|
||||
WaitForMessageReceivedWithTimeTravel<RpcMessage>(
|
||||
new List<NetworkManager> { m_ServerNetworkManager }
|
||||
);
|
||||
|
||||
var serverComponent = GetServerComponent();
|
||||
Assert.AreEqual(20, serverComponent.ReanticipateOnAnticipationFailVariable.Value, Mathf.Epsilon);
|
||||
Assert.AreEqual(20, serverComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon);
|
||||
|
||||
var otherClientComponent = GetOtherClientComponent();
|
||||
Assert.AreEqual(0, otherClientComponent.ReanticipateOnAnticipationFailVariable.Value, Mathf.Epsilon);
|
||||
Assert.AreEqual(0, otherClientComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon);
|
||||
|
||||
WaitForMessageReceivedWithTimeTravel<NetworkVariableDeltaMessage>(m_ClientNetworkManagers.ToList());
|
||||
|
||||
Assert.AreEqual(25, testComponent.ReanticipateOnAnticipationFailVariable.Value, Mathf.Epsilon);
|
||||
Assert.AreEqual(20, testComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon);
|
||||
|
||||
Assert.AreEqual(25, otherClientComponent.ReanticipateOnAnticipationFailVariable.Value, Mathf.Epsilon);
|
||||
Assert.AreEqual(20, otherClientComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenNonStaleDataArrivesToIgnoreVariable_ItIsNotIgnored([Values(10u, 30u, 60u)] uint tickRate, [Values(0u, 1u, 2u)] uint skipFrames)
|
||||
{
|
||||
m_ServerNetworkManager.NetworkConfig.TickRate = tickRate;
|
||||
m_ServerNetworkManager.NetworkTickSystem.TickRate = tickRate;
|
||||
|
||||
for (var i = 0; i < skipFrames; ++i)
|
||||
{
|
||||
TimeTravel(1 / 60f, 1);
|
||||
}
|
||||
var testComponent = GetTestComponent();
|
||||
testComponent.SnapOnAnticipationFailVariable.Anticipate(10);
|
||||
|
||||
Assert.AreEqual(10, testComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(0, testComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
testComponent.SetSnapValueRpc(20);
|
||||
WaitForMessageReceivedWithTimeTravel<RpcMessage>(new List<NetworkManager> { m_ServerNetworkManager });
|
||||
|
||||
var serverComponent = GetServerComponent();
|
||||
|
||||
Assert.AreEqual(20, serverComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(20, serverComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
|
||||
WaitForMessageReceivedWithTimeTravel<NetworkVariableDeltaMessage>(m_ClientNetworkManagers.ToList());
|
||||
|
||||
// Both values get updated
|
||||
Assert.AreEqual(20, testComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(20, testComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
|
||||
// Other client got the server value and had made no anticipation, so it applies it to the anticipated value as well.
|
||||
var otherClientComponent = GetOtherClientComponent();
|
||||
Assert.AreEqual(20, otherClientComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(20, otherClientComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenStaleDataArrivesToIgnoreVariable_ItIsIgnored([Values(10u, 30u, 60u)] uint tickRate, [Values(0u, 1u, 2u)] uint skipFrames)
|
||||
{
|
||||
m_ServerNetworkManager.NetworkConfig.TickRate = tickRate;
|
||||
m_ServerNetworkManager.NetworkTickSystem.TickRate = tickRate;
|
||||
|
||||
for (var i = 0; i < skipFrames; ++i)
|
||||
{
|
||||
TimeTravel(1 / 60f, 1);
|
||||
}
|
||||
var testComponent = GetTestComponent();
|
||||
testComponent.SnapOnAnticipationFailVariable.Anticipate(10);
|
||||
|
||||
Assert.AreEqual(10, testComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(0, testComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
|
||||
testComponent.SetSnapValueRpc(30);
|
||||
|
||||
var serverComponent = GetServerComponent();
|
||||
serverComponent.SnapOnAnticipationFailVariable.AuthoritativeValue = 20;
|
||||
|
||||
Assert.AreEqual(20, serverComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(20, serverComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
|
||||
WaitForMessageReceivedWithTimeTravel<NetworkVariableDeltaMessage>(m_ClientNetworkManagers.ToList());
|
||||
|
||||
if (testComponent.SnapRpcResponseReceived)
|
||||
{
|
||||
// In this case the tick rate is slow enough that the RPC was received and processed, so we check that.
|
||||
Assert.AreEqual(30, testComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(30, testComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
|
||||
var otherClientComponent = GetOtherClientComponent();
|
||||
Assert.AreEqual(30, otherClientComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(30, otherClientComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
// In this case, we got an update before the RPC was processed, so we should have ignored it.
|
||||
// Anticipated client received this data for a tick earlier than its anticipation, and should have prioritized the anticipated value
|
||||
Assert.AreEqual(10, testComponent.SnapOnAnticipationFailVariable.Value);
|
||||
// However, the authoritative value still gets updated
|
||||
Assert.AreEqual(20, testComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
|
||||
// Other client got the server value and had made no anticipation, so it applies it to the anticipated value as well.
|
||||
var otherClientComponent = GetOtherClientComponent();
|
||||
Assert.AreEqual(20, otherClientComponent.SnapOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(20, otherClientComponent.SnapOnAnticipationFailVariable.AuthoritativeValue);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenStaleDataArrivesToReanticipatedVariable_ItIsAppliedAndReanticipated()
|
||||
{
|
||||
var testComponent = GetTestComponent();
|
||||
testComponent.ReanticipateOnAnticipationFailVariable.Anticipate(10);
|
||||
|
||||
Assert.AreEqual(10, testComponent.ReanticipateOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(0, testComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue);
|
||||
|
||||
var serverComponent = GetServerComponent();
|
||||
serverComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue = 20;
|
||||
|
||||
Assert.AreEqual(20, serverComponent.ReanticipateOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(20, serverComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue);
|
||||
|
||||
WaitForMessageReceivedWithTimeTravel<NetworkVariableDeltaMessage>(m_ClientNetworkManagers.ToList());
|
||||
|
||||
Assert.AreEqual(25, testComponent.ReanticipateOnAnticipationFailVariable.Value);
|
||||
// However, the authoritative value still gets updated
|
||||
Assert.AreEqual(20, testComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue);
|
||||
|
||||
// Other client got the server value and had made no anticipation, so it applies it to the anticipated value as well.
|
||||
var otherClientComponent = GetOtherClientComponent();
|
||||
Assert.AreEqual(25, otherClientComponent.ReanticipateOnAnticipationFailVariable.Value);
|
||||
Assert.AreEqual(20, otherClientComponent.ReanticipateOnAnticipationFailVariable.AuthoritativeValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Tests/Runtime/NetworkVariableAnticipationTests.cs.meta
Normal file
2
Tests/Runtime/NetworkVariableAnticipationTests.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 74e627a9d18dcd04e9c56ab2539a6593
|
||||
@@ -386,9 +386,12 @@ namespace Unity.Netcode.RuntimeTests
|
||||
|
||||
// Used just to create a NetworkVariable in the templated NetworkBehaviour type that isn't referenced anywhere else
|
||||
// Please do not reference this class anywhere else!
|
||||
internal class TestClass_ReferencedOnlyByTemplateNetworkBehavourType : TestClass
|
||||
internal class TestClass_ReferencedOnlyByTemplateNetworkBehaviourType : TestClass, IEquatable<TestClass_ReferencedOnlyByTemplateNetworkBehaviourType>
|
||||
{
|
||||
|
||||
public bool Equals(TestClass_ReferencedOnlyByTemplateNetworkBehaviourType other)
|
||||
{
|
||||
return Equals((TestClass)other);
|
||||
}
|
||||
}
|
||||
|
||||
internal class NetworkVariableTest : NetworkBehaviour
|
||||
@@ -921,7 +924,7 @@ namespace Unity.Netcode.RuntimeTests
|
||||
m_Player1OnClient1.GetComponent<ClassHavingNetworkBehaviour2>().TheVar.Value.SomeInt == m_Player1OnServer.GetComponent<ClassHavingNetworkBehaviour2>().TheVar.Value.SomeInt;
|
||||
}
|
||||
|
||||
m_Player1OnServer.GetComponent<ClassHavingNetworkBehaviour2>().TheVar.Value = new TestClass_ReferencedOnlyByTemplateNetworkBehavourType { SomeInt = k_TestUInt, SomeBool = false };
|
||||
m_Player1OnServer.GetComponent<ClassHavingNetworkBehaviour2>().TheVar.Value = new TestClass_ReferencedOnlyByTemplateNetworkBehaviourType { SomeInt = k_TestUInt, SomeBool = false };
|
||||
m_Player1OnServer.GetComponent<ClassHavingNetworkBehaviour2>().TheVar.SetDirty(true);
|
||||
|
||||
// Wait for the client-side to notify it is finished initializing and spawning.
|
||||
|
||||
@@ -903,7 +903,7 @@ namespace Unity.Netcode.RuntimeTests
|
||||
}
|
||||
|
||||
// Please do not reference TestClass_ReferencedOnlyByTemplateNetworkBehavourType anywhere other than here!
|
||||
internal class ClassHavingNetworkBehaviour2 : TemplateNetworkBehaviourType<TestClass_ReferencedOnlyByTemplateNetworkBehavourType>
|
||||
internal class ClassHavingNetworkBehaviour2 : TemplateNetworkBehaviourType<TestClass_ReferencedOnlyByTemplateNetworkBehaviourType>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
138
Tests/Runtime/NetworkVariableTraitsTests.cs
Normal file
138
Tests/Runtime/NetworkVariableTraitsTests.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
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); ;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Tests/Runtime/NetworkVariableTraitsTests.cs.meta
Normal file
2
Tests/Runtime/NetworkVariableTraitsTests.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 49f46ca2b4327464498a7465891647bb
|
||||
@@ -112,6 +112,16 @@ namespace Unity.Netcode.RuntimeTests
|
||||
protected override IEnumerator OnSetup()
|
||||
{
|
||||
WorkingUserNetworkVariableComponentBase.Reset();
|
||||
|
||||
UserNetworkVariableSerialization<MyTypeOne>.WriteValue = null;
|
||||
UserNetworkVariableSerialization<MyTypeOne>.ReadValue = null;
|
||||
UserNetworkVariableSerialization<MyTypeOne>.DuplicateValue = null;
|
||||
UserNetworkVariableSerialization<MyTypeTwo>.WriteValue = null;
|
||||
UserNetworkVariableSerialization<MyTypeTwo>.ReadValue = null;
|
||||
UserNetworkVariableSerialization<MyTypeTwo>.DuplicateValue = null;
|
||||
UserNetworkVariableSerialization<MyTypeThree>.WriteValue = null;
|
||||
UserNetworkVariableSerialization<MyTypeThree>.ReadValue = null;
|
||||
UserNetworkVariableSerialization<MyTypeThree>.DuplicateValue = null;
|
||||
return base.OnSetup();
|
||||
}
|
||||
|
||||
@@ -217,5 +227,37 @@ namespace Unity.Netcode.RuntimeTests
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected override IEnumerator OnTearDown()
|
||||
{
|
||||
// These have to get set to SOMETHING, otherwise we will get an exception thrown because Object.Destroy()
|
||||
// calls __initializeNetworkVariables, and the network variable initialization attempts to call FallbackSerializer<T>,
|
||||
// which throws an exception if any of these values are null. They don't have to DO anything, they just have to
|
||||
// be non-null to keep the test from failing during teardown.
|
||||
// None of this is related to what's being tested above, and in reality, these values being null is an invalid
|
||||
// use case. But one of the tests is explicitly testing that invalid use case, and the values are being set
|
||||
// to null in OnSetup to ensure test isolation. This wouldn't be a situation a user would have to think about
|
||||
// in a real world use case.
|
||||
UserNetworkVariableSerialization<MyTypeOne>.WriteValue = (FastBufferWriter writer, in MyTypeOne value) => { };
|
||||
UserNetworkVariableSerialization<MyTypeOne>.ReadValue = (FastBufferReader reader, out MyTypeOne value) => { value = new MyTypeOne(); };
|
||||
UserNetworkVariableSerialization<MyTypeOne>.DuplicateValue = (in MyTypeOne value, ref MyTypeOne duplicatedValue) =>
|
||||
{
|
||||
duplicatedValue = value;
|
||||
};
|
||||
UserNetworkVariableSerialization<MyTypeTwo>.WriteValue = (FastBufferWriter writer, in MyTypeTwo value) => { };
|
||||
UserNetworkVariableSerialization<MyTypeTwo>.ReadValue = (FastBufferReader reader, out MyTypeTwo value) => { value = new MyTypeTwo(); };
|
||||
UserNetworkVariableSerialization<MyTypeTwo>.DuplicateValue = (in MyTypeTwo value, ref MyTypeTwo duplicatedValue) =>
|
||||
{
|
||||
duplicatedValue = value;
|
||||
};
|
||||
UserNetworkVariableSerialization<MyTypeThree>.WriteValue = (FastBufferWriter writer, in MyTypeThree value) => { };
|
||||
UserNetworkVariableSerialization<MyTypeThree>.ReadValue = (FastBufferReader reader, out MyTypeThree value) => { value = new MyTypeThree(); };
|
||||
UserNetworkVariableSerialization<MyTypeThree>.DuplicateValue = (in MyTypeThree value, ref MyTypeThree duplicatedValue) =>
|
||||
{
|
||||
duplicatedValue = value;
|
||||
};
|
||||
return base.OnTearDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ namespace Unity.Netcode.RuntimeTests
|
||||
{
|
||||
private class TestNetworkBehaviour : NetworkBehaviour
|
||||
{
|
||||
public static bool ReceivedRPC;
|
||||
|
||||
public NetworkVariable<NetworkBehaviourReference> TestVariable = new NetworkVariable<NetworkBehaviourReference>();
|
||||
|
||||
public TestNetworkBehaviour RpcReceivedBehaviour;
|
||||
@@ -25,6 +27,7 @@ namespace Unity.Netcode.RuntimeTests
|
||||
public void SendReferenceServerRpc(NetworkBehaviourReference value)
|
||||
{
|
||||
RpcReceivedBehaviour = (TestNetworkBehaviour)value;
|
||||
ReceivedRPC = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,8 +60,43 @@ namespace Unity.Netcode.RuntimeTests
|
||||
Assert.AreEqual(testNetworkBehaviour, testNetworkBehaviour.RpcReceivedBehaviour);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator TestSerializeNull([Values] bool initializeWithNull)
|
||||
{
|
||||
TestNetworkBehaviour.ReceivedRPC = false;
|
||||
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
|
||||
var testNetworkBehaviour = networkObjectContext.Object.gameObject.AddComponent<TestNetworkBehaviour>();
|
||||
networkObjectContext.Object.Spawn();
|
||||
|
||||
using var otherObjectContext = UnityObjectContext.CreateNetworkObject();
|
||||
otherObjectContext.Object.Spawn();
|
||||
|
||||
// If not initializing with null, then use the default constructor with no assigned NetworkBehaviour
|
||||
if (!initializeWithNull)
|
||||
{
|
||||
testNetworkBehaviour.SendReferenceServerRpc(new NetworkBehaviourReference());
|
||||
}
|
||||
else // Otherwise, initialize and pass in null as the reference
|
||||
{
|
||||
testNetworkBehaviour.SendReferenceServerRpc(new NetworkBehaviourReference(null));
|
||||
}
|
||||
|
||||
// wait for rpc completion
|
||||
float t = 0;
|
||||
while (!TestNetworkBehaviour.ReceivedRPC)
|
||||
{
|
||||
t += Time.deltaTime;
|
||||
if (t > 5f)
|
||||
{
|
||||
new AssertionException("RPC with NetworkBehaviour reference hasn't been received");
|
||||
}
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// validate
|
||||
Assert.AreEqual(null, testNetworkBehaviour.RpcReceivedBehaviour);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator TestRpcImplicitNetworkBehaviour()
|
||||
@@ -131,15 +169,6 @@ namespace Unity.Netcode.RuntimeTests
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FailSerializeNullBehaviour()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() =>
|
||||
{
|
||||
NetworkBehaviourReference outReference = null;
|
||||
});
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
//Stop, shutdown, and destroy
|
||||
|
||||
@@ -19,6 +19,8 @@ namespace Unity.Netcode.RuntimeTests
|
||||
{
|
||||
private class TestNetworkBehaviour : NetworkBehaviour
|
||||
{
|
||||
public static bool ReceivedRPC;
|
||||
|
||||
public NetworkVariable<NetworkObjectReference> TestVariable = new NetworkVariable<NetworkObjectReference>();
|
||||
|
||||
public NetworkObject RpcReceivedNetworkObject;
|
||||
@@ -28,6 +30,7 @@ namespace Unity.Netcode.RuntimeTests
|
||||
[ServerRpc]
|
||||
public void SendReferenceServerRpc(NetworkObjectReference value)
|
||||
{
|
||||
ReceivedRPC = true;
|
||||
RpcReceivedGameObject = value;
|
||||
RpcReceivedNetworkObject = value;
|
||||
}
|
||||
@@ -150,6 +153,60 @@ namespace Unity.Netcode.RuntimeTests
|
||||
Assert.AreEqual(networkObject, result);
|
||||
}
|
||||
|
||||
public enum NetworkObjectConstructorTypes
|
||||
{
|
||||
None,
|
||||
NullNetworkObject,
|
||||
NullGameObject
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator TestSerializeNull([Values] NetworkObjectConstructorTypes networkObjectConstructorTypes)
|
||||
{
|
||||
TestNetworkBehaviour.ReceivedRPC = false;
|
||||
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
|
||||
var testNetworkBehaviour = networkObjectContext.Object.gameObject.AddComponent<TestNetworkBehaviour>();
|
||||
networkObjectContext.Object.Spawn();
|
||||
|
||||
switch (networkObjectConstructorTypes)
|
||||
{
|
||||
case NetworkObjectConstructorTypes.None:
|
||||
{
|
||||
testNetworkBehaviour.SendReferenceServerRpc(new NetworkObjectReference());
|
||||
break;
|
||||
}
|
||||
case NetworkObjectConstructorTypes.NullNetworkObject:
|
||||
{
|
||||
testNetworkBehaviour.SendReferenceServerRpc(new NetworkObjectReference((NetworkObject)null));
|
||||
break;
|
||||
}
|
||||
case NetworkObjectConstructorTypes.NullGameObject:
|
||||
{
|
||||
testNetworkBehaviour.SendReferenceServerRpc(new NetworkObjectReference((GameObject)null));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// wait for rpc completion
|
||||
float t = 0;
|
||||
while (!TestNetworkBehaviour.ReceivedRPC)
|
||||
{
|
||||
|
||||
t += Time.deltaTime;
|
||||
if (t > 5f)
|
||||
{
|
||||
new AssertionException("RPC with NetworkBehaviour reference hasn't been received");
|
||||
}
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// validate
|
||||
Assert.AreEqual(null, testNetworkBehaviour.RpcReceivedNetworkObject);
|
||||
Assert.AreEqual(null, testNetworkBehaviour.RpcReceivedGameObject);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator TestRpc()
|
||||
{
|
||||
@@ -305,24 +362,6 @@ namespace Unity.Netcode.RuntimeTests
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FailSerializeNullNetworkObject()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() =>
|
||||
{
|
||||
NetworkObjectReference outReference = (NetworkObject)null;
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FailSerializeNullGameObject()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() =>
|
||||
{
|
||||
NetworkObjectReference outReference = (GameObject)null;
|
||||
});
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
//Stop, shutdown, and destroy
|
||||
|
||||
Reference in New Issue
Block a user