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.3] - 2024-07-23 ### Added - Added: `UnityTransport.GetNetworkDriver` and `UnityTransport.GetLocalEndpoint` methods to expose the driver and local endpoint being used. (#2978) ### Fixed - Fixed issue where deferred despawn was causing GC allocations when converting an `IEnumerable` to a list. (#2983) - Fixed issue where the realtime network stats monitor was not able to display RPC traffic in release builds due to those stats being only available in development builds or the editor. (#2979) - Fixed issue where `NetworkManager.ScenesLoaded` was not being updated if `PostSynchronizationSceneUnloading` was set and any loaded scenes not used during synchronization were unloaded. (#2971) - Fixed issue where `Rigidbody2d` under Unity 6000.0.11f1 has breaking changes where `velocity` is now `linearVelocity` and `isKinematic` is replaced by `bodyType`. (#2971) - Fixed issue where `NetworkSpawnManager.InstantiateAndSpawn` and `NetworkObject.InstantiateAndSpawn` were not honoring the ownerClientId parameter when using a client-server network topology. (#2968) - Fixed issue where internal delta serialization could not have a byte serializer defined when serializing deltas for other types. Added `[GenerateSerializationForType(typeof(byte))]` to both the `NetworkVariable` and `AnticipatedNetworkVariable` classes to assure a byte serializer is defined.(#2962) - Fixed issue when scene management was disabled and the session owner would still try to synchronize a late joining client. (#2962) - Fixed issue when using a distributed authority network topology where it would allow a session owner to spawn a `NetworkObject` prior to being approved. Now, an error message is logged and the `NetworkObject` will not be spawned prior to the client being approved. (#2962) - Fixed issue where attempting to spawn during `NetworkBehaviour.OnInSceneObjectsSpawned` and `NetworkBehaviour.OnNetworkSessionSynchronized` notifications would throw a collection modified exception. (#2962) ### Changed - Changed logic where clients can now set the `NetworkSceneManager` client synchronization mode when using a distributed authority network topology. (#2985)
156 lines
5.2 KiB
C#
156 lines
5.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Unity.Netcode.TestHelpers.Runtime
|
|
{
|
|
/// <summary>
|
|
/// Will automatically register for the NetworkVariable OnValueChanged
|
|
/// delegate handler. It then will expose that single delegate invocation
|
|
/// to anything that registers for this NetworkVariableHelper's instance's OnValueChanged event.
|
|
/// This allows us to register any NetworkVariable type as well as there are basically two "types of types":
|
|
/// <list type="bullet">
|
|
/// <item>IEquatable<T></item>
|
|
/// <item>ValueType</item>
|
|
/// </list>
|
|
/// From both we can then at least determine if the value indeed changed
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public class NetworkVariableHelper<T> : NetworkVariableBaseHelper
|
|
{
|
|
private readonly NetworkVariable<T> m_NetworkVariable;
|
|
public delegate void OnMyValueChangedDelegateHandler(T previous, T next);
|
|
public event OnMyValueChangedDelegateHandler OnValueChanged;
|
|
|
|
/// <summary>
|
|
/// IEquatable<T> Equals Check
|
|
/// </summary>
|
|
private void CheckVariableChanged(IEquatable<T> previous, IEquatable<T> next)
|
|
{
|
|
if (!previous.Equals(next))
|
|
{
|
|
ValueChanged();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// ValueType Equals Check
|
|
/// </summary>
|
|
private void CheckVariableChanged(ValueType previous, ValueType next)
|
|
{
|
|
if (!previous.Equals(next))
|
|
{
|
|
ValueChanged();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// INetworkVariable's OnVariableChanged delegate callback
|
|
/// </summary>
|
|
/// <param name="previous"></param>
|
|
/// <param name="next"></param>
|
|
private void OnVariableChanged(T previous, T next)
|
|
{
|
|
if (previous is ValueType testValueType)
|
|
{
|
|
CheckVariableChanged(previous as ValueType, next as ValueType);
|
|
}
|
|
else
|
|
{
|
|
CheckVariableChanged(previous as IEquatable<T>, next as IEquatable<T>);
|
|
}
|
|
|
|
OnValueChanged?.Invoke(previous, next);
|
|
}
|
|
|
|
public NetworkVariableHelper(NetworkVariableBase networkVariable) : base(networkVariable)
|
|
{
|
|
m_NetworkVariable = networkVariable as NetworkVariable<T>;
|
|
m_NetworkVariable.OnValueChanged = OnVariableChanged;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The BaseNetworkVariableHelper keeps track of:
|
|
/// The number of instances and associates the instance with the NetworkVariable
|
|
/// The number of times a specific NetworkVariable instance had its value changed (i.e. !Equal)
|
|
/// Note: This could be expanded for future tests focuses around NetworkVariables
|
|
/// </summary>
|
|
public class NetworkVariableBaseHelper
|
|
{
|
|
private static Dictionary<NetworkVariableBaseHelper, NetworkVariableBase> s_Instances;
|
|
private static Dictionary<NetworkVariableBase, int> s_InstanceChangedCount;
|
|
|
|
/// <summary>
|
|
/// Returns the total number of registered INetworkVariables
|
|
/// </summary>
|
|
public static int InstanceCount
|
|
{
|
|
get
|
|
{
|
|
if (s_Instances != null)
|
|
{
|
|
return s_Instances.Count;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns total number of changes that occurred for all registered INetworkVariables
|
|
/// </summary>
|
|
public static int VarChangedCount
|
|
{
|
|
get
|
|
{
|
|
if (s_InstanceChangedCount != null)
|
|
{
|
|
var changeCount = 0;
|
|
foreach (var keyPair in s_InstanceChangedCount)
|
|
{
|
|
changeCount += keyPair.Value;
|
|
}
|
|
return changeCount;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called by the child class NetworkVariableHelper when a value changed
|
|
/// </summary>
|
|
protected void ValueChanged()
|
|
{
|
|
if (s_Instances.ContainsKey(this))
|
|
{
|
|
if (s_InstanceChangedCount.ContainsKey(s_Instances[this]))
|
|
{
|
|
s_InstanceChangedCount[s_Instances[this]]++;
|
|
}
|
|
}
|
|
}
|
|
|
|
public NetworkVariableBaseHelper(NetworkVariableBase networkVariable)
|
|
{
|
|
if (s_Instances == null)
|
|
{
|
|
s_Instances = new Dictionary<NetworkVariableBaseHelper, NetworkVariableBase>();
|
|
}
|
|
|
|
if (s_InstanceChangedCount == null)
|
|
{
|
|
s_InstanceChangedCount = new Dictionary<NetworkVariableBase, int>();
|
|
}
|
|
|
|
// Register new instance and associated INetworkVariable
|
|
if (!s_Instances.ContainsKey(this))
|
|
{
|
|
s_Instances.Add(this, networkVariable);
|
|
if (!s_InstanceChangedCount.ContainsKey(networkVariable))
|
|
{
|
|
s_InstanceChangedCount.Add(networkVariable, 0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|