com.unity.netcode.gameobjects@1.9.1

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

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

## [1.9.1] - 2024-04-18

### Added
- Added AnticipatedNetworkVariable<T>, which adds support for client anticipation of NetworkVariable values, allowing for more responsive gameplay (#2820)
- Added AnticipatedNetworkTransform, which adds support for client anticipation of NetworkTransforms (#2820)
- 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) (#2820)
- 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. (#2820)
- Added virtual method NetworkVariableBase.OnInitialize() which can be used by NetworkVariable subclasses to add initialization code (#2820)
- Added virtual method NetworkVariableBase.Update(), which is called once per frame to support behaviors such as interpolation between an anticipated value and an authoritative one. (#2820)
- Added NetworkTime.TickWithPartial, which represents the current tick as a double that includes the fractional/partial tick value. (#2820)
- 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. (#2820)
- `NetworkVariable` now includes built-in support for `NativeHashSet`, `NativeHashMap`, `List`, `HashSet`, and `Dictionary` (#2813)
- `NetworkVariable` now includes delta compression for collection values (`NativeList`, `NativeArray`, `NativeHashSet`, `NativeHashMap`, `List`, `HashSet`, `Dictionary`, and `FixedString` types) to save bandwidth by only sending the values that changed. (Note: For `NativeList`, `NativeArray`, and `List`, this algorithm works differently than that used in `NetworkList`. This algorithm will use less bandwidth for "set" and "add" operations, but `NetworkList` is more bandwidth-efficient if you are performing frequent "insert" operations.) (#2813)
- `UserNetworkVariableSerialization` now has optional callbacks for `WriteDelta` and `ReadDelta`. If both are provided, they will be used for all serialization operations on NetworkVariables of that type except for the first one for each client. If either is missing, the existing `Write` and `Read` will always be used. (#2813)
- Network variables wrapping `INetworkSerializable` types can perform delta serialization by setting `UserNetworkVariableSerialization<T>.WriteDelta` and `UserNetworkVariableSerialization<T>.ReadDelta` for those types. The built-in `INetworkSerializable` serializer will continue to be used for all other serialization operations, but if those callbacks are set, it will call into them on all but the initial serialization to perform delta serialization. (This could be useful if you have a large struct where most values do not change regularly and you want to send only the fields that did change.) (#2813)

### Fixed

- Fixed issue where NetworkTransformEditor would throw and exception if you excluded the physics package. (#2871)
- Fixed issue where `NetworkTransform` could not properly synchronize its base position when using half float precision. (#2845)
- Fixed issue where the host was not invoking `OnClientDisconnectCallback` for its own local client when internally shutting down. (#2822)
- Fixed issue where NetworkTransform could potentially attempt to "unregister" a named message prior to it being registered. (#2807)
- Fixed issue where in-scene placed `NetworkObject`s with complex nested children `NetworkObject`s (more than one child in depth) would not synchronize properly if WorldPositionStays was set to true. (#2796)

### Changed

- Changed `NetworkObjectReference` and `NetworkBehaviourReference` to allow null references when constructing and serializing. (#2874)
- Changed `NetworkAnimator` no longer requires the `Animator` component to exist on the same `GameObject`. (#2872)
- Changed `NetworkTransform` to now use `NetworkTransformMessage` as opposed to named messages for NetworkTransformState updates. (#2810)
- Changed `CustomMessageManager` so it no longer attempts to register or "unregister" a null or empty string and will log an error if this condition occurs. (#2807)
This commit is contained in:
Unity Technologies
2024-04-18 00:00:00 +00:00
parent f8ebf679ec
commit 158f26b913
72 changed files with 9955 additions and 1289 deletions

View File

@@ -165,13 +165,30 @@ namespace Unity.Netcode.TestHelpers.Runtime
foreach (var sobj in inSceneNetworkObjects)
{
if (sobj.NetworkManagerOwner != networkManager)
ProcessInSceneObject(sobj, networkManager);
}
}
/// <summary>
/// Assures to apply an ObjectNameIdentifier to all children
/// </summary>
private static void ProcessInSceneObject(NetworkObject networkObject, NetworkManager networkManager)
{
if (networkObject.NetworkManagerOwner != networkManager)
{
networkObject.NetworkManagerOwner = networkManager;
}
if (networkObject.GetComponent<ObjectNameIdentifier>() == null)
{
networkObject.gameObject.AddComponent<ObjectNameIdentifier>();
var networkObjects = networkObject.gameObject.GetComponentsInChildren<NetworkObject>();
foreach (var child in networkObjects)
{
sobj.NetworkManagerOwner = networkManager;
}
if (sobj.GetComponent<ObjectNameIdentifier>() == null && sobj.GetComponentInChildren<ObjectNameIdentifier>() == null)
{
sobj.gameObject.AddComponent<ObjectNameIdentifier>();
if (child == networkObject)
{
continue;
}
ProcessInSceneObject(child, networkManager);
}
}
}

View File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using Unity.Collections;
using Random = UnityEngine.Random;
namespace Unity.Netcode.TestHelpers.Runtime
{
@@ -8,37 +10,107 @@ namespace Unity.Netcode.TestHelpers.Runtime
private struct MessageData
{
public ulong FromClientId;
public ArraySegment<byte> Payload;
public FastBufferReader Payload;
public NetworkEvent Event;
public float AvailableTime;
public int Sequence;
public NetworkDelivery Delivery;
}
private static Dictionary<ulong, Queue<MessageData>> s_MessageQueue = new Dictionary<ulong, Queue<MessageData>>();
private static Dictionary<ulong, List<MessageData>> s_MessageQueue = new Dictionary<ulong, List<MessageData>>();
public override ulong ServerClientId { get; } = 0;
public static ulong HighTransportId = 0;
public ulong TransportId = 0;
public float SimulatedLatencySeconds;
public float PacketDropRate;
public float LatencyJitter;
public Dictionary<ulong, int> LastSentSequence = new Dictionary<ulong, int>();
public Dictionary<ulong, int> LastReceivedSequence = new Dictionary<ulong, int>();
public NetworkManager NetworkManager;
public override void Send(ulong clientId, ArraySegment<byte> payload, NetworkDelivery networkDelivery)
{
var copy = new byte[payload.Array.Length];
Array.Copy(payload.Array, copy, payload.Array.Length);
s_MessageQueue[clientId].Enqueue(new MessageData { FromClientId = TransportId, Payload = new ArraySegment<byte>(copy, payload.Offset, payload.Count), Event = NetworkEvent.Data });
if ((networkDelivery == NetworkDelivery.Unreliable || networkDelivery == NetworkDelivery.UnreliableSequenced) && Random.Range(0, 1) < PacketDropRate)
{
return;
}
if (!LastSentSequence.ContainsKey(clientId))
{
LastSentSequence[clientId] = 1;
}
var reader = new FastBufferReader(payload, Allocator.TempJob);
s_MessageQueue[clientId].Add(new MessageData
{
FromClientId = TransportId,
Payload = reader,
Event = NetworkEvent.Data,
AvailableTime = NetworkManager.RealTimeProvider.UnscaledTime + SimulatedLatencySeconds + Random.Range(-LatencyJitter, LatencyJitter),
Sequence = ++LastSentSequence[clientId],
Delivery = networkDelivery
});
s_MessageQueue[clientId].Sort(((a, b) => a.AvailableTime.CompareTo(b.AvailableTime)));
}
public override NetworkEvent PollEvent(out ulong clientId, out ArraySegment<byte> payload, out float receiveTime)
{
if (s_MessageQueue[TransportId].Count > 0)
{
var data = s_MessageQueue[TransportId].Dequeue();
clientId = data.FromClientId;
payload = data.Payload;
MessageData data;
for (; ; )
{
data = s_MessageQueue[TransportId][0];
if (data.AvailableTime > NetworkManager.RealTimeProvider.UnscaledTime)
{
clientId = 0;
payload = new ArraySegment<byte>();
receiveTime = 0;
return NetworkEvent.Nothing;
}
s_MessageQueue[TransportId].RemoveAt(0);
clientId = data.FromClientId;
if (data.Event == NetworkEvent.Data && data.Delivery == NetworkDelivery.UnreliableSequenced && LastReceivedSequence.ContainsKey(clientId) && data.Sequence <= LastReceivedSequence[clientId])
{
continue;
}
break;
}
if (data.Delivery == NetworkDelivery.UnreliableSequenced)
{
LastReceivedSequence[clientId] = data.Sequence;
}
payload = new ArraySegment<byte>();
if (data.Event == NetworkEvent.Data)
{
payload = data.Payload.ToArray();
data.Payload.Dispose();
}
receiveTime = NetworkManager.RealTimeProvider.RealTimeSinceStartup;
if (NetworkManager.IsServer && data.Event == NetworkEvent.Connect)
{
s_MessageQueue[data.FromClientId].Enqueue(new MessageData { Event = NetworkEvent.Connect, FromClientId = ServerClientId, Payload = new ArraySegment<byte>() });
if (!LastSentSequence.ContainsKey(data.FromClientId))
{
LastSentSequence[data.FromClientId] = 1;
}
s_MessageQueue[data.FromClientId].Add(
new MessageData
{
Event = NetworkEvent.Connect,
FromClientId = ServerClientId,
AvailableTime = NetworkManager.RealTimeProvider.UnscaledTime + SimulatedLatencySeconds + Random.Range(-LatencyJitter, LatencyJitter),
Sequence = ++LastSentSequence[data.FromClientId]
});
}
return data.Event;
}
@@ -51,30 +123,45 @@ namespace Unity.Netcode.TestHelpers.Runtime
public override bool StartClient()
{
TransportId = ++HighTransportId;
s_MessageQueue[TransportId] = new Queue<MessageData>();
s_MessageQueue[ServerClientId].Enqueue(new MessageData { Event = NetworkEvent.Connect, FromClientId = TransportId, Payload = new ArraySegment<byte>() });
s_MessageQueue[TransportId] = new List<MessageData>();
s_MessageQueue[ServerClientId].Add(
new MessageData
{
Event = NetworkEvent.Connect,
FromClientId = TransportId,
});
return true;
}
public override bool StartServer()
{
s_MessageQueue[ServerClientId] = new Queue<MessageData>();
s_MessageQueue[ServerClientId] = new List<MessageData>();
return true;
}
public override void DisconnectRemoteClient(ulong clientId)
{
s_MessageQueue[clientId].Enqueue(new MessageData { Event = NetworkEvent.Disconnect, FromClientId = TransportId, Payload = new ArraySegment<byte>() });
s_MessageQueue[clientId].Add(
new MessageData
{
Event = NetworkEvent.Disconnect,
FromClientId = TransportId,
});
}
public override void DisconnectLocalClient()
{
s_MessageQueue[ServerClientId].Enqueue(new MessageData { Event = NetworkEvent.Disconnect, FromClientId = TransportId, Payload = new ArraySegment<byte>() });
s_MessageQueue[ServerClientId].Add(
new MessageData
{
Event = NetworkEvent.Disconnect,
FromClientId = TransportId,
});
}
public override ulong GetCurrentRtt(ulong clientId)
{
return 0;
return (ulong)(SimulatedLatencySeconds * 1000);
}
public override void Shutdown()
@@ -85,5 +172,35 @@ namespace Unity.Netcode.TestHelpers.Runtime
{
NetworkManager = networkManager;
}
protected static void DisposeQueueItems()
{
foreach (var kvp in s_MessageQueue)
{
foreach (var value in kvp.Value)
{
if (value.Event == NetworkEvent.Data)
{
value.Payload.Dispose();
}
}
}
}
public static void Reset()
{
DisposeQueueItems();
s_MessageQueue.Clear();
HighTransportId = 0;
}
public static void ClearQueues()
{
DisposeQueueItems();
foreach (var kvp in s_MessageQueue)
{
kvp.Value.Clear();
}
}
}
}

View File

@@ -308,6 +308,14 @@ namespace Unity.Netcode.TestHelpers.Runtime
NetcodeLogAssert = new NetcodeLogAssert();
if (m_EnableTimeTravel)
{
if (m_NetworkManagerInstatiationMode == NetworkManagerInstatiationMode.AllTests)
{
MockTransport.ClearQueues();
}
else
{
MockTransport.Reset();
}
// Setup the frames per tick for time travel advance to next tick
ConfigureFramesPerTick();
}
@@ -548,6 +556,33 @@ namespace Unity.Netcode.TestHelpers.Runtime
Assert.True(WaitForConditionOrTimeOutWithTimeTravel(() => !networkManager.IsConnectedClient));
}
protected void SetTimeTravelSimulatedLatency(float latencySeconds)
{
((MockTransport)m_ServerNetworkManager.NetworkConfig.NetworkTransport).SimulatedLatencySeconds = latencySeconds;
foreach (var client in m_ClientNetworkManagers)
{
((MockTransport)client.NetworkConfig.NetworkTransport).SimulatedLatencySeconds = latencySeconds;
}
}
protected void SetTimeTravelSimulatedDropRate(float dropRatePercent)
{
((MockTransport)m_ServerNetworkManager.NetworkConfig.NetworkTransport).PacketDropRate = dropRatePercent;
foreach (var client in m_ClientNetworkManagers)
{
((MockTransport)client.NetworkConfig.NetworkTransport).PacketDropRate = dropRatePercent;
}
}
protected void SetTimeTravelSimulatedLatencyJitter(float jitterSeconds)
{
((MockTransport)m_ServerNetworkManager.NetworkConfig.NetworkTransport).LatencyJitter = jitterSeconds;
foreach (var client in m_ClientNetworkManagers)
{
((MockTransport)client.NetworkConfig.NetworkTransport).LatencyJitter = jitterSeconds;
}
}
/// <summary>
/// Creates the server and clients
/// </summary>
@@ -1005,6 +1040,17 @@ namespace Unity.Netcode.TestHelpers.Runtime
VerboseDebug($"Exiting {nameof(TearDown)}");
LogWaitForMessages();
NetcodeLogAssert.Dispose();
if (m_EnableTimeTravel)
{
if (m_NetworkManagerInstatiationMode == NetworkManagerInstatiationMode.AllTests)
{
MockTransport.ClearQueues();
}
else
{
MockTransport.Reset();
}
}
}
/// <summary>
@@ -1554,8 +1600,17 @@ namespace Unity.Netcode.TestHelpers.Runtime
/// </summary>
/// <param name="amountOfTimeInSeconds"></param>
/// <param name="numFramesToSimulate"></param>
protected static void TimeTravel(double amountOfTimeInSeconds, int numFramesToSimulate)
protected static void TimeTravel(double amountOfTimeInSeconds, int numFramesToSimulate = -1)
{
if (numFramesToSimulate < 0)
{
var frameRate = Application.targetFrameRate;
if (frameRate <= 0)
{
frameRate = 60;
}
numFramesToSimulate = Math.Max((int)(amountOfTimeInSeconds / frameRate), 1);
}
var interval = amountOfTimeInSeconds / numFramesToSimulate;
for (var i = 0; i < numFramesToSimulate; ++i)
{
@@ -1613,6 +1668,16 @@ namespace Unity.Netcode.TestHelpers.Runtime
TimeTravel(timePassed, frames);
}
private struct UpdateData
{
public MethodInfo Update;
public MethodInfo FixedUpdate;
public MethodInfo LateUpdate;
}
private static object[] s_EmptyObjectArray = { };
private static Dictionary<Type, UpdateData> s_UpdateFunctionCache = new Dictionary<Type, UpdateData>();
/// <summary>
/// Simulates one SDK frame. This can be used even without TimeTravel, though it's of somewhat less use
/// without TimeTravel, as, without the mock transport, it will likely not provide enough time for any
@@ -1620,33 +1685,50 @@ namespace Unity.Netcode.TestHelpers.Runtime
/// </summary>
public static void SimulateOneFrame()
{
foreach (NetworkUpdateStage stage in Enum.GetValues(typeof(NetworkUpdateStage)))
foreach (NetworkUpdateStage updateStage in Enum.GetValues(typeof(NetworkUpdateStage)))
{
NetworkUpdateLoop.RunNetworkUpdateStage(stage);
string methodName = string.Empty;
switch (stage)
var stage = updateStage;
// These two are out of order numerically due to backward compatibility
// requirements. We have to swap them to maintain correct execution
// order.
if (stage == NetworkUpdateStage.PostScriptLateUpdate)
{
case NetworkUpdateStage.FixedUpdate:
methodName = "FixedUpdate"; // mapping NetworkUpdateStage.FixedUpdate to MonoBehaviour.FixedUpdate
break;
case NetworkUpdateStage.Update:
methodName = "Update"; // mapping NetworkUpdateStage.Update to MonoBehaviour.Update
break;
case NetworkUpdateStage.PreLateUpdate:
methodName = "LateUpdate"; // mapping NetworkUpdateStage.PreLateUpdate to MonoBehaviour.LateUpdate
break;
stage = NetworkUpdateStage.PostLateUpdate;
}
if (!string.IsNullOrEmpty(methodName))
else if (stage == NetworkUpdateStage.PostLateUpdate)
{
#if UNITY_2023_1_OR_NEWER
foreach (var behaviour in Object.FindObjectsByType<NetworkBehaviour>(FindObjectsSortMode.InstanceID))
#else
foreach (var behaviour in Object.FindObjectsOfType<NetworkBehaviour>())
#endif
stage = NetworkUpdateStage.PostScriptLateUpdate;
}
NetworkUpdateLoop.RunNetworkUpdateStage(stage);
if (stage == NetworkUpdateStage.Update || stage == NetworkUpdateStage.FixedUpdate || stage == NetworkUpdateStage.PreLateUpdate)
{
foreach (var behaviour in Object.FindObjectsByType<NetworkBehaviour>(FindObjectsSortMode.None))
{
var method = behaviour.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
method?.Invoke(behaviour, new object[] { });
var type = behaviour.GetType();
if (!s_UpdateFunctionCache.TryGetValue(type, out var updateData))
{
updateData = new UpdateData
{
Update = type.GetMethod("Update", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance),
FixedUpdate = type.GetMethod("FixedUpdate", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance),
LateUpdate = type.GetMethod("LateUpdate", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance),
};
s_UpdateFunctionCache[type] = updateData;
}
switch (stage)
{
case NetworkUpdateStage.FixedUpdate:
updateData.FixedUpdate?.Invoke(behaviour, new object[] { });
break;
case NetworkUpdateStage.Update:
updateData.Update?.Invoke(behaviour, new object[] { });
break;
case NetworkUpdateStage.PreLateUpdate:
updateData.LateUpdate?.Invoke(behaviour, new object[] { });
break;
}
}
}
}