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)
288 lines
11 KiB
C#
288 lines
11 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace Unity.Netcode
|
|
{
|
|
public struct NetworkVariableUpdateTraits
|
|
{
|
|
[Tooltip("The minimum amount of time that must pass between sending updates. If this amount of time has not passed since the last update, dirtiness will be ignored.")]
|
|
public float MinSecondsBetweenUpdates;
|
|
|
|
[Tooltip("The maximum amount of time that a variable can be dirty without sending an update. If this amount of time has passed since the last update, an update will be sent even if the dirtiness threshold has not been met.")]
|
|
public float MaxSecondsBetweenUpdates;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Interface for network value containers
|
|
/// </summary>
|
|
public abstract class NetworkVariableBase : IDisposable
|
|
{
|
|
[SerializeField]
|
|
internal NetworkVariableUpdateTraits UpdateTraits = default;
|
|
|
|
[NonSerialized]
|
|
internal double LastUpdateSent;
|
|
|
|
/// <summary>
|
|
/// The delivery type (QoS) to send data with
|
|
/// </summary>
|
|
internal const NetworkDelivery Delivery = NetworkDelivery.ReliableFragmentedSequenced;
|
|
|
|
/// <summary>
|
|
/// Maintains a link to the associated NetworkBehaviour
|
|
/// </summary>
|
|
private protected NetworkBehaviour m_NetworkBehaviour;
|
|
|
|
public NetworkBehaviour GetBehaviour()
|
|
{
|
|
return m_NetworkBehaviour;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes the NetworkVariable
|
|
/// </summary>
|
|
/// <param name="networkBehaviour">The NetworkBehaviour the NetworkVariable belongs to</param>
|
|
public void Initialize(NetworkBehaviour networkBehaviour)
|
|
{
|
|
m_NetworkBehaviour = networkBehaviour;
|
|
if (m_NetworkBehaviour.NetworkManager)
|
|
{
|
|
if (m_NetworkBehaviour.NetworkManager.NetworkTimeSystem != null)
|
|
{
|
|
UpdateLastSentTime();
|
|
}
|
|
}
|
|
|
|
OnInitialize();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called on initialization
|
|
/// </summary>
|
|
public virtual void OnInitialize()
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the update traits for this network variable to determine how frequently it will send updates.
|
|
/// </summary>
|
|
/// <param name="traits"></param>
|
|
public void SetUpdateTraits(NetworkVariableUpdateTraits traits)
|
|
{
|
|
UpdateTraits = traits;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check whether or not this variable has changed significantly enough to send an update.
|
|
/// If not, no update will be sent even if the variable is dirty, unless the time since last update exceeds
|
|
/// the <see cref="UpdateTraits"/>' <see cref="NetworkVariableUpdateTraits.MaxSecondsBetweenUpdates"/>.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public virtual bool ExceedsDirtinessThreshold()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The default read permissions
|
|
/// </summary>
|
|
public const NetworkVariableReadPermission DefaultReadPerm = NetworkVariableReadPermission.Everyone;
|
|
|
|
/// <summary>
|
|
/// The default write permissions
|
|
/// </summary>
|
|
public const NetworkVariableWritePermission DefaultWritePerm = NetworkVariableWritePermission.Server;
|
|
|
|
/// <summary>
|
|
/// The default constructor for <see cref="NetworkVariableBase"/> that can be used to create a
|
|
/// custom NetworkVariable.
|
|
/// </summary>
|
|
/// <param name="readPerm">the <see cref="NetworkVariableReadPermission"/> access settings</param>
|
|
/// <param name="writePerm">the <see cref="NetworkVariableWritePermission"/> access settings</param>
|
|
protected NetworkVariableBase(
|
|
NetworkVariableReadPermission readPerm = DefaultReadPerm,
|
|
NetworkVariableWritePermission writePerm = DefaultWritePerm)
|
|
{
|
|
ReadPerm = readPerm;
|
|
WritePerm = writePerm;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The <see cref="m_IsDirty"/> property is used to determine if the
|
|
/// value of the `NetworkVariable` has changed.
|
|
/// </summary>
|
|
private bool m_IsDirty;
|
|
|
|
/// <summary>
|
|
/// Gets or sets the name of the network variable's instance
|
|
/// (MemberInfo) where it was declared.
|
|
/// </summary>
|
|
public string Name { get; internal set; }
|
|
|
|
/// <summary>
|
|
/// The read permission for this var
|
|
/// </summary>
|
|
public readonly NetworkVariableReadPermission ReadPerm;
|
|
|
|
/// <summary>
|
|
/// The write permission for this var
|
|
/// </summary>
|
|
public readonly NetworkVariableWritePermission WritePerm;
|
|
|
|
/// <summary>
|
|
/// Sets whether or not the variable needs to be delta synced
|
|
/// </summary>
|
|
/// <param name="isDirty">Whether or not the var is dirty</param>
|
|
public virtual void SetDirty(bool isDirty)
|
|
{
|
|
m_IsDirty = isDirty;
|
|
|
|
if (m_IsDirty)
|
|
{
|
|
MarkNetworkBehaviourDirty();
|
|
}
|
|
}
|
|
|
|
internal bool CanSend()
|
|
{
|
|
var timeSinceLastUpdate = m_NetworkBehaviour.NetworkManager.NetworkTimeSystem.LocalTime - LastUpdateSent;
|
|
return
|
|
(
|
|
UpdateTraits.MaxSecondsBetweenUpdates > 0 &&
|
|
timeSinceLastUpdate >= UpdateTraits.MaxSecondsBetweenUpdates
|
|
) ||
|
|
(
|
|
timeSinceLastUpdate >= UpdateTraits.MinSecondsBetweenUpdates &&
|
|
ExceedsDirtinessThreshold()
|
|
);
|
|
}
|
|
|
|
internal void UpdateLastSentTime()
|
|
{
|
|
LastUpdateSent = m_NetworkBehaviour.NetworkManager.NetworkTimeSystem.LocalTime;
|
|
}
|
|
|
|
protected void MarkNetworkBehaviourDirty()
|
|
{
|
|
if (m_NetworkBehaviour == null)
|
|
{
|
|
Debug.LogWarning($"NetworkVariable is written to, but doesn't know its NetworkBehaviour yet. " +
|
|
"Are you modifying a NetworkVariable before the NetworkObject is spawned?");
|
|
return;
|
|
}
|
|
if (m_NetworkBehaviour.NetworkManager.ShutdownInProgress)
|
|
{
|
|
if (m_NetworkBehaviour.NetworkManager.LogLevel <= LogLevel.Developer)
|
|
{
|
|
Debug.LogWarning($"NetworkVariable is written to during the NetworkManager shutdown! " +
|
|
"Are you modifying a NetworkVariable within a NetworkBehaviour.OnDestroy or NetworkBehaviour.OnDespawn method?");
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (!m_NetworkBehaviour.NetworkManager.IsListening)
|
|
{
|
|
if (m_NetworkBehaviour.NetworkManager.LogLevel <= LogLevel.Developer)
|
|
{
|
|
Debug.LogWarning($"NetworkVariable is written to after the NetworkManager has already shutdown! " +
|
|
"Are you modifying a NetworkVariable within a NetworkBehaviour.OnDestroy or NetworkBehaviour.OnDespawn method?");
|
|
}
|
|
return;
|
|
}
|
|
m_NetworkBehaviour.NetworkManager.BehaviourUpdater.AddForUpdate(m_NetworkBehaviour.NetworkObject);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resets the dirty state and marks the variable as synced / clean
|
|
/// </summary>
|
|
public virtual void ResetDirty()
|
|
{
|
|
m_IsDirty = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets Whether or not the container is dirty
|
|
/// </summary>
|
|
/// <returns>Whether or not the container is dirty</returns>
|
|
public virtual bool IsDirty()
|
|
{
|
|
return m_IsDirty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets if a specific client has permission to read the var or not
|
|
/// </summary>
|
|
/// <param name="clientId">The client id</param>
|
|
/// <returns>Whether or not the client has permission to read</returns>
|
|
public bool CanClientRead(ulong clientId)
|
|
{
|
|
switch (ReadPerm)
|
|
{
|
|
default:
|
|
case NetworkVariableReadPermission.Everyone:
|
|
return true;
|
|
case NetworkVariableReadPermission.Owner:
|
|
return clientId == m_NetworkBehaviour.NetworkObject.OwnerClientId || NetworkManager.ServerClientId == clientId;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets if a specific client has permission to write the var or not
|
|
/// </summary>
|
|
/// <param name="clientId">The client id</param>
|
|
/// <returns>Whether or not the client has permission to write</returns>
|
|
public bool CanClientWrite(ulong clientId)
|
|
{
|
|
switch (WritePerm)
|
|
{
|
|
default:
|
|
case NetworkVariableWritePermission.Server:
|
|
return clientId == NetworkManager.ServerClientId;
|
|
case NetworkVariableWritePermission.Owner:
|
|
return clientId == m_NetworkBehaviour.NetworkObject.OwnerClientId;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the ClientId of the owning client
|
|
/// </summary>
|
|
internal ulong OwnerClientId()
|
|
{
|
|
return m_NetworkBehaviour.NetworkObject.OwnerClientId;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes the dirty changes, that is, the changes since the variable was last dirty, to the writer
|
|
/// </summary>
|
|
/// <param name="writer">The stream to write the dirty changes to</param>
|
|
public abstract void WriteDelta(FastBufferWriter writer);
|
|
|
|
/// <summary>
|
|
/// Writes the complete state of the variable to the writer
|
|
/// </summary>
|
|
/// <param name="writer">The stream to write the state to</param>
|
|
public abstract void WriteField(FastBufferWriter writer);
|
|
|
|
/// <summary>
|
|
/// Reads the complete state from the reader and applies it
|
|
/// </summary>
|
|
/// <param name="reader">The stream to read the state from</param>
|
|
public abstract void ReadField(FastBufferReader reader);
|
|
|
|
/// <summary>
|
|
/// Reads delta from the reader and applies them to the internal value
|
|
/// </summary>
|
|
/// <param name="reader">The stream to read the delta from</param>
|
|
/// <param name="keepDirtyDelta">Whether or not the delta should be kept as dirty or consumed</param>
|
|
public abstract void ReadDelta(FastBufferReader reader, bool keepDirtyDelta);
|
|
|
|
/// <summary>
|
|
/// Virtual <see cref="IDisposable"/> implementation
|
|
/// </summary>
|
|
public virtual void Dispose()
|
|
{
|
|
}
|
|
}
|
|
}
|