This repository has been archived on 2025-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
com.unity.netcode.gameobjects/Tests/Runtime/NetworkShowHideTests.cs
Unity Technologies 0f7a30d285 com.unity.netcode.gameobjects@1.0.0-pre.10
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.10] - 2022-06-21

### Added

- Added a new `OnTransportFailure` callback to `NetworkManager`. This callback is invoked when the manager's `NetworkTransport` encounters an unrecoverable error. Transport failures also cause the `NetworkManager` to shut down. Currently, this is only used by `UnityTransport` to signal a timeout of its connection to the Unity Relay servers. (#1994)
- Added `NetworkEvent.TransportFailure`, which can be used by implementations of `NetworkTransport` to signal to `NetworkManager` that an unrecoverable error was encountered. (#1994)
- Added test to ensure a warning occurs when nesting NetworkObjects in a NetworkPrefab (#1969)
- Added `NetworkManager.RemoveNetworkPrefab(...)` to remove a prefab from the prefabs list (#1950)

### Changed

- Updated `UnityTransport` dependency on `com.unity.transport` to 1.1.0. (#2025)
- (API Breaking) `ConnectionApprovalCallback` is no longer an `event` and will not allow more than 1 handler registered at a time. Also, `ConnectionApprovalCallback` is now a `Func<>` taking `ConnectionApprovalRequest` in and returning `ConnectionApprovalResponse` back out (#1972)

### Removed

### Fixed
- Fixed issue where dynamically spawned `NetworkObject`s could throw an exception if the scene of origin handle was zero (0) and the `NetworkObject` was already spawned. (#2017)
- Fixed issue where `NetworkObject.Observers` was not being cleared when despawned. (#2009)
- Fixed `NetworkAnimator` could not run in the server authoritative mode. (#2003)
- Fixed issue where late joining clients would get a soft synchronization error if any in-scene placed NetworkObjects were parented under another `NetworkObject`. (#1985)
- Fixed issue where `NetworkBehaviourReference` would throw a type cast exception if using `NetworkBehaviourReference.TryGet` and the component type was not found. (#1984)
- Fixed `NetworkSceneManager` was not sending scene event notifications for the currently active scene and any additively loaded scenes when loading a new scene in `LoadSceneMode.Single` mode. (#1975)
- Fixed issue where one or more clients disconnecting during a scene event would cause `LoadEventCompleted` or `UnloadEventCompleted` to wait until the `NetworkConfig.LoadSceneTimeOut` period before being triggered. (#1973)
- Fixed issues when multiple `ConnectionApprovalCallback`s were registered (#1972)
- Fixed a regression in serialization support: `FixedString`, `Vector2Int`, and `Vector3Int` types can now be used in NetworkVariables and RPCs again without requiring a `ForceNetworkSerializeByMemcpy<>` wrapper. (#1961)
- Fixed generic types that inherit from NetworkBehaviour causing crashes at compile time. (#1976)
- Fixed endless dialog boxes when adding a `NetworkBehaviour` to a `NetworkManager` or vice-versa. (#1947)
- Fixed `NetworkAnimator` issue where it was only synchronizing parameters if the layer or state changed or was transitioning between states. (#1946)
- Fixed `NetworkAnimator` issue where when it did detect a parameter had changed it would send all parameters as opposed to only the parameters that changed. (#1946)
- Fixed `NetworkAnimator` issue where it was not always disposing the `NativeArray` that is allocated when spawned. (#1946)
- Fixed `NetworkAnimator` issue where it was not taking the animation speed or state speed multiplier into consideration. (#1946)
- Fixed `NetworkAnimator` issue where it was not properly synchronizing late joining clients if they joined while `Animator` was transitioning between states. (#1946)
- Fixed `NetworkAnimator` issue where the server was not relaying changes to non-owner clients when a client was the owner. (#1946)
- Fixed issue where the `PacketLoss` metric for tools would return the packet loss over a connection lifetime instead of a single frame. (#2004)
2022-06-21 00:00:00 +00:00

269 lines
10 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using Unity.Netcode.TestHelpers.Runtime;
namespace Unity.Netcode.RuntimeTests
{
public class NetworkShowHideTestComponent : NetworkBehaviour
{
}
public class ShowHideObject : NetworkBehaviour
{
public static List<ShowHideObject> ClientTargetedNetworkObjects = new List<ShowHideObject>();
public static ulong ClientIdToTarget;
public static NetworkObject GetNetworkObjectById(ulong networkObjectId)
{
foreach (var entry in ClientTargetedNetworkObjects)
{
if (entry.NetworkObjectId == networkObjectId)
{
return entry.NetworkObject;
}
}
return null;
}
public override void OnNetworkSpawn()
{
if (NetworkManager.LocalClientId == ClientIdToTarget)
{
ClientTargetedNetworkObjects.Add(this);
}
base.OnNetworkSpawn();
}
public override void OnNetworkDespawn()
{
if (ClientTargetedNetworkObjects.Contains(this))
{
ClientTargetedNetworkObjects.Remove(this);
}
base.OnNetworkDespawn();
}
public NetworkVariable<int> MyNetworkVariable;
private void Start()
{
MyNetworkVariable = new NetworkVariable<int>();
MyNetworkVariable.OnValueChanged += Changed;
}
public void Changed(int before, int after)
{
Debug.Log($"Value changed from {before} to {after}");
}
}
public class NetworkShowHideTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 2;
private ulong m_ClientId0;
private GameObject m_PrefabToSpawn;
private NetworkObject m_NetSpawnedObject1;
private NetworkObject m_NetSpawnedObject2;
private NetworkObject m_NetSpawnedObject3;
private NetworkObject m_Object1OnClient0;
private NetworkObject m_Object2OnClient0;
private NetworkObject m_Object3OnClient0;
protected override void OnCreatePlayerPrefab()
{
var networkTransform = m_PlayerPrefab.AddComponent<NetworkShowHideTestComponent>();
}
protected override void OnServerAndClientsCreated()
{
m_PrefabToSpawn = CreateNetworkObjectPrefab("ShowHideObject");
m_PrefabToSpawn.AddComponent<ShowHideObject>();
}
// Check that the first client see them, or not, as expected
private IEnumerator CheckVisible(bool isVisible)
{
int count = 0;
do
{
yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 5);
count++;
if (count > 20)
{
// timeout waiting for object to reach the expect visibility
Assert.Fail("timeout waiting for object to reach the expect visibility");
break;
}
} while (m_NetSpawnedObject1.IsNetworkVisibleTo(m_ClientId0) != isVisible ||
m_NetSpawnedObject2.IsNetworkVisibleTo(m_ClientId0) != isVisible ||
m_NetSpawnedObject3.IsNetworkVisibleTo(m_ClientId0) != isVisible ||
m_Object1OnClient0.IsSpawned != isVisible ||
m_Object2OnClient0.IsSpawned != isVisible ||
m_Object3OnClient0.IsSpawned != isVisible
);
Debug.Assert(m_NetSpawnedObject1.IsNetworkVisibleTo(m_ClientId0) == isVisible);
Debug.Assert(m_NetSpawnedObject2.IsNetworkVisibleTo(m_ClientId0) == isVisible);
Debug.Assert(m_NetSpawnedObject3.IsNetworkVisibleTo(m_ClientId0) == isVisible);
Debug.Assert(m_Object1OnClient0.IsSpawned == isVisible);
Debug.Assert(m_Object2OnClient0.IsSpawned == isVisible);
Debug.Assert(m_Object3OnClient0.IsSpawned == isVisible);
var clientNetworkManager = m_ClientNetworkManagers.Where((c) => c.LocalClientId == m_ClientId0).First();
if (isVisible)
{
Assert.True(ShowHideObject.ClientTargetedNetworkObjects.Count == 3, $"Client-{clientNetworkManager.LocalClientId} should have 3 instances visible but only has {ShowHideObject.ClientTargetedNetworkObjects.Count}!");
}
else
{
Assert.True(ShowHideObject.ClientTargetedNetworkObjects.Count == 0, $"Client-{clientNetworkManager.LocalClientId} should have no visible instances but still has {ShowHideObject.ClientTargetedNetworkObjects.Count}!");
}
}
// Set the 3 objects visibility
private void Show(bool individually, bool visibility)
{
if (individually)
{
if (!visibility)
{
m_NetSpawnedObject1.NetworkHide(m_ClientId0);
m_NetSpawnedObject2.NetworkHide(m_ClientId0);
m_NetSpawnedObject3.NetworkHide(m_ClientId0);
}
else
{
m_NetSpawnedObject1.NetworkShow(m_ClientId0);
m_NetSpawnedObject2.NetworkShow(m_ClientId0);
m_NetSpawnedObject3.NetworkShow(m_ClientId0);
}
}
else
{
var list = new List<NetworkObject>();
list.Add(m_NetSpawnedObject1);
list.Add(m_NetSpawnedObject2);
list.Add(m_NetSpawnedObject3);
if (!visibility)
{
NetworkObject.NetworkHide(list, m_ClientId0);
}
else
{
NetworkObject.NetworkShow(list, m_ClientId0);
}
}
}
private bool RefreshNetworkObjects()
{
m_Object1OnClient0 = ShowHideObject.GetNetworkObjectById(m_NetSpawnedObject1.NetworkObjectId);
m_Object2OnClient0 = ShowHideObject.GetNetworkObjectById(m_NetSpawnedObject2.NetworkObjectId);
m_Object3OnClient0 = ShowHideObject.GetNetworkObjectById(m_NetSpawnedObject3.NetworkObjectId);
if (m_Object1OnClient0 == null || m_Object2OnClient0 == null || m_Object3OnClient0 == null)
{
return false;
}
Assert.True(m_Object1OnClient0.NetworkManagerOwner == m_ClientNetworkManagers[0]);
Assert.True(m_Object2OnClient0.NetworkManagerOwner == m_ClientNetworkManagers[0]);
Assert.True(m_Object3OnClient0.NetworkManagerOwner == m_ClientNetworkManagers[0]);
return true;
}
[UnityTest]
public IEnumerator NetworkShowHideTest()
{
m_ClientId0 = m_ClientNetworkManagers[0].LocalClientId;
ShowHideObject.ClientTargetedNetworkObjects.Clear();
ShowHideObject.ClientIdToTarget = m_ClientId0;
// create 3 objects
var spawnedObject1 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
var spawnedObject2 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
var spawnedObject3 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
m_NetSpawnedObject1 = spawnedObject1.GetComponent<NetworkObject>();
m_NetSpawnedObject2 = spawnedObject2.GetComponent<NetworkObject>();
m_NetSpawnedObject3 = spawnedObject3.GetComponent<NetworkObject>();
for (int mode = 0; mode < 2; mode++)
{
// get the NetworkObject on a client instance
yield return WaitForConditionOrTimeOut(RefreshNetworkObjects);
AssertOnTimeout($"Could not refresh all NetworkObjects!");
// check object start visible
yield return CheckVisible(true);
// hide them on one client
Show(mode == 0, false);
yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 5);
m_NetSpawnedObject1.GetComponent<ShowHideObject>().MyNetworkVariable.Value = 3;
yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 5);
// verify they got hidden
yield return CheckVisible(false);
// show them to that client
Show(mode == 0, true);
yield return WaitForConditionOrTimeOut(RefreshNetworkObjects);
AssertOnTimeout($"Could not refresh all NetworkObjects!");
// verify they become visible
yield return CheckVisible(true);
}
}
[UnityTest]
public IEnumerator NetworkShowHideQuickTest()
{
m_ClientId0 = m_ClientNetworkManagers[0].LocalClientId;
ShowHideObject.ClientTargetedNetworkObjects.Clear();
ShowHideObject.ClientIdToTarget = m_ClientId0;
var spawnedObject1 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
var spawnedObject2 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
var spawnedObject3 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
m_NetSpawnedObject1 = spawnedObject1.GetComponent<NetworkObject>();
m_NetSpawnedObject2 = spawnedObject2.GetComponent<NetworkObject>();
m_NetSpawnedObject3 = spawnedObject3.GetComponent<NetworkObject>();
for (int mode = 0; mode < 2; mode++)
{
// get the NetworkObject on a client instance
yield return WaitForConditionOrTimeOut(RefreshNetworkObjects);
AssertOnTimeout($"Could not refresh all NetworkObjects!");
// check object start visible
yield return CheckVisible(true);
// hide and show them on one client, during the same frame
Show(mode == 0, false);
Show(mode == 0, true);
yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 5);
yield return WaitForConditionOrTimeOut(RefreshNetworkObjects);
AssertOnTimeout($"Could not refresh all NetworkObjects!");
yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 5);
// verify they become visible
yield return CheckVisible(true);
}
}
}
}