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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user