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.10.0] - 2024-07-22

### Added

- Added `NetworkBehaviour.OnNetworkPreSpawn` and `NetworkBehaviour.OnNetworkPostSpawn` methods that provide the ability to handle pre and post spawning actions during the `NetworkObject` spawn sequence. (#2906)
- Added a client-side only `NetworkBehaviour.OnNetworkSessionSynchronized` convenience method that is invoked on all `NetworkBehaviour`s after a newly joined client has finished synchronizing with the network session in progress. (#2906)
- Added `NetworkBehaviour.OnInSceneObjectsSpawned` convenience method that is invoked when all in-scene `NetworkObject`s have been spawned after a scene has been loaded or upon a host or server starting. (#2906)

### Fixed

- 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. (#2980)
- Fixed issue where `NetworkManager.ScenesLoaded` was not being updated if `PostSynchronizationSceneUnloading` was set and any loaded scenes not used during synchronization were unloaded.(#2977)
- 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. (#2953)
- Fixed issue with the client count not being correct on the host or server side when a client disconnects itself from a session. (#2941)
- Fixed issue with the host trying to send itself a message that it has connected when first starting up. (#2941)
- Fixed issue where in-scene placed NetworkObjects could be destroyed if a client disconnects early and/or before approval. (#2923)
- Fixed issue where `NetworkDeltaPosition` would "jitter" periodically if both unreliable delta state updates and half-floats were used together. (#2922)
- Fixed issue where `NetworkRigidbody2D` would not properly change body type based on the instance's authority when spawned. (#2916)
- Fixed issue where a `NetworkObject` component's associated `NetworkBehaviour` components would not be detected if scene loading is disabled in the editor and the currently loaded scene has in-scene placed `NetworkObject`s. (#2906)
- Fixed issue where an in-scene placed `NetworkObject` with `NetworkTransform` that is also parented under a `GameObject` would not properly synchronize when the parent `GameObject` had a world space position other than 0,0,0. (#2895)

### Changed
This commit is contained in:
Unity Technologies
2024-07-22 00:00:00 +00:00
parent 158f26b913
commit 896943c8bf
31 changed files with 729 additions and 107 deletions

View File

@@ -81,7 +81,7 @@ namespace Unity.Netcode.RuntimeTests
public IEnumerator ValidateApprovalTimeout()
{
// Delay for half of the wait period
yield return new WaitForSeconds(k_TestTimeoutPeriod * 0.5f);
yield return new WaitForSeconds(k_TestTimeoutPeriod * 0.25f);
// Verify we haven't received the time out message yet
NetcodeLogAssert.LogWasNotReceived(LogType.Log, m_ExpectedLogMessage);

View File

@@ -182,6 +182,9 @@ namespace Unity.Netcode.RuntimeTests
Assert.IsTrue(m_DisconnectedEvent[m_ServerNetworkManager].ClientId == m_ClientId, $"Expected ClientID {m_ClientId} but found ClientID {m_DisconnectedEvent[m_ServerNetworkManager].ClientId} for the server {nameof(NetworkManager)} disconnect event entry!");
Assert.IsTrue(m_DisconnectedEvent.ContainsKey(clientManager), $"Could not find the client {nameof(NetworkManager)} disconnect event entry!");
Assert.IsTrue(m_DisconnectedEvent[clientManager].ClientId == m_ClientId, $"Expected ClientID {m_ClientId} but found ClientID {m_DisconnectedEvent[m_ServerNetworkManager].ClientId} for the client {nameof(NetworkManager)} disconnect event entry!");
Assert.IsTrue(m_ServerNetworkManager.ConnectedClientsIds.Count == 1, $"Expected connected client identifiers count to be 1 but it was {m_ServerNetworkManager.ConnectedClientsIds.Count}!");
Assert.IsTrue(m_ServerNetworkManager.ConnectedClients.Count == 1, $"Expected connected client identifiers count to be 1 but it was {m_ServerNetworkManager.ConnectedClients.Count}!");
Assert.IsTrue(m_ServerNetworkManager.ConnectedClientsList.Count == 1, $"Expected connected client identifiers count to be 1 but it was {m_ServerNetworkManager.ConnectedClientsList.Count}!");
}
if (m_OwnerPersistence == OwnerPersistence.DestroyWithOwner)

View File

@@ -0,0 +1,137 @@
using System.Collections;
using NUnit.Framework;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
using UnityEngine.TestTools;
namespace Unity.Netcode.RuntimeTests
{
public class NetworkBehaviourPrePostSpawnTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 0;
private bool m_AllowServerToStart;
private GameObject m_PrePostSpawnObject;
protected override void OnServerAndClientsCreated()
{
m_PrePostSpawnObject = CreateNetworkObjectPrefab("PrePostSpawn");
// Reverse the order of the components to get inverted spawn sequence
m_PrePostSpawnObject.AddComponent<NetworkBehaviourPostSpawn>();
m_PrePostSpawnObject.AddComponent<NetworkBehaviourPreSpawn>();
base.OnServerAndClientsCreated();
}
public class NetworkBehaviourPreSpawn : NetworkBehaviour
{
public static int ValueToSet;
public bool OnNetworkPreSpawnCalled;
public bool NetworkVarValueMatches;
public NetworkVariable<int> TestNetworkVariable;
protected override void OnNetworkPreSpawn(ref NetworkManager networkManager)
{
OnNetworkPreSpawnCalled = true;
// If we are the server, then set the randomly generated value (1-200).
// Otherwise, just set the value to 0.
var val = networkManager.IsServer ? ValueToSet : 0;
// Instantiate the NetworkVariable as everyone read & owner write while also setting the value
TestNetworkVariable = new NetworkVariable<int>(val, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
base.OnNetworkPreSpawn(ref networkManager);
}
public override void OnNetworkSpawn()
{
// For both client and server this should match at this point
NetworkVarValueMatches = TestNetworkVariable.Value == ValueToSet;
base.OnNetworkSpawn();
}
}
public class NetworkBehaviourPostSpawn : NetworkBehaviour
{
public bool OnNetworkPostSpawnCalled;
private NetworkBehaviourPreSpawn m_NetworkBehaviourPreSpawn;
public int ValueSet;
public override void OnNetworkSpawn()
{
// Obtain the NetworkBehaviourPreSpawn component
// (could also do this during OnNetworkPreSpawn if we wanted)
m_NetworkBehaviourPreSpawn = GetComponent<NetworkBehaviourPreSpawn>();
base.OnNetworkSpawn();
}
protected override void OnNetworkPostSpawn()
{
OnNetworkPostSpawnCalled = true;
// We should be able to access the component we got during OnNetworkSpawn and all values should be set
// (i.e. OnNetworkSpawn run on all NetworkObject relative NetworkBehaviours)
ValueSet = m_NetworkBehaviourPreSpawn.TestNetworkVariable.Value;
base.OnNetworkPostSpawn();
}
}
protected override bool CanStartServerAndClients()
{
return m_AllowServerToStart;
}
protected override IEnumerator OnSetup()
{
m_AllowServerToStart = false;
return base.OnSetup();
}
protected override void OnNewClientCreated(NetworkManager networkManager)
{
networkManager.NetworkConfig.Prefabs = m_ServerNetworkManager.NetworkConfig.Prefabs;
base.OnNewClientCreated(networkManager);
}
/// <summary>
/// This validates that pre spawn can be used to instantiate and assign a NetworkVariable (or other prespawn tasks)
/// which can be useful for assigning a NetworkVariable value on the server side when the NetworkVariable has owner write permissions.
/// This also assures that duruing post spawn all associated NetworkBehaviours have run through the OnNetworkSpawn pass (i.e. OnNetworkSpawn order is not an issue)
/// </summary>
[UnityTest]
public IEnumerator OnNetworkPreAndPostSpawn()
{
m_AllowServerToStart = true;
NetworkBehaviourPreSpawn.ValueToSet = Random.Range(1, 200);
yield return StartServerAndClients();
yield return CreateAndStartNewClient();
// Spawn the object with the newly joined client as the owner
var serverInstance = SpawnObject(m_PrePostSpawnObject, m_ClientNetworkManagers[0]);
var serverNetworkObject = serverInstance.GetComponent<NetworkObject>();
var serverPreSpawn = serverInstance.GetComponent<NetworkBehaviourPreSpawn>();
var serverPostSpawn = serverInstance.GetComponent<NetworkBehaviourPostSpawn>();
yield return WaitForConditionOrTimeOut(() => s_GlobalNetworkObjects.ContainsKey(m_ClientNetworkManagers[0].LocalClientId)
&& s_GlobalNetworkObjects[m_ClientNetworkManagers[0].LocalClientId].ContainsKey(serverNetworkObject.NetworkObjectId));
AssertOnTimeout($"Client-{m_ClientNetworkManagers[0].LocalClientId} failed to spawn {nameof(NetworkObject)} id-{serverNetworkObject.NetworkObjectId}!");
var clientNetworkObject = s_GlobalNetworkObjects[m_ClientNetworkManagers[0].LocalClientId][serverNetworkObject.NetworkObjectId];
var clientPreSpawn = clientNetworkObject.GetComponent<NetworkBehaviourPreSpawn>();
var clientPostSpawn = clientNetworkObject.GetComponent<NetworkBehaviourPostSpawn>();
Assert.IsTrue(serverPreSpawn.OnNetworkPreSpawnCalled, $"[Server-side] OnNetworkPreSpawn not invoked!");
Assert.IsTrue(clientPreSpawn.OnNetworkPreSpawnCalled, $"[Client-side] OnNetworkPreSpawn not invoked!");
Assert.IsTrue(serverPostSpawn.OnNetworkPostSpawnCalled, $"[Server-side] OnNetworkPostSpawn not invoked!");
Assert.IsTrue(clientPostSpawn.OnNetworkPostSpawnCalled, $"[Client-side] OnNetworkPostSpawn not invoked!");
Assert.IsTrue(serverPreSpawn.NetworkVarValueMatches, $"[Server-side][PreSpawn] Value {NetworkBehaviourPreSpawn.ValueToSet} does not match {serverPreSpawn.TestNetworkVariable.Value}!");
Assert.IsTrue(clientPreSpawn.NetworkVarValueMatches, $"[Client-side][PreSpawn] Value {NetworkBehaviourPreSpawn.ValueToSet} does not match {clientPreSpawn.TestNetworkVariable.Value}!");
Assert.IsTrue(serverPostSpawn.ValueSet == NetworkBehaviourPreSpawn.ValueToSet, $"[Server-side][PostSpawn] Value {NetworkBehaviourPreSpawn.ValueToSet} does not match {serverPostSpawn.ValueSet}!");
Assert.IsTrue(clientPostSpawn.ValueSet == NetworkBehaviourPreSpawn.ValueToSet, $"[Client-side][PostSpawn] Value {NetworkBehaviourPreSpawn.ValueToSet} does not match {clientPostSpawn.ValueSet}!");
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2263d66f6df15a7428d279dbdaba1519
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,5 +1,9 @@
using System.Collections;
using NUnit.Framework;
using Unity.Collections;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
using UnityEngine.TestTools;
namespace Unity.Netcode.RuntimeTests
{
@@ -30,5 +34,87 @@ namespace Unity.Netcode.RuntimeTests
networkManager.Shutdown();
Object.DestroyImmediate(gameObject);
}
[UnityTest]
public IEnumerator VerifyCustomMessageShutdownOrder()
{
Assert.True(NetcodeIntegrationTestHelpers.Create(1, out NetworkManager server, out NetworkManager[] clients));
bool isHost = false;
// Start server to cause initialization
NetcodeIntegrationTestHelpers.Start(isHost, server, clients);
// [Client-Side] Wait for a connection to the server
yield return NetcodeIntegrationTestHelpers.WaitForClientsConnected(clients, null, 512);
// [Host-Side] Check to make sure all clients are connected
yield return NetcodeIntegrationTestHelpers.WaitForClientsConnectedToServer(server, isHost ? (clients.Length + 1) : clients.Length, null, 512);
// Create a message to pass directly to the message handler. If we send the message its processed before we get a chance to shutdown
var dummySendData = new FastBufferWriter(128, Allocator.Temp);
dummySendData.WriteValueSafe("Dummy Data");
// make the message
var unnamedMessage = new UnnamedMessage
{
SendData = dummySendData
};
// make the message
using var serializedMessage = new FastBufferWriter(128, Allocator.Temp);
unnamedMessage.Serialize(serializedMessage, 0);
// Generate the full message
var messageHeader = new NetworkMessageHeader
{
MessageSize = (uint)serializedMessage.Length,
MessageType = server.MessageManager.GetMessageType(typeof(UnnamedMessage)),
};
var fullMessage = new FastBufferWriter(512, Allocator.Temp);
BytePacker.WriteValueBitPacked(fullMessage, messageHeader.MessageType);
BytePacker.WriteValueBitPacked(fullMessage, messageHeader.MessageSize);
fullMessage.WriteBytesSafe(serializedMessage.ToArray());
// Pack the message into a batch
var batchHeader = new NetworkBatchHeader
{
BatchCount = 1
};
var batchedMessage = new FastBufferWriter(1100, Allocator.Temp);
using (batchedMessage)
{
batchedMessage.TryBeginWrite(FastBufferWriter.GetWriteSize(batchHeader) +
FastBufferWriter.GetWriteSize(fullMessage));
batchedMessage.WriteValue(batchHeader);
batchedMessage.WriteBytesSafe(fullMessage.ToArray());
// Fill out the rest of the batch header
batchedMessage.Seek(0);
batchHeader = new NetworkBatchHeader
{
Magic = NetworkBatchHeader.MagicValue,
BatchSize = batchedMessage.Length,
BatchHash = XXHash.Hash64(fullMessage.ToArray()),
BatchCount = 1
};
batchedMessage.WriteValue(batchHeader);
// Handle the message as if it came from the server/client
server.MessageManager.HandleIncomingData(clients[0].LocalClientId, batchedMessage.ToArray(), 0);
foreach (var c in clients)
{
c.MessageManager.HandleIncomingData(server.LocalClientId, batchedMessage.ToArray(), 0);
}
}
// shutdown the network managher
NetcodeIntegrationTestHelpers.Destroy();
}
}
}

View File

@@ -751,11 +751,29 @@ namespace Unity.Netcode.RuntimeTests
base.OnAuthorityPushTransformState(ref networkTransformState);
}
public bool AuthorityMove;
public Vector3 DirectionToMove;
public float MoveSpeed;
protected override void Update()
{
if (CanCommitToTransform && AuthorityMove)
{
transform.position += DirectionToMove * MoveSpeed * Time.deltaTime;
}
base.Update();
}
public delegate void NonAuthorityReceivedTransformStateDelegateHandler(ref NetworkTransformState networkTransformState);
public event NonAuthorityReceivedTransformStateDelegateHandler NonAuthorityReceivedTransformState;
public bool StateUpdated { get; internal set; }
protected override void OnNetworkTransformStateUpdated(ref NetworkTransformState oldState, ref NetworkTransformState newState)
{
StateUpdated = true;
NonAuthorityReceivedTransformState?.Invoke(ref newState);
base.OnNetworkTransformStateUpdated(ref oldState, ref newState);
}

View File

@@ -310,5 +310,64 @@ namespace Unity.Netcode.RuntimeTests
Assert.True(Approximately(newScale, m_AuthoritativeTransform.transform.localScale), "Authoritative scale does not match!");
Assert.True(Approximately(newScale, m_NonAuthoritativeTransform.transform.localScale), "Non-Authoritative scale does not match!");
}
/// <summary>
/// Validates that the unreliable frame synchronization is correct on the
/// non-authority side when using half float precision.
/// </summary>
[Test]
public void UnreliableHalfPrecisionTest([Values] Interpolation interpolation)
{
var interpolate = interpolation != Interpolation.EnableInterpolate;
m_AuthoritativeTransform.Interpolate = interpolate;
m_NonAuthoritativeTransform.Interpolate = interpolate;
m_AuthoritativeTransform.UseHalfFloatPrecision = true;
m_NonAuthoritativeTransform.UseHalfFloatPrecision = true;
m_AuthoritativeTransform.UseUnreliableDeltas = true;
m_NonAuthoritativeTransform.UseUnreliableDeltas = true;
m_AuthoritativeTransform.AuthorityPushedTransformState += AuthorityPushedTransformState;
m_NonAuthoritativeTransform.NonAuthorityReceivedTransformState += NonAuthorityReceivedTransformState;
m_AuthoritativeTransform.MoveSpeed = 6.325f;
m_AuthoritativeTransform.AuthorityMove = true;
m_AuthoritativeTransform.DirectionToMove = GetRandomVector3(-1.0f, 1.0f);
// Iterate several times so the authority moves around enough where we get 10 frame synchs to compare against.
for (int i = 0; i < 10; i++)
{
m_AuthorityFrameSync = false;
m_NonAuthorityFrameSync = false;
VerboseDebug($"Starting with authority ({m_AuthoritativeTransform.transform.position}) and nonauthority({m_NonAuthoritativeTransform.transform.position})");
var success = WaitForConditionOrTimeOutWithTimeTravel(() => m_AuthorityFrameSync && m_NonAuthorityFrameSync, 320);
Assert.True(success, $"Timed out waiting for authority or nonauthority frame state synchronization!");
VerboseDebug($"Comparing authority ({m_AuthorityPosition}) with nonauthority({m_NonAuthorityPosition})");
Assert.True(Approximately(m_AuthorityPosition, m_NonAuthorityPosition), $"Non-Authoritative position {m_AuthorityPosition} does not match authortative position {m_NonAuthorityPosition}!");
}
m_AuthoritativeTransform.AuthorityMove = false;
m_AuthoritativeTransform.AuthorityPushedTransformState -= AuthorityPushedTransformState;
m_NonAuthoritativeTransform.NonAuthorityReceivedTransformState -= NonAuthorityReceivedTransformState;
}
private bool m_AuthorityFrameSync;
private Vector3 m_AuthorityPosition;
private bool m_NonAuthorityFrameSync;
private Vector3 m_NonAuthorityPosition;
private void AuthorityPushedTransformState(ref NetworkTransform.NetworkTransformState networkTransformState)
{
if (networkTransformState.UnreliableFrameSync)
{
m_AuthorityPosition = m_AuthoritativeTransform.GetSpaceRelativePosition();
m_AuthorityFrameSync = true;
}
}
private void NonAuthorityReceivedTransformState(ref NetworkTransform.NetworkTransformState networkTransformState)
{
if (networkTransformState.UnreliableFrameSync)
{
m_NonAuthorityPosition = networkTransformState.NetworkDeltaPosition.GetFullPosition();
m_NonAuthorityFrameSync = true;
}
}
}
}