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:
@@ -104,9 +104,11 @@ namespace Unity.Netcode
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
peerClientIds[idx] = peerId;
|
||||
++idx;
|
||||
if (peerClientIds.Length > idx)
|
||||
{
|
||||
peerClientIds[idx] = peerId;
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
@@ -491,24 +493,32 @@ namespace Unity.Netcode
|
||||
// Process the incoming message queue so that we get everything from the server disconnecting us or, if we are the server, so we got everything from that client.
|
||||
MessageManager.ProcessIncomingMessageQueue();
|
||||
|
||||
InvokeOnClientDisconnectCallback(clientId);
|
||||
|
||||
if (LocalClient.IsHost)
|
||||
{
|
||||
InvokeOnPeerDisconnectedCallback(clientId);
|
||||
}
|
||||
|
||||
if (LocalClient.IsServer)
|
||||
{
|
||||
// We need to process the disconnection before notifying
|
||||
OnClientDisconnectFromServer(clientId);
|
||||
|
||||
// Now notify the client has disconnected
|
||||
InvokeOnClientDisconnectCallback(clientId);
|
||||
|
||||
if (LocalClient.IsHost)
|
||||
{
|
||||
InvokeOnPeerDisconnectedCallback(clientId);
|
||||
}
|
||||
}
|
||||
else // As long as we are not in the middle of a shutdown
|
||||
if (!NetworkManager.ShutdownInProgress)
|
||||
else
|
||||
{
|
||||
// We must pass true here and not process any sends messages as we are no longer connected.
|
||||
// Otherwise, attempting to process messages here can cause an exception within UnityTransport
|
||||
// as the client ID is no longer valid.
|
||||
NetworkManager.Shutdown(true);
|
||||
// Notify local client of disconnection
|
||||
InvokeOnClientDisconnectCallback(clientId);
|
||||
|
||||
// As long as we are not in the middle of a shutdown
|
||||
if (!NetworkManager.ShutdownInProgress)
|
||||
{
|
||||
// We must pass true here and not process any sends messages as we are no longer connected.
|
||||
// Otherwise, attempting to process messages here can cause an exception within UnityTransport
|
||||
// as the client ID is no longer valid.
|
||||
NetworkManager.Shutdown(true);
|
||||
}
|
||||
}
|
||||
|
||||
if (NetworkManager.IsServer)
|
||||
@@ -900,9 +910,14 @@ namespace Unity.Netcode
|
||||
|
||||
ConnectedClients.Add(clientId, networkClient);
|
||||
ConnectedClientsList.Add(networkClient);
|
||||
var message = new ClientConnectedMessage { ClientId = clientId };
|
||||
NetworkManager.MessageManager.SendMessage(ref message, NetworkDelivery.ReliableFragmentedSequenced, ConnectedClientIds);
|
||||
ConnectedClientIds.Add(clientId);
|
||||
// Host should not send this message to itself
|
||||
if (clientId != NetworkManager.ServerClientId)
|
||||
{
|
||||
var message = new ClientConnectedMessage { ClientId = clientId };
|
||||
NetworkManager.MessageManager.SendMessage(ref message, NetworkDelivery.ReliableFragmentedSequenced, ConnectedClientIds);
|
||||
}
|
||||
|
||||
return networkClient;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace Unity.Netcode
|
||||
// RuntimeAccessModifiersILPP will make this `public`
|
||||
internal static readonly Dictionary<Type, Dictionary<uint, RpcReceiveHandler>> __rpc_func_table = new Dictionary<Type, Dictionary<uint, RpcReceiveHandler>>();
|
||||
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
|
||||
// RuntimeAccessModifiersILPP will make this `public`
|
||||
internal static readonly Dictionary<Type, Dictionary<uint, string>> __rpc_name_table = new Dictionary<Type, Dictionary<uint, string>>();
|
||||
#endif
|
||||
@@ -123,7 +123,7 @@ namespace Unity.Netcode
|
||||
}
|
||||
|
||||
bufferWriter.Dispose();
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
|
||||
if (__rpc_name_table[GetType()].TryGetValue(rpcMethodId, out var rpcMethodName))
|
||||
{
|
||||
NetworkManager.NetworkMetrics.TrackRpcSent(
|
||||
@@ -254,7 +254,7 @@ namespace Unity.Netcode
|
||||
}
|
||||
|
||||
bufferWriter.Dispose();
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
|
||||
if (__rpc_name_table[GetType()].TryGetValue(rpcMethodId, out var rpcMethodName))
|
||||
{
|
||||
if (clientRpcParams.Send.TargetClientIds != null)
|
||||
@@ -615,16 +615,70 @@ namespace Unity.Netcode
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets called after the <see cref="NetworkObject"/> is spawned. No NetworkBehaviours associated with the NetworkObject will have had <see cref="OnNetworkSpawn"/> invoked yet.
|
||||
/// A reference to <see cref="NetworkManager"/> is passed in as a parameter to determine the context of execution (IsServer/IsClient)
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <param name="networkManager">a ref to the <see cref="NetworkManager"/> since this is not yet set on the <see cref="NetworkBehaviour"/></param>
|
||||
/// The <see cref="NetworkBehaviour"/> will not have anything assigned to it at this point in time.
|
||||
/// Settings like ownership, NetworkBehaviourId, NetworkManager, and most other spawn related properties will not be set.
|
||||
/// This can be used to handle things like initializing/instantiating a NetworkVariable or the like.
|
||||
/// </remarks>
|
||||
protected virtual void OnNetworkPreSpawn(ref NetworkManager networkManager) { }
|
||||
|
||||
/// <summary>
|
||||
/// Gets called when the <see cref="NetworkObject"/> gets spawned, message handlers are ready to be registered and the network is setup.
|
||||
/// </summary>
|
||||
public virtual void OnNetworkSpawn() { }
|
||||
|
||||
/// <summary>
|
||||
/// Gets called after the <see cref="NetworkObject"/> is spawned. All NetworkBehaviours associated with the NetworkObject will have had <see cref="OnNetworkSpawn"/> invoked.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Will be invoked on each <see cref="NetworkBehaviour"/> associated with the <see cref="NetworkObject"/> being spawned.
|
||||
/// All associated <see cref="NetworkBehaviour"/> components will have had <see cref="OnNetworkSpawn"/> invoked on the spawned <see cref="NetworkObject"/>.
|
||||
/// </remarks>
|
||||
protected virtual void OnNetworkPostSpawn() { }
|
||||
|
||||
/// <summary>
|
||||
/// [Client-Side Only]
|
||||
/// When a new client joins it is synchronized with all spawned NetworkObjects and scenes loaded for the session joined. At the end of the synchronization process, when all
|
||||
/// <see cref="NetworkObject"/>s and scenes (if scene management is enabled) have finished synchronizing, all NetworkBehaviour components associated with spawned <see cref="NetworkObject"/>s
|
||||
/// will have this method invoked.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This can be used to handle post synchronization actions where you might need to access a different NetworkObject and/or NetworkBehaviour not local to the current NetworkObject context.
|
||||
/// This is only invoked on clients during a client-server network topology session.
|
||||
/// </remarks>
|
||||
protected virtual void OnNetworkSessionSynchronized() { }
|
||||
|
||||
/// <summary>
|
||||
/// [Client & Server Side]
|
||||
/// When a scene is loaded an in-scene placed NetworkObjects are all spawned, this method is invoked on all of the newly spawned in-scene placed NetworkObjects.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This can be used to handle post scene loaded actions for in-scene placed NetworkObjcts where you might need to access a different NetworkObject and/or NetworkBehaviour not local to the current NetworkObject context.
|
||||
/// </remarks>
|
||||
protected virtual void OnInSceneObjectsSpawned() { }
|
||||
|
||||
/// <summary>
|
||||
/// Gets called when the <see cref="NetworkObject"/> gets despawned. Is called both on the server and clients.
|
||||
/// </summary>
|
||||
public virtual void OnNetworkDespawn() { }
|
||||
|
||||
internal void NetworkPreSpawn(ref NetworkManager networkManager)
|
||||
{
|
||||
try
|
||||
{
|
||||
OnNetworkPreSpawn(ref networkManager);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogException(e);
|
||||
}
|
||||
}
|
||||
|
||||
internal void InternalOnNetworkSpawn()
|
||||
{
|
||||
IsSpawned = true;
|
||||
@@ -653,6 +707,42 @@ namespace Unity.Netcode
|
||||
}
|
||||
}
|
||||
|
||||
internal void NetworkPostSpawn()
|
||||
{
|
||||
try
|
||||
{
|
||||
OnNetworkPostSpawn();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogException(e);
|
||||
}
|
||||
}
|
||||
|
||||
internal void NetworkSessionSynchronized()
|
||||
{
|
||||
try
|
||||
{
|
||||
OnNetworkSessionSynchronized();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogException(e);
|
||||
}
|
||||
}
|
||||
|
||||
internal void InSceneNetworkObjectsSpawned()
|
||||
{
|
||||
try
|
||||
{
|
||||
OnInSceneObjectsSpawned();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogException(e);
|
||||
}
|
||||
}
|
||||
|
||||
internal void InternalOnNetworkDespawn()
|
||||
{
|
||||
IsSpawned = false;
|
||||
@@ -681,7 +771,7 @@ namespace Unity.Netcode
|
||||
/// <summary>
|
||||
/// Invoked on all clients, override this method to be notified of any
|
||||
/// ownership changes (even if the instance was niether the previous or
|
||||
/// newly assigned current owner).
|
||||
/// newly assigned current owner).
|
||||
/// </summary>
|
||||
/// <param name="previous">the previous owner</param>
|
||||
/// <param name="current">the current owner</param>
|
||||
@@ -742,7 +832,7 @@ namespace Unity.Netcode
|
||||
#pragma warning restore IDE1006 // restore naming rule violation check
|
||||
{
|
||||
__rpc_func_table[GetType()][hash] = handler;
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
|
||||
__rpc_name_table[GetType()][hash] = rpcMethodName;
|
||||
#endif
|
||||
}
|
||||
@@ -768,7 +858,7 @@ namespace Unity.Netcode
|
||||
if (!__rpc_func_table.ContainsKey(GetType()))
|
||||
{
|
||||
__rpc_func_table[GetType()] = new Dictionary<uint, RpcReceiveHandler>();
|
||||
#if UNITY_EDITOR || DEVELOPMENT_BUILD
|
||||
#if UNITY_EDITOR || DEVELOPMENT_BUILD || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
|
||||
__rpc_name_table[GetType()] = new Dictionary<uint, string>();
|
||||
#endif
|
||||
__initializeRpcs();
|
||||
@@ -1223,9 +1313,9 @@ namespace Unity.Netcode
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when the <see cref="GameObject"/> the <see cref="NetworkBehaviour"/> is attached to.
|
||||
/// NOTE: If you override this, you will want to always invoke this base class version of this
|
||||
/// <see cref="OnDestroy"/> method!!
|
||||
/// Invoked when the <see cref="GameObject"/> the <see cref="NetworkBehaviour"/> is attached to is destroyed.
|
||||
/// NOTE: If you override this, you should invoke this base class version of this
|
||||
/// <see cref="OnDestroy"/> method.
|
||||
/// </summary>
|
||||
public virtual void OnDestroy()
|
||||
{
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace Unity.Netcode
|
||||
// RuntimeAccessModifiersILPP will make this `public`
|
||||
internal static readonly Dictionary<uint, RpcReceiveHandler> __rpc_func_table = new Dictionary<uint, RpcReceiveHandler>();
|
||||
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
|
||||
// RuntimeAccessModifiersILPP will make this `public`
|
||||
internal static readonly Dictionary<uint, string> __rpc_name_table = new Dictionary<uint, string>();
|
||||
#endif
|
||||
@@ -463,20 +463,21 @@ namespace Unity.Netcode
|
||||
public event Action OnServerStarted = null;
|
||||
|
||||
/// <summary>
|
||||
/// The callback to invoke once the local client is ready
|
||||
/// The callback to invoke once the local client is ready.
|
||||
/// </summary>
|
||||
public event Action OnClientStarted = null;
|
||||
|
||||
/// <summary>
|
||||
/// This callback is invoked once the local server is stopped.
|
||||
/// </summary>
|
||||
/// <remarks>The bool parameter will be set to true when stopping a host instance and false when stopping a server instance.</remarks>
|
||||
/// <param name="arg1">The first parameter of this event will be set to <see cref="true"/> when stopping a host instance and <see cref="false"/> when stopping a server instance.</param>
|
||||
public event Action<bool> OnServerStopped = null;
|
||||
|
||||
/// <summary>
|
||||
/// The callback to invoke once the local client stops
|
||||
/// The callback to invoke once the local client stops.
|
||||
/// </summary>
|
||||
/// <remarks>The parameter states whether the client was running in host mode</remarks>
|
||||
/// <remarks>The bool parameter will be set to true when stopping a host client and false when stopping a standard client instance.</remarks>
|
||||
/// <param name="arg1">The first parameter of this event will be set to <see cref="true"/> when stopping the host client and <see cref="false"/> when stopping a standard client instance.</param>
|
||||
public event Action<bool> OnClientStopped = null;
|
||||
|
||||
@@ -1187,7 +1188,6 @@ namespace Unity.Netcode
|
||||
|
||||
// Everything is shutdown in the order of their dependencies
|
||||
DeferredMessageManager?.CleanupAllTriggers();
|
||||
CustomMessagingManager = null;
|
||||
|
||||
RpcTarget?.Dispose();
|
||||
RpcTarget = null;
|
||||
@@ -1198,6 +1198,8 @@ namespace Unity.Netcode
|
||||
// Shutdown connection manager last which shuts down transport
|
||||
ConnectionManager.Shutdown();
|
||||
|
||||
CustomMessagingManager = null;
|
||||
|
||||
if (MessageManager != null)
|
||||
{
|
||||
MessageManager.Dispose();
|
||||
|
||||
@@ -512,6 +512,7 @@ namespace Unity.Netcode
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
m_ChildNetworkBehaviours = null;
|
||||
SetCachedParent(transform.parent);
|
||||
SceneOrigin = gameObject.scene;
|
||||
}
|
||||
@@ -1360,6 +1361,18 @@ namespace Unity.Netcode
|
||||
}
|
||||
}
|
||||
|
||||
internal void InvokeBehaviourNetworkPreSpawn()
|
||||
{
|
||||
var networkManager = NetworkManager;
|
||||
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
|
||||
{
|
||||
if (ChildNetworkBehaviours[i].gameObject.activeInHierarchy)
|
||||
{
|
||||
ChildNetworkBehaviours[i].NetworkPreSpawn(ref networkManager);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void InvokeBehaviourNetworkSpawn()
|
||||
{
|
||||
NetworkManager.SpawnManager.UpdateOwnershipTable(this, OwnerClientId);
|
||||
@@ -1384,6 +1397,42 @@ namespace Unity.Netcode
|
||||
}
|
||||
}
|
||||
|
||||
internal void InvokeBehaviourNetworkPostSpawn()
|
||||
{
|
||||
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
|
||||
{
|
||||
if (ChildNetworkBehaviours[i].gameObject.activeInHierarchy)
|
||||
{
|
||||
ChildNetworkBehaviours[i].NetworkPostSpawn();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal void InternalNetworkSessionSynchronized()
|
||||
{
|
||||
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
|
||||
{
|
||||
if (ChildNetworkBehaviours[i].gameObject.activeInHierarchy)
|
||||
{
|
||||
ChildNetworkBehaviours[i].NetworkSessionSynchronized();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void InternalInSceneNetworkObjectsSpawned()
|
||||
{
|
||||
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
|
||||
{
|
||||
if (ChildNetworkBehaviours[i].gameObject.activeInHierarchy)
|
||||
{
|
||||
ChildNetworkBehaviours[i].InSceneNetworkObjectsSpawned();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal void InvokeBehaviourNetworkDespawn()
|
||||
{
|
||||
NetworkManager.SpawnManager.UpdateOwnershipTable(this, OwnerClientId, true);
|
||||
@@ -1886,6 +1935,9 @@ namespace Unity.Netcode
|
||||
// in order to be able to determine which NetworkVariables the client will be allowed to read.
|
||||
networkObject.OwnerClientId = sceneObject.OwnerClientId;
|
||||
|
||||
// Special Case: Invoke NetworkBehaviour.OnPreSpawn methods here before SynchronizeNetworkBehaviours
|
||||
networkObject.InvokeBehaviourNetworkPreSpawn();
|
||||
|
||||
// Synchronize NetworkBehaviours
|
||||
var bufferSerializer = new BufferSerializer<BufferSerializerReader>(new BufferSerializerReader(reader));
|
||||
networkObject.SynchronizeNetworkBehaviours(ref bufferSerializer, networkManager.LocalClientId);
|
||||
|
||||
@@ -170,6 +170,12 @@ namespace Unity.Netcode
|
||||
networkManager.IsConnectedClient = true;
|
||||
// When scene management is disabled we notify after everything is synchronized
|
||||
networkManager.ConnectionManager.InvokeOnClientConnectedCallback(context.SenderId);
|
||||
|
||||
// For convenience, notify all NetworkBehaviours that synchronization is complete.
|
||||
foreach (var networkObject in networkManager.SpawnManager.SpawnedObjectsList)
|
||||
{
|
||||
networkObject.InternalNetworkSessionSynchronized();
|
||||
}
|
||||
}
|
||||
|
||||
ConnectedClientIds.Dispose();
|
||||
|
||||
@@ -41,7 +41,7 @@ namespace Unity.Netcode
|
||||
|
||||
payload = new FastBufferReader(reader.GetUnsafePtrAtCurrentPosition(), Allocator.None, reader.Length - reader.Position);
|
||||
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
|
||||
if (NetworkBehaviour.__rpc_name_table[networkBehaviour.GetType()].TryGetValue(metadata.NetworkRpcMethodId, out var rpcMethodName))
|
||||
{
|
||||
networkManager.NetworkMetrics.TrackRpcReceived(
|
||||
|
||||
@@ -36,12 +36,12 @@ namespace Unity.Netcode
|
||||
|
||||
private protected void SendMessageToClient(NetworkBehaviour behaviour, ulong clientId, ref RpcMessage message, NetworkDelivery delivery)
|
||||
{
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
|
||||
var size =
|
||||
#endif
|
||||
behaviour.NetworkManager.MessageManager.SendMessage(ref message, delivery, clientId);
|
||||
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
|
||||
if (NetworkBehaviour.__rpc_name_table[behaviour.GetType()].TryGetValue(message.Metadata.NetworkRpcMethodId, out var rpcMethodName))
|
||||
{
|
||||
behaviour.NetworkManager.NetworkMetrics.TrackRpcSent(
|
||||
|
||||
@@ -46,7 +46,7 @@ namespace Unity.Netcode
|
||||
message.Handle(ref context);
|
||||
length = tempBuffer.Length;
|
||||
}
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
|
||||
if (NetworkBehaviour.__rpc_name_table[behaviour.GetType()].TryGetValue(message.Metadata.NetworkRpcMethodId, out var rpcMethodName))
|
||||
{
|
||||
behaviour.NetworkManager.NetworkMetrics.TrackRpcSent(
|
||||
|
||||
@@ -18,12 +18,12 @@ namespace Unity.Netcode
|
||||
internal override void Send(NetworkBehaviour behaviour, ref RpcMessage message, NetworkDelivery delivery, RpcParams rpcParams)
|
||||
{
|
||||
var proxyMessage = new ProxyMessage { Delivery = delivery, TargetClientIds = TargetClientIds.AsArray(), WrappedMessage = message };
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
|
||||
var size =
|
||||
#endif
|
||||
behaviour.NetworkManager.MessageManager.SendMessage(ref proxyMessage, delivery, NetworkManager.ServerClientId);
|
||||
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR
|
||||
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
|
||||
if (NetworkBehaviour.__rpc_name_table[behaviour.GetType()].TryGetValue(message.Metadata.NetworkRpcMethodId, out var rpcMethodName))
|
||||
{
|
||||
foreach (var clientId in TargetClientIds)
|
||||
|
||||
@@ -51,6 +51,7 @@ namespace Unity.Netcode
|
||||
#pragma warning restore IDE0001
|
||||
[Serializable]
|
||||
[GenerateSerializationForGenericParameter(0)]
|
||||
[GenerateSerializationForType(typeof(byte))]
|
||||
public class AnticipatedNetworkVariable<T> : NetworkVariableBase
|
||||
{
|
||||
[SerializeField]
|
||||
|
||||
@@ -9,6 +9,7 @@ namespace Unity.Netcode
|
||||
/// <typeparam name="T">the unmanaged type for <see cref="NetworkVariable{T}"/> </typeparam>
|
||||
[Serializable]
|
||||
[GenerateSerializationForGenericParameter(0)]
|
||||
[GenerateSerializationForType(typeof(byte))]
|
||||
public class NetworkVariable<T> : NetworkVariableBase
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -717,7 +717,7 @@ namespace Unity.Netcode
|
||||
writer.WriteValueSafe(value);
|
||||
return;
|
||||
}
|
||||
writer.WriteByte(0);
|
||||
writer.WriteByteSafe(0);
|
||||
BytePacker.WriteValuePacked(writer, value.Length);
|
||||
writer.WriteValueSafe(changes);
|
||||
unsafe
|
||||
@@ -766,7 +766,7 @@ namespace Unity.Netcode
|
||||
{
|
||||
if (changes.IsSet(i))
|
||||
{
|
||||
reader.ReadByte(out ptr[i]);
|
||||
reader.ReadByteSafe(out ptr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,6 +226,11 @@ namespace Unity.Netcode
|
||||
foreach (var sceneToUnload in m_ScenesToUnload)
|
||||
{
|
||||
SceneManager.UnloadSceneAsync(sceneToUnload);
|
||||
// Update the ScenesLoaded when we unload scenes
|
||||
if (sceneManager.ScenesLoaded.ContainsKey(sceneToUnload.handle))
|
||||
{
|
||||
sceneManager.ScenesLoaded.Remove(sceneToUnload.handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1688,6 +1688,17 @@ namespace Unity.Netcode
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var keyValuePairByGlobalObjectIdHash in ScenePlacedObjects)
|
||||
{
|
||||
foreach (var keyValuePairBySceneHandle in keyValuePairByGlobalObjectIdHash.Value)
|
||||
{
|
||||
if (!keyValuePairBySceneHandle.Value.IsPlayerObject)
|
||||
{
|
||||
keyValuePairBySceneHandle.Value.InternalInSceneNetworkObjectsSpawned();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add any despawned when spawned in-scene placed NetworkObjects to the scene event data
|
||||
sceneEventData.AddDespawnedInSceneNetworkObjects();
|
||||
|
||||
@@ -2142,6 +2153,12 @@ namespace Unity.Netcode
|
||||
|
||||
OnSynchronizeComplete?.Invoke(NetworkManager.LocalClientId);
|
||||
|
||||
// For convenience, notify all NetworkBehaviours that synchronization is complete.
|
||||
foreach (var networkObject in NetworkManager.SpawnManager.SpawnedObjectsList)
|
||||
{
|
||||
networkObject.InternalNetworkSessionSynchronized();
|
||||
}
|
||||
|
||||
EndSceneEvent(sceneEventId);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -722,7 +722,7 @@ namespace Unity.Netcode
|
||||
{
|
||||
// is not packed!
|
||||
InternalBuffer.ReadValueSafe(out ushort newObjectsCount);
|
||||
|
||||
var sceneObjects = new List<NetworkObject>();
|
||||
for (ushort i = 0; i < newObjectsCount; i++)
|
||||
{
|
||||
var sceneObject = new NetworkObject.SceneObject();
|
||||
@@ -734,10 +734,22 @@ namespace Unity.Netcode
|
||||
m_NetworkManager.SceneManager.SetTheSceneBeingSynchronized(sceneObject.NetworkSceneHandle);
|
||||
}
|
||||
|
||||
NetworkObject.AddSceneObject(sceneObject, InternalBuffer, m_NetworkManager);
|
||||
var networkObject = NetworkObject.AddSceneObject(sceneObject, InternalBuffer, m_NetworkManager);
|
||||
|
||||
if (sceneObject.IsSceneObject)
|
||||
{
|
||||
sceneObjects.Add(networkObject);
|
||||
}
|
||||
}
|
||||
// Now deserialize the despawned in-scene placed NetworkObjects list (if any)
|
||||
DeserializeDespawnedInScenePlacedNetworkObjects();
|
||||
|
||||
// Notify all newly spawned in-scene placed NetworkObjects that all in-scene placed
|
||||
// NetworkObjects have been spawned.
|
||||
foreach (var networkObject in sceneObjects)
|
||||
{
|
||||
networkObject.InternalInSceneNetworkObjectsSpawned();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -983,6 +995,15 @@ namespace Unity.Netcode
|
||||
}
|
||||
}
|
||||
|
||||
// Notify that all in-scene placed NetworkObjects have been spawned
|
||||
foreach (var networkObject in m_NetworkObjectsSync)
|
||||
{
|
||||
if (networkObject.IsSceneObject.HasValue && networkObject.IsSceneObject.Value)
|
||||
{
|
||||
networkObject.InternalInSceneNetworkObjectsSpawned();
|
||||
}
|
||||
}
|
||||
|
||||
// Now deserialize the despawned in-scene placed NetworkObjects list (if any)
|
||||
DeserializeDespawnedInScenePlacedNetworkObjects();
|
||||
|
||||
|
||||
@@ -5,7 +5,9 @@ namespace Unity.Netcode
|
||||
{
|
||||
/// <summary>
|
||||
/// A helper struct for serializing <see cref="NetworkBehaviour"/>s over the network. Can be used in RPCs and <see cref="NetworkVariable{T}"/>.
|
||||
/// Note: network ids get recycled by the NetworkManager after a while. So a reference pointing to
|
||||
/// Network IDs get recycled by the NetworkManager after a while, so using a NetworkBehaviourReference for too long may result in a
|
||||
/// different NetworkBehaviour being returned for the assigned NetworkBehaviourId. NetworkBehaviourReferences are best for short-term
|
||||
/// use when receieved via RPC or custom message, rather than for long-term references to NetworkBehaviours.
|
||||
/// </summary>
|
||||
public struct NetworkBehaviourReference : INetworkSerializable, IEquatable<NetworkBehaviourReference>
|
||||
{
|
||||
@@ -43,7 +45,7 @@ namespace Unity.Netcode
|
||||
/// <returns>True if the <see cref="NetworkBehaviour"/> was found; False if the <see cref="NetworkBehaviour"/> was not found. This can happen if the corresponding <see cref="NetworkObject"/> has not been spawned yet. you can try getting the reference at a later point in time.</returns>
|
||||
public bool TryGet(out NetworkBehaviour networkBehaviour, NetworkManager networkManager = null)
|
||||
{
|
||||
networkBehaviour = GetInternal(this, null);
|
||||
networkBehaviour = GetInternal(this, networkManager);
|
||||
return networkBehaviour != null;
|
||||
}
|
||||
|
||||
@@ -56,7 +58,7 @@ namespace Unity.Netcode
|
||||
/// <returns>True if the <see cref="NetworkBehaviour"/> was found; False if the <see cref="NetworkBehaviour"/> was not found. This can happen if the corresponding <see cref="NetworkObject"/> has not been spawned yet. you can try getting the reference at a later point in time.</returns>
|
||||
public bool TryGet<T>(out T networkBehaviour, NetworkManager networkManager = null) where T : NetworkBehaviour
|
||||
{
|
||||
networkBehaviour = GetInternal(this, null) as T;
|
||||
networkBehaviour = GetInternal(this, networkManager) as T;
|
||||
return networkBehaviour != null;
|
||||
}
|
||||
|
||||
|
||||
@@ -531,20 +531,27 @@ namespace Unity.Netcode
|
||||
networkObject.DestroyWithScene = sceneObject.DestroyWithScene;
|
||||
networkObject.NetworkSceneHandle = sceneObject.NetworkSceneHandle;
|
||||
|
||||
|
||||
var nonNetworkObjectParent = false;
|
||||
// SPECIAL CASE FOR IN-SCENE PLACED: (only when the parent has a NetworkObject)
|
||||
// This is a special case scenario where a late joining client has joined and loaded one or
|
||||
// more scenes that contain nested in-scene placed NetworkObject children yet the server's
|
||||
// synchronization information does not indicate the NetworkObject in question has a parent.
|
||||
// Under this scenario, we want to remove the parent before spawning and setting the transform values.
|
||||
if (sceneObject.IsSceneObject && !sceneObject.HasParent && networkObject.transform.parent != null)
|
||||
if (sceneObject.IsSceneObject && networkObject.transform.parent != null)
|
||||
{
|
||||
var parentNetworkObject = networkObject.transform.parent.GetComponent<NetworkObject>();
|
||||
// if the in-scene placed NetworkObject has a parent NetworkObject but the synchronization information does not
|
||||
// include parenting, then we need to force the removal of that parent
|
||||
if (networkObject.transform.parent.GetComponent<NetworkObject>() != null)
|
||||
if (!sceneObject.HasParent && parentNetworkObject)
|
||||
{
|
||||
// remove the parent
|
||||
networkObject.ApplyNetworkParenting(true, true);
|
||||
}
|
||||
else if (sceneObject.HasParent && !parentNetworkObject)
|
||||
{
|
||||
nonNetworkObjectParent = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Set the transform unless we were spawned by a prefab handler
|
||||
@@ -554,7 +561,7 @@ namespace Unity.Netcode
|
||||
{
|
||||
// If world position stays is true or we have auto object parent synchronization disabled
|
||||
// then we want to apply the position and rotation values world space relative
|
||||
if (worldPositionStays || !networkObject.AutoObjectParentSync)
|
||||
if ((worldPositionStays && !nonNetworkObjectParent) || !networkObject.AutoObjectParentSync)
|
||||
{
|
||||
networkObject.transform.position = position;
|
||||
networkObject.transform.rotation = rotation;
|
||||
@@ -604,7 +611,12 @@ namespace Unity.Netcode
|
||||
return networkObject;
|
||||
}
|
||||
|
||||
// Ran on both server and client
|
||||
/// <summary>
|
||||
/// Invoked when spawning locally
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Pre and Post spawn methods *CAN* be invoked prior to invoking <see cref="SpawnNetworkObjectLocallyCommon"/>
|
||||
/// </remarks>
|
||||
internal void SpawnNetworkObjectLocally(NetworkObject networkObject, ulong networkId, bool sceneObject, bool playerObject, ulong ownerClientId, bool destroyWithScene)
|
||||
{
|
||||
if (networkObject == null)
|
||||
@@ -625,11 +637,21 @@ namespace Unity.Netcode
|
||||
Debug.LogError("Spawning NetworkObjects with nested NetworkObjects is only supported for scene objects. Child NetworkObjects will not be spawned over the network!");
|
||||
}
|
||||
}
|
||||
// Invoke NetworkBehaviour.OnPreSpawn methods
|
||||
networkObject.InvokeBehaviourNetworkPreSpawn();
|
||||
|
||||
SpawnNetworkObjectLocallyCommon(networkObject, networkId, sceneObject, playerObject, ownerClientId, destroyWithScene);
|
||||
|
||||
// Invoke NetworkBehaviour.OnPostSpawn methods
|
||||
networkObject.InvokeBehaviourNetworkPostSpawn();
|
||||
}
|
||||
|
||||
// Ran on both server and client
|
||||
/// <summary>
|
||||
/// Invoked from AddSceneObject
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// IMPORTANT: Pre spawn methods need to be invoked from within <see cref="NetworkObject.AddSceneObject"/>.
|
||||
/// </remarks>
|
||||
internal void SpawnNetworkObjectLocally(NetworkObject networkObject, in NetworkObject.SceneObject sceneObject, bool destroyWithScene)
|
||||
{
|
||||
if (networkObject == null)
|
||||
@@ -642,7 +664,11 @@ namespace Unity.Netcode
|
||||
throw new SpawnStateException("Object is already spawned");
|
||||
}
|
||||
|
||||
// Do not invoke Pre spawn here (SynchronizeNetworkBehaviours needs to be invoked prior to this)
|
||||
SpawnNetworkObjectLocallyCommon(networkObject, sceneObject.NetworkObjectId, sceneObject.IsSceneObject, sceneObject.IsPlayerObject, sceneObject.OwnerClientId, destroyWithScene);
|
||||
|
||||
// It is ok to invoke NetworkBehaviour.OnPostSpawn methods
|
||||
networkObject.InvokeBehaviourNetworkPostSpawn();
|
||||
}
|
||||
|
||||
private void SpawnNetworkObjectLocallyCommon(NetworkObject networkObject, ulong networkId, bool sceneObject, bool playerObject, ulong ownerClientId, bool destroyWithScene)
|
||||
@@ -859,7 +885,7 @@ namespace Unity.Netcode
|
||||
{
|
||||
// If it is an in-scene placed NetworkObject then just despawn and let it be destroyed when the scene
|
||||
// is unloaded. Otherwise, despawn and destroy it.
|
||||
var shouldDestroy = !(networkObjects[i].IsSceneObject != null && networkObjects[i].IsSceneObject.Value);
|
||||
var shouldDestroy = !(networkObjects[i].IsSceneObject == null || (networkObjects[i].IsSceneObject != null && networkObjects[i].IsSceneObject.Value));
|
||||
|
||||
// If we are going to destroy this NetworkObject, check for any in-scene placed children that need to be removed
|
||||
if (shouldDestroy)
|
||||
@@ -948,11 +974,16 @@ namespace Unity.Netcode
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
foreach (var networkObject in networkObjectsToSpawn)
|
||||
{
|
||||
SpawnNetworkObjectLocally(networkObject, GetNetworkObjectId(), true, false, networkObject.OwnerClientId, true);
|
||||
}
|
||||
|
||||
// Notify all in-scene placed NetworkObjects have been spawned
|
||||
foreach (var networkObject in networkObjectsToSpawn)
|
||||
{
|
||||
networkObject.InternalInSceneNetworkObjectsSpawned();
|
||||
}
|
||||
}
|
||||
|
||||
internal void OnDespawnObject(NetworkObject networkObject, bool destroyGameObject)
|
||||
|
||||
@@ -202,11 +202,12 @@ namespace Unity.Netcode.Transports.UTP
|
||||
set => m_MaxPacketQueueSize = value;
|
||||
}
|
||||
|
||||
[Tooltip("The maximum size of an unreliable payload that can be handled by the transport.")]
|
||||
[Tooltip("The maximum size of an unreliable payload that can be handled by the transport. The memory for MaxPayloadSize is allocated once per connection and is released when the connection is closed.")]
|
||||
[SerializeField]
|
||||
private int m_MaxPayloadSize = InitialMaxPayloadSize;
|
||||
|
||||
/// <summary>The maximum size of an unreliable payload that can be handled by the transport.</summary>
|
||||
/// <remarks>The memory for MaxPayloadSize is allocated once per connection and is released when the connection is closed.</remarks>
|
||||
public int MaxPayloadSize
|
||||
{
|
||||
get => m_MaxPayloadSize;
|
||||
|
||||
Reference in New Issue
Block a user