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/Editor/NetworkObjectTests.cs
Unity Technologies ffef45b50f com.unity.netcode.gameobjects@1.7.0
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.7.0] - 2023-10-11

### Added

- exposed NetworkObject.GetNetworkBehaviourAtOrderIndex as a public API (#2724)
- Added context menu tool that provides users with the ability to quickly update the GlobalObjectIdHash value for all in-scene placed prefab instances that were created prior to adding a NetworkObject component to it. (#2707)
- Added methods NetworkManager.SetPeerMTU and NetworkManager.GetPeerMTU to be able to set MTU sizes per-peer (#2676)
- Added `GenerateSerializationForGenericParameterAttribute`, which can be applied to user-created Network Variable types to ensure the codegen generates serialization for the generic types they wrap. (#2694)
- Added `GenerateSerializationForTypeAttribute`, which can be applied to any class or method to ensure the codegen generates serialization for the specific provided type. (#2694)
- Exposed `NetworkVariableSerialization<T>.Read`, `NetworkVariableSerialization<T>.Write`, `NetworkVariableSerialization<T>.AreEqual`, and `NetworkVariableSerialization<T>.Duplicate` to further support the creation of user-created network variables by allowing users to access the generated serialization methods and serialize generic types efficiently without boxing. (#2694)
- Added `NetworkVariableBase.MarkNetworkBehaviourDirty` so that user-created network variable types can mark their containing `NetworkBehaviour` to be processed by the update loop. (#2694)

### Fixed

- Fixed issue where the server side `NetworkSceneManager` instance was not adding the currently active scene to its list of scenes loaded. (#2723)
- Generic NetworkBehaviour types no longer result in compile errors or runtime errors (#2720)
- Rpcs within Generic NetworkBehaviour types can now serialize parameters of the class's generic types (but may not have generic types of their own) (#2720)
- Errors are no longer thrown when entering play mode with domain reload disabled (#2720)
- NetworkSpawn is now correctly called each time when entering play mode with scene reload disabled (#2720)
- NetworkVariables of non-integer types will no longer break the inspector (#2714)
- NetworkVariables with NonSerializedAttribute will not appear in the inspector (#2714)
- Fixed issue where `UnityTransport` would attempt to establish WebSocket connections even if using UDP/DTLS Relay allocations when the build target was WebGL. This only applied to working in the editor since UDP/DTLS can't work in the browser. (#2695)
- Fixed issue where a `NetworkBehaviour` component's `OnNetworkDespawn` was not being invoked on the host-server side for an in-scene placed `NetworkObject` when a scene was unloaded (during a scene transition) and the `NetworkBehaviour` component was positioned/ordered before the `NetworkObject` component. (#2685)
- Fixed issue where `SpawnWithObservers` was not being honored when `NetworkConfig.EnableSceneManagement` was disabled. (#2682)
- Fixed issue where `NetworkAnimator` was not internally tracking changes to layer weights which prevented proper layer weight synchronization back to the original layer weight value. (#2674)
- Fixed "writing past the end of the buffer" error when calling ResetDirty() on managed network variables that are larger than 256 bytes when serialized. (#2670)
- Fixed issue where generation of the `DefaultNetworkPrefabs` asset was not enabled by default. (#2662)
- Fixed issue where the `GlobalObjectIdHash` value could be updated but the asset not marked as dirty. (#2662)
- Fixed issue where the `GlobalObjectIdHash` value of a (network) prefab asset could be assigned an incorrect value when editing the prefab in a temporary scene. (#2662)
- Fixed issue where the `GlobalObjectIdHash` value generated after creating a (network) prefab from an object constructed within the scene would not be the correct final value in a stand alone build. (#2662)

### Changed

- Updated dependency on `com.unity.transport` to version 1.4.0. (#2716)
2023-10-11 00:00:00 +00:00

160 lines
6.2 KiB
C#

using System.Text.RegularExpressions;
using NUnit.Framework;
using Unity.Netcode.Editor;
using UnityEngine;
using UnityEngine.TestTools;
namespace Unity.Netcode.EditorTests
{
public class NetworkObjectTests
{
[Test]
public void NetworkManagerOverrideTest()
{
// Create "bait"
var singletonNetworkManager = new GameObject(nameof(NetworkManager)).AddComponent<NetworkManager>();
singletonNetworkManager.SetSingleton();
// Create override
var networkManager = new GameObject(nameof(NetworkManager)).AddComponent<NetworkManager>();
// NetworkObject
var gameObject = new GameObject(nameof(NetworkManagerOverrideTest));
var networkObject = gameObject.AddComponent<NetworkObject>();
// Set override
networkObject.NetworkManagerOwner = networkManager;
Debug.Assert(networkObject.NetworkManager == networkManager);
Object.DestroyImmediate(singletonNetworkManager.gameObject);
Object.DestroyImmediate(networkManager.gameObject);
Object.DestroyImmediate(gameObject);
}
[Test]
[TestCase(0)]
[TestCase(1)]
[TestCase(2)]
public void GetBehaviourIndexNone(int index)
{
var gameObject = new GameObject(nameof(GetBehaviourIndexNone));
var networkObject = gameObject.AddComponent<NetworkObject>();
LogAssert.Expect(LogType.Error, new Regex(".*out of bounds.*"));
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex((ushort)index), Is.Null);
// Cleanup
Object.DestroyImmediate(gameObject);
}
[Test]
public void GetBehaviourIndexOne()
{
var gameObject = new GameObject(nameof(GetBehaviourIndexOne));
var networkObject = gameObject.AddComponent<NetworkObject>();
var networkBehaviour = gameObject.AddComponent<EmptyNetworkBehaviour>();
LogAssert.Expect(LogType.Error, new Regex(".*out of bounds.*"));
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(0), Is.EqualTo(networkBehaviour));
Assert.That(networkObject.GetNetworkBehaviourAtOrderIndex(1), Is.Null);
// Cleanup
Object.DestroyImmediate(gameObject);
}
/// <summary>
/// Verifies that a NetworkObject component that is positioned after a NetworkBehaviour component will
/// be migrated to a component index value that is before the lowest NetworkBehaviour component index value.
/// (The lowest NetworkBehaviour component's index value will also change when this happens)
/// </summary>
[Test]
public void NetworkObjectComponentOrder()
{
var gameObject = new GameObject(nameof(GetBehaviourIndexOne));
// Add the Networkbehaviour first
var networkBehaviour = gameObject.AddComponent<EmptyNetworkBehaviour>();
// Add an empty MonoBehaviour inbetween the NetworkBehaviour and NetworkObject
gameObject.AddComponent<EmptyMonoBehaviour>();
// Add the NetworkObject
var networkObject = gameObject.AddComponent<NetworkObject>();
var componentIndices = GetIndices(gameObject);
// Verify the NetworkObject procedes the NetworkBehaviour
Assert.True(componentIndices.NetworkObjectIndex > componentIndices.NetworkBehaviourIndex, $"[Initial Setup] NetworkObject index ({componentIndices.NetworkObjectIndex}) is not greater than the NetworkBehaviour index ({componentIndices.NetworkBehaviourIndex})!");
// Force-Invoke the CheckForNetworkObject method in order to verify the NetworkObject is moved
NetworkBehaviourEditor.CheckForNetworkObject(gameObject);
var adjustedIndices = GetIndices(gameObject);
Assert.True(ValidateComponentIndices(componentIndices, GetIndices(gameObject)), "NetworkObject did not get migrated below the NetworkBehaviour!");
// Cleanup
Object.DestroyImmediate(gameObject);
}
private bool ValidateComponentIndices(ComponentIndices previous, ComponentIndices current)
{
if (previous.NetworkObjectIndex != current.NetworkObjectIndex && previous.NetworkBehaviourIndex != current.NetworkBehaviourIndex)
{
if (current.NetworkObjectIndex < previous.NetworkObjectIndex && current.NetworkObjectIndex < current.NetworkBehaviourIndex)
{
return true;
}
}
return false;
}
private ComponentIndices GetIndices(GameObject gameObject)
{
// Get the index/order values for the added NetworkBehaviour and NetworkObject
var components = gameObject.GetComponents<MonoBehaviour>();
var componentIndices = new ComponentIndices()
{
NetworkObjectIndex = -1,
NetworkBehaviourIndex = -1
};
for (int i = 0; i < components.Length; i++)
{
if (componentIndices.NetworkObjectIndex != -1 && componentIndices.NetworkBehaviourIndex != -1)
{
break;
}
var component = components[i];
var networkObjectComponent = component as NetworkObject;
if (networkObjectComponent != null)
{
componentIndices.NetworkObjectIndex = i;
continue;
}
var networkBehaviourComponent = component as EmptyNetworkBehaviour;
if (networkBehaviourComponent != null)
{
componentIndices.NetworkBehaviourIndex = i;
continue;
}
}
return componentIndices;
}
private struct ComponentIndices
{
public int NetworkObjectIndex;
public int NetworkBehaviourIndex;
}
public class EmptyNetworkBehaviour : NetworkBehaviour
{
}
public class EmptyMonoBehaviour : MonoBehaviour
{
}
}
}