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.0.0-pre.6] - 2022-03-02 ### Added - NetworkAnimator now properly synchrhonizes all animation layers as well as runtime-adjusted weighting between them (#1765) - Added first set of tests for NetworkAnimator - parameter syncing, trigger set / reset, override network animator (#1735) ### Changed ### Fixed - Fixed an issue where sometimes the first client to connect to the server could see messages from the server as coming from itself. (#1683) - Fixed an issue where clients seemed to be able to send messages to ClientId 1, but these messages would actually still go to the server (id 0) instead of that client. (#1683) - Improved clarity of error messaging when a client attempts to send a message to a destination other than the server, which isn't allowed. (#1683) - Disallowed async keyword in RPCs (#1681) - Fixed an issue where Alpha release versions of Unity (version 2022.2.0a5 and later) will not compile due to the UNet Transport no longer existing (#1678) - Fixed messages larger than 64k being written with incorrectly truncated message size in header (#1686) (credit: @kaen) - Fixed overloading RPC methods causing collisions and failing on IL2CPP targets. (#1694) - Fixed spawn flow to propagate `IsSceneObject` down to children NetworkObjects, decouple implicit relationship between object spawning & `IsSceneObject` flag (#1685) - Fixed error when serializing ConnectionApprovalMessage with scene management disabled when one or more objects is hidden via the CheckObjectVisibility delegate (#1720) - Fixed CheckObjectVisibility delegate not being properly invoked for connecting clients when Scene Management is enabled. (#1680) - Fixed NetworkList to properly call INetworkSerializable's NetworkSerialize() method (#1682) - Fixed NetworkVariables containing more than 1300 bytes of data (such as large NetworkLists) no longer cause an OverflowException (the limit on data size is now whatever limit the chosen transport imposes on fragmented NetworkDelivery mechanisms) (#1725) - Fixed ServerRpcParams and ClientRpcParams must be the last parameter of an RPC in order to function properly. Added a compile-time check to ensure this is the case and trigger an error if they're placed elsewhere (#1721) - Fixed FastBufferReader being created with a length of 1 if provided an input of length 0 (#1724) - Fixed The NetworkConfig's checksum hash includes the NetworkTick so that clients with a different tickrate than the server are identified and not allowed to connect (#1728) - Fixed OwnedObjects not being properly modified when using ChangeOwnership (#1731) - Improved performance in NetworkAnimator (#1735) - Removed the "always sync" network animator (aka "autosend") parameters (#1746)
217 lines
8.2 KiB
C#
217 lines
8.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.TestTools;
|
|
using Unity.Netcode.TestHelpers.Runtime;
|
|
|
|
namespace Unity.Netcode.RuntimeTests
|
|
{
|
|
public class HiddenVariableTest : NetworkBehaviour
|
|
{
|
|
}
|
|
|
|
public class HiddenVariableObject : NetworkBehaviour
|
|
{
|
|
public NetworkVariable<int> MyNetworkVariable = new NetworkVariable<int>();
|
|
public NetworkList<int> MyNetworkList = new NetworkList<int>();
|
|
|
|
public static Dictionary<ulong, int> ValueOnClient = new Dictionary<ulong, int>();
|
|
public static int ExpectedSize = 0;
|
|
public static int SpawnCount = 0;
|
|
|
|
public override void OnNetworkSpawn()
|
|
{
|
|
Debug.Log($"{nameof(HiddenVariableObject)}.{nameof(OnNetworkSpawn)}() with value {MyNetworkVariable.Value}");
|
|
|
|
MyNetworkVariable.OnValueChanged += Changed;
|
|
MyNetworkList.OnListChanged += ListChanged;
|
|
SpawnCount++;
|
|
|
|
base.OnNetworkSpawn();
|
|
}
|
|
|
|
public void Changed(int before, int after)
|
|
{
|
|
Debug.Log($"Value changed from {before} to {after} on {NetworkManager.LocalClientId}");
|
|
ValueOnClient[NetworkManager.LocalClientId] = after;
|
|
}
|
|
public void ListChanged(NetworkListEvent<int> listEvent)
|
|
{
|
|
Debug.Log($"ListEvent received: type {listEvent.Type}, index {listEvent.Index}, value {listEvent.Value}");
|
|
Debug.Assert(ExpectedSize == MyNetworkList.Count);
|
|
}
|
|
}
|
|
|
|
public class HiddenVariableTests : NetcodeIntegrationTest
|
|
{
|
|
protected override int NumberOfClients => 4;
|
|
|
|
private NetworkObject m_NetSpawnedObject;
|
|
private List<NetworkObject> m_NetSpawnedObjectOnClient = new List<NetworkObject>();
|
|
private GameObject m_TestNetworkPrefab;
|
|
|
|
protected override void OnCreatePlayerPrefab()
|
|
{
|
|
m_PlayerPrefab.AddComponent<HiddenVariableTest>();
|
|
}
|
|
|
|
protected override void OnServerAndClientsCreated()
|
|
{
|
|
m_TestNetworkPrefab = CreateNetworkObjectPrefab("MyTestObject");
|
|
m_TestNetworkPrefab.AddComponent<HiddenVariableObject>();
|
|
}
|
|
|
|
public IEnumerator WaitForSpawnCount(int targetCount)
|
|
{
|
|
var endTime = Time.realtimeSinceStartup + 1.0;
|
|
while (HiddenVariableObject.SpawnCount != targetCount &&
|
|
Time.realtimeSinceStartup < endTime)
|
|
{
|
|
yield return new WaitForSeconds(0.01f);
|
|
}
|
|
}
|
|
|
|
public void VerifyLists()
|
|
{
|
|
NetworkList<int> prev = null;
|
|
int numComparison = 0;
|
|
|
|
// for all the instances of NetworkList
|
|
foreach (var gameObject in m_NetSpawnedObjectOnClient)
|
|
{
|
|
// this skips despawned/hidden objects
|
|
if (gameObject != null)
|
|
{
|
|
// if we've seen another one before
|
|
if (prev != null)
|
|
{
|
|
var curr = gameObject.GetComponent<HiddenVariableObject>().MyNetworkList;
|
|
|
|
// check that the two lists are identical
|
|
Debug.Assert(curr.Count == prev.Count);
|
|
for (int index = 0; index < curr.Count; index++)
|
|
{
|
|
Debug.Assert(curr[index] == prev[index]);
|
|
}
|
|
numComparison++;
|
|
}
|
|
// store the list
|
|
prev = gameObject.GetComponent<HiddenVariableObject>().MyNetworkList;
|
|
}
|
|
}
|
|
Debug.Log($"{numComparison} comparisons done.");
|
|
}
|
|
|
|
public IEnumerator RefreshGameObects()
|
|
{
|
|
m_NetSpawnedObjectOnClient.Clear();
|
|
|
|
foreach (var netMan in m_ClientNetworkManagers)
|
|
{
|
|
var serverClientPlayerResult = new NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject>();
|
|
yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation(
|
|
x => x.NetworkObjectId == m_NetSpawnedObject.NetworkObjectId,
|
|
netMan,
|
|
serverClientPlayerResult);
|
|
m_NetSpawnedObjectOnClient.Add(serverClientPlayerResult.Result);
|
|
}
|
|
}
|
|
|
|
[UnityTest]
|
|
public IEnumerator HiddenVariableTest()
|
|
{
|
|
HiddenVariableObject.SpawnCount = 0;
|
|
HiddenVariableObject.ValueOnClient.Clear();
|
|
HiddenVariableObject.ExpectedSize = 0;
|
|
HiddenVariableObject.SpawnCount = 0;
|
|
|
|
Debug.Log("Running test");
|
|
|
|
|
|
// ==== Spawn object with ownership on one client
|
|
var client = m_ServerNetworkManager.ConnectedClientsList[1];
|
|
var otherClient = m_ServerNetworkManager.ConnectedClientsList[2];
|
|
m_NetSpawnedObject = SpawnObject(m_TestNetworkPrefab, m_ClientNetworkManagers[1]).GetComponent<NetworkObject>();
|
|
|
|
yield return RefreshGameObects();
|
|
|
|
// === Check spawn occured
|
|
yield return WaitForSpawnCount(NumberOfClients + 1);
|
|
Debug.Assert(HiddenVariableObject.SpawnCount == NumberOfClients + 1);
|
|
Debug.Log("Objects spawned");
|
|
|
|
// ==== Set the NetworkVariable value to 2
|
|
HiddenVariableObject.ExpectedSize = 1;
|
|
HiddenVariableObject.SpawnCount = 0;
|
|
|
|
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkVariable.Value = 2;
|
|
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkList.Add(2);
|
|
|
|
yield return new WaitForSeconds(1.0f);
|
|
|
|
foreach (var id in m_ServerNetworkManager.ConnectedClientsIds)
|
|
{
|
|
Debug.Assert(HiddenVariableObject.ValueOnClient[id] == 2);
|
|
}
|
|
|
|
VerifyLists();
|
|
|
|
Debug.Log("Value changed");
|
|
|
|
// ==== Hide our object to a different client
|
|
HiddenVariableObject.ExpectedSize = 2;
|
|
m_NetSpawnedObject.NetworkHide(otherClient.ClientId);
|
|
|
|
// ==== Change the NetworkVariable value
|
|
// we should get one less notification of value changing and no errors or exception
|
|
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkVariable.Value = 3;
|
|
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkList.Add(3);
|
|
|
|
yield return new WaitForSeconds(1.0f);
|
|
foreach (var id in m_ServerNetworkManager.ConnectedClientsIds)
|
|
{
|
|
if (id != otherClient.ClientId)
|
|
{
|
|
Debug.Assert(HiddenVariableObject.ValueOnClient[id] == 3);
|
|
}
|
|
}
|
|
|
|
VerifyLists();
|
|
Debug.Log("Values changed");
|
|
|
|
// ==== Show our object again to this client
|
|
HiddenVariableObject.ExpectedSize = 3;
|
|
m_NetSpawnedObject.NetworkShow(otherClient.ClientId);
|
|
|
|
// ==== Wait for object to be spawned
|
|
yield return WaitForSpawnCount(1);
|
|
Debug.Assert(HiddenVariableObject.SpawnCount == 1);
|
|
Debug.Log("Object spawned");
|
|
|
|
// ==== We need a refresh for the newly re-spawned object
|
|
yield return RefreshGameObects();
|
|
|
|
// ==== Change the NetworkVariable value
|
|
// we should get all notifications of value changing and no errors or exception
|
|
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkVariable.Value = 4;
|
|
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkList.Add(4);
|
|
|
|
yield return new WaitForSeconds(1.0f);
|
|
|
|
foreach (var id in m_ServerNetworkManager.ConnectedClientsIds)
|
|
{
|
|
Debug.Assert(HiddenVariableObject.ValueOnClient[id] == 4);
|
|
}
|
|
|
|
VerifyLists();
|
|
Debug.Log("Values changed");
|
|
|
|
// ==== Hide our object to that different client again, and then destroy it
|
|
m_NetSpawnedObject.NetworkHide(otherClient.ClientId);
|
|
yield return new WaitForSeconds(0.2f);
|
|
m_NetSpawnedObject.Despawn();
|
|
yield return new WaitForSeconds(0.2f);
|
|
}
|
|
}
|
|
}
|