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)
423 lines
16 KiB
C#
423 lines
16 KiB
C#
using System;
|
|
using System.Collections;
|
|
using NUnit.Framework;
|
|
using Unity.Collections;
|
|
using Unity.Netcode.TestHelpers.Runtime;
|
|
using UnityEngine;
|
|
using UnityEngine.TestTools;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace Unity.Netcode.RuntimeTests
|
|
{
|
|
/// <summary>
|
|
/// Unit tests to test:
|
|
/// - Serializing NetworkObject to NetworkObjectReference
|
|
/// - Deserializing NetworkObjectReference to NetworkObject
|
|
/// - Implicit operators of NetworkObjectReference
|
|
/// </summary>
|
|
internal class NetworkObjectReferenceTests : IDisposable
|
|
{
|
|
private class TestNetworkBehaviour : NetworkBehaviour
|
|
{
|
|
public static bool ReceivedRPC;
|
|
|
|
public NetworkVariable<NetworkObjectReference> TestVariable = new NetworkVariable<NetworkObjectReference>();
|
|
|
|
public NetworkObject RpcReceivedNetworkObject;
|
|
|
|
public GameObject RpcReceivedGameObject;
|
|
|
|
[ServerRpc]
|
|
public void SendReferenceServerRpc(NetworkObjectReference value)
|
|
{
|
|
ReceivedRPC = true;
|
|
RpcReceivedGameObject = value;
|
|
RpcReceivedNetworkObject = value;
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestSerializeNetworkObject()
|
|
{
|
|
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
|
|
networkObjectContext.Object.Spawn();
|
|
var outWriter = new FastBufferWriter(1300, Allocator.Temp);
|
|
try
|
|
{
|
|
// serialize
|
|
var outSerializer = new BufferSerializer<BufferSerializerWriter>(new BufferSerializerWriter(outWriter));
|
|
NetworkObjectReference outReference = networkObjectContext.Object;
|
|
outReference.NetworkSerialize(outSerializer);
|
|
|
|
// deserialize
|
|
NetworkObjectReference inReference = default;
|
|
var inReader = new FastBufferReader(outWriter, Allocator.Temp);
|
|
try
|
|
{
|
|
var inSerializer =
|
|
new BufferSerializer<BufferSerializerReader>(new BufferSerializerReader(inReader));
|
|
inReference.NetworkSerialize(inSerializer);
|
|
}
|
|
finally
|
|
{
|
|
inReader.Dispose();
|
|
}
|
|
|
|
// validate
|
|
Assert.NotNull((NetworkObject)inReference);
|
|
Assert.AreEqual(inReference.NetworkObjectId, networkObjectContext.Object.NetworkObjectId);
|
|
Assert.AreEqual(outReference, inReference);
|
|
Assert.AreEqual(networkObjectContext.Object, (NetworkObject)inReference);
|
|
}
|
|
finally
|
|
{
|
|
outWriter.Dispose();
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestSerializeGameObject()
|
|
{
|
|
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
|
|
networkObjectContext.Object.Spawn();
|
|
var outWriter = new FastBufferWriter(1300, Allocator.Temp);
|
|
try
|
|
{
|
|
// serialize
|
|
var outSerializer = new BufferSerializer<BufferSerializerWriter>(new BufferSerializerWriter(outWriter));
|
|
NetworkObjectReference outReference = networkObjectContext.Object.gameObject;
|
|
outReference.NetworkSerialize(outSerializer);
|
|
|
|
// deserialize
|
|
NetworkObjectReference inReference = default;
|
|
var inReader = new FastBufferReader(outWriter, Allocator.Temp);
|
|
try
|
|
{
|
|
var inSerializer =
|
|
new BufferSerializer<BufferSerializerReader>(new BufferSerializerReader(inReader));
|
|
inReference.NetworkSerialize(inSerializer);
|
|
}
|
|
finally
|
|
{
|
|
inReader.Dispose();
|
|
}
|
|
GameObject gameObject = inReference;
|
|
|
|
// validate
|
|
Assert.AreEqual(outReference, inReference);
|
|
Assert.AreEqual(networkObjectContext.Object.gameObject, gameObject);
|
|
}
|
|
finally
|
|
{
|
|
outWriter.Dispose();
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestImplicitConversionToGameObject()
|
|
{
|
|
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
|
|
networkObjectContext.Object.Spawn();
|
|
|
|
NetworkObjectReference outReference = networkObjectContext.Object.gameObject;
|
|
|
|
GameObject go = outReference;
|
|
Assert.AreEqual(networkObjectContext.Object.gameObject, go);
|
|
}
|
|
|
|
[Test]
|
|
public void TestImplicitToGameObjectIsNullWhenNotFound()
|
|
{
|
|
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
|
|
networkObjectContext.Object.Spawn();
|
|
|
|
NetworkObjectReference outReference = networkObjectContext.Object.gameObject;
|
|
|
|
networkObjectContext.Object.Despawn();
|
|
Object.DestroyImmediate(networkObjectContext.Object.gameObject);
|
|
|
|
GameObject go = outReference;
|
|
Assert.IsNull(go);
|
|
}
|
|
[Test]
|
|
public void TestTryGet()
|
|
{
|
|
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
|
|
networkObjectContext.Object.Spawn();
|
|
|
|
NetworkObjectReference networkObjectReference = networkObjectContext.Object;
|
|
|
|
Assert.True(networkObjectReference.TryGet(out NetworkObject networkObject));
|
|
Assert.NotNull(networkObject);
|
|
networkObjectReference.TryGet(out NetworkObject result);
|
|
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()
|
|
{
|
|
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
|
|
var testNetworkBehaviour = networkObjectContext.Object.gameObject.AddComponent<TestNetworkBehaviour>();
|
|
networkObjectContext.Object.Spawn();
|
|
|
|
using var otherObjectContext = UnityObjectContext.CreateNetworkObject();
|
|
otherObjectContext.Object.Spawn();
|
|
|
|
testNetworkBehaviour.SendReferenceServerRpc(new NetworkObjectReference(otherObjectContext.Object));
|
|
|
|
// wait for rpc completion
|
|
float t = 0;
|
|
while (testNetworkBehaviour.RpcReceivedGameObject == null)
|
|
{
|
|
t += Time.deltaTime;
|
|
if (t > 5f)
|
|
{
|
|
new AssertionException("RPC with NetworkBehaviour reference hasn't been received");
|
|
}
|
|
|
|
yield return null;
|
|
}
|
|
|
|
// validate
|
|
Assert.AreEqual(otherObjectContext.Object, testNetworkBehaviour.RpcReceivedNetworkObject);
|
|
Assert.AreEqual(otherObjectContext.Object.gameObject, testNetworkBehaviour.RpcReceivedGameObject);
|
|
}
|
|
|
|
[UnityTest]
|
|
public IEnumerator TestRpcImplicitNetworkObject()
|
|
{
|
|
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
|
|
var testNetworkBehaviour = networkObjectContext.Object.gameObject.AddComponent<TestNetworkBehaviour>();
|
|
networkObjectContext.Object.Spawn();
|
|
|
|
using var otherObjectContext = UnityObjectContext.CreateNetworkObject();
|
|
otherObjectContext.Object.Spawn();
|
|
|
|
testNetworkBehaviour.SendReferenceServerRpc(otherObjectContext.Object);
|
|
|
|
// wait for rpc completion
|
|
float t = 0;
|
|
while (testNetworkBehaviour.RpcReceivedGameObject == null)
|
|
{
|
|
t += Time.deltaTime;
|
|
if (t > 5f)
|
|
{
|
|
new AssertionException("RPC with NetworkBehaviour reference hasn't been received");
|
|
}
|
|
|
|
yield return null;
|
|
}
|
|
|
|
// validate
|
|
Assert.AreEqual(otherObjectContext.Object, testNetworkBehaviour.RpcReceivedNetworkObject);
|
|
Assert.AreEqual(otherObjectContext.Object.gameObject, testNetworkBehaviour.RpcReceivedGameObject);
|
|
}
|
|
|
|
[UnityTest]
|
|
public IEnumerator TestRpcImplicitGameObject()
|
|
{
|
|
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
|
|
var testNetworkBehaviour = networkObjectContext.Object.gameObject.AddComponent<TestNetworkBehaviour>();
|
|
networkObjectContext.Object.Spawn();
|
|
|
|
using var otherObjectContext = UnityObjectContext.CreateNetworkObject();
|
|
otherObjectContext.Object.Spawn();
|
|
|
|
testNetworkBehaviour.SendReferenceServerRpc(otherObjectContext.Object.gameObject);
|
|
|
|
// wait for rpc completion
|
|
float t = 0;
|
|
while (testNetworkBehaviour.RpcReceivedGameObject == null)
|
|
{
|
|
t += Time.deltaTime;
|
|
if (t > 5f)
|
|
{
|
|
new AssertionException("RPC with NetworkBehaviour reference hasn't been received");
|
|
}
|
|
|
|
yield return null;
|
|
}
|
|
|
|
// validate
|
|
Assert.AreEqual(otherObjectContext.Object, testNetworkBehaviour.RpcReceivedNetworkObject);
|
|
Assert.AreEqual(otherObjectContext.Object.gameObject, testNetworkBehaviour.RpcReceivedGameObject);
|
|
}
|
|
|
|
[Test]
|
|
public void TestNetworkVariable()
|
|
{
|
|
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
|
|
var testNetworkBehaviour = networkObjectContext.Object.gameObject.AddComponent<TestNetworkBehaviour>();
|
|
networkObjectContext.Object.Spawn();
|
|
|
|
using var otherObjectContext = UnityObjectContext.CreateNetworkObject();
|
|
otherObjectContext.Object.Spawn();
|
|
|
|
// check default value is null
|
|
Assert.IsNull((NetworkObject)testNetworkBehaviour.TestVariable.Value);
|
|
|
|
testNetworkBehaviour.TestVariable.Value = networkObjectContext.Object;
|
|
|
|
Assert.AreEqual((GameObject)testNetworkBehaviour.TestVariable.Value, networkObjectContext.Object.gameObject);
|
|
Assert.AreEqual((NetworkObject)testNetworkBehaviour.TestVariable.Value, networkObjectContext.Object);
|
|
}
|
|
|
|
[Test]
|
|
public void TestDespawn()
|
|
{
|
|
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
|
|
networkObjectContext.Object.Spawn();
|
|
var originalId = networkObjectContext.Object.NetworkObjectId;
|
|
|
|
NetworkObjectReference networkObjectReference = networkObjectContext.Object;
|
|
Assert.AreEqual(networkObjectContext.Object, (NetworkObject)networkObjectReference);
|
|
|
|
networkObjectContext.Object.Despawn();
|
|
Assert.IsFalse(networkObjectReference.TryGet(out NetworkObject _));
|
|
|
|
networkObjectContext.Object.Spawn();
|
|
|
|
// After spawning again the reference will still no longer work as it still points to the old object
|
|
Assert.AreNotEqual(originalId, networkObjectContext.Object.NetworkObjectId);
|
|
Assert.IsFalse(networkObjectReference.TryGet(out NetworkObject _));
|
|
|
|
// creating a new reference will make it work again
|
|
networkObjectReference = networkObjectContext.Object;
|
|
Assert.AreEqual(networkObjectContext.Object, (NetworkObject)networkObjectReference);
|
|
}
|
|
|
|
[Test]
|
|
public void FailSerializeNonSpawnedNetworkObject()
|
|
{
|
|
using var networkObjectContext = UnityObjectContext.CreateNetworkObject();
|
|
|
|
Assert.Throws<ArgumentException>(() =>
|
|
{
|
|
NetworkObjectReference outReference = networkObjectContext.Object;
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public void FailSerializeGameObjectWithoutNetworkObject()
|
|
{
|
|
using var gameObjectContext = UnityObjectContext.CreateGameObject();
|
|
|
|
Assert.Throws<ArgumentException>(() =>
|
|
{
|
|
NetworkObjectReference outReference = gameObjectContext.Object;
|
|
});
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
//Stop, shutdown, and destroy
|
|
NetworkManagerHelper.ShutdownNetworkManager();
|
|
}
|
|
|
|
public NetworkObjectReferenceTests()
|
|
{
|
|
//Create, instantiate, and host
|
|
NetworkManagerHelper.StartNetworkManager(out _);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper method for tests to create and destroy Unity Objects.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of Object this context incorporates.</typeparam>
|
|
internal class UnityObjectContext<T> : UnityObjectContext where T : Object
|
|
{
|
|
private T m_Object;
|
|
|
|
internal UnityObjectContext(T unityObject, Object root)
|
|
: base(root)
|
|
{
|
|
m_Object = unityObject;
|
|
}
|
|
|
|
public T Object => m_Object;
|
|
}
|
|
|
|
internal class UnityObjectContext : IDisposable
|
|
{
|
|
private Object m_Root;
|
|
|
|
protected UnityObjectContext(Object root)
|
|
{
|
|
m_Root = root;
|
|
}
|
|
|
|
public static UnityObjectContext<GameObject> CreateGameObject(string name = "")
|
|
{
|
|
var gameObject = new GameObject(name);
|
|
return new UnityObjectContext<GameObject>(gameObject, gameObject);
|
|
}
|
|
|
|
public static UnityObjectContext<NetworkObject> CreateNetworkObject(string name = "")
|
|
{
|
|
var gameObject = new GameObject(name);
|
|
var networkObject = gameObject.AddComponent<NetworkObject>();
|
|
return new UnityObjectContext<NetworkObject>(networkObject, gameObject);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Object.DestroyImmediate(m_Root);
|
|
}
|
|
}
|
|
}
|