Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Unity Technologies committed Jul 22, 2024
1 parent 158f26b commit 896943c
Show file tree
Hide file tree
Showing 31 changed files with 728 additions and 106 deletions.
24 changes: 23 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ All notable changes to this project will be documented in this file.
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


## [1.9.1] - 2024-04-18

Expand All @@ -16,7 +39,6 @@ Additional documentation and release notes are available at [Multiplayer Documen
- Added virtual method NetworkVariableBase.OnInitialize() which can be used by NetworkVariable subclasses to add initialization code (#2820)
- Added virtual method NetworkVariableBase.Update(), which is called once per frame to support behaviors such as interpolation between an anticipated value and an authoritative one. (#2820)
- Added NetworkTime.TickWithPartial, which represents the current tick as a double that includes the fractional/partial tick value. (#2820)
- Added NetworkTickSystem.AnticipationTick, which can be helpful with implementation of client anticipation. This value represents the tick the current local client was at at the beginning of the most recent network round trip, which enables it to correlate server update ticks with the client tick that may have triggered them. (#2820)
- `NetworkVariable` now includes built-in support for `NativeHashSet`, `NativeHashMap`, `List`, `HashSet`, and `Dictionary` (#2813)
- `NetworkVariable` now includes delta compression for collection values (`NativeList`, `NativeArray`, `NativeHashSet`, `NativeHashMap`, `List`, `HashSet`, `Dictionary`, and `FixedString` types) to save bandwidth by only sending the values that changed. (Note: For `NativeList`, `NativeArray`, and `List`, this algorithm works differently than that used in `NetworkList`. This algorithm will use less bandwidth for "set" and "add" operations, but `NetworkList` is more bandwidth-efficient if you are performing frequent "insert" operations.) (#2813)
- `UserNetworkVariableSerialization` now has optional callbacks for `WriteDelta` and `ReadDelta`. If both are provided, they will be used for all serialization operations on NetworkVariables of that type except for the first one for each client. If either is missing, the existing `Write` and `Read` will always be used. (#2813)
Expand Down
8 changes: 6 additions & 2 deletions Components/NetworkDeltaPosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ public struct NetworkDeltaPosition : INetworkSerializable
/// </summary>
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
HalfVector3.NetworkSerialize(serializer);
if (SynchronizeBase)
if (!SynchronizeBase)
{
HalfVector3.NetworkSerialize(serializer);
}
else
{
serializer.SerializeValue(ref DeltaPosition);
serializer.SerializeValue(ref CurrentBasePosition);
}
}
Expand Down
89 changes: 57 additions & 32 deletions Components/NetworkRigidbody2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,76 +12,101 @@ namespace Unity.Netcode.Components
[AddComponentMenu("Netcode/Network Rigidbody 2D")]
public class NetworkRigidbody2D : NetworkBehaviour
{
/// <summary>
/// Determines if we are server (true) or owner (false) authoritative
/// </summary>
private bool m_IsServerAuthoritative;

private Rigidbody2D m_Rigidbody;
private NetworkTransform m_NetworkTransform;

private bool m_OriginalKinematic;
private RigidbodyInterpolation2D m_OriginalInterpolation;

// Used to cache the authority state of this rigidbody during the last frame
private bool m_IsAuthority;

private void Awake()
{
m_NetworkTransform = GetComponent<NetworkTransform>();
m_IsServerAuthoritative = m_NetworkTransform.IsServerAuthoritative();

SetupRigidBody();
}

/// <summary>
/// Gets a bool value indicating whether this <see cref="NetworkRigidbody2D"/> on this peer currently holds authority.
/// If the current <see cref="NetworkTransform"/> has authority,
/// then use the <see cref="Rigidbody2D"/> interpolation strategy,
/// if the <see cref="NetworkTransform"/> is handling interpolation,
/// set interpolation to none on the <see cref="Rigidbody2D"/>
/// <br/>
/// Turn off physics for the rigid body until spawned, otherwise
/// clients can run fixed update before the first
/// full <see cref="NetworkTransform"/> update
/// </summary>
private bool HasAuthority => m_NetworkTransform.CanCommitToTransform;

private void Awake()
private void SetupRigidBody()
{
m_Rigidbody = GetComponent<Rigidbody2D>();
m_NetworkTransform = GetComponent<NetworkTransform>();
m_OriginalInterpolation = m_Rigidbody.interpolation;

m_Rigidbody.interpolation = m_IsAuthority ? m_OriginalInterpolation : (m_NetworkTransform.Interpolate ? RigidbodyInterpolation2D.None : m_OriginalInterpolation);
// Turn off physics for the rigid body until spawned, otherwise
// clients can run fixed update before the first full
// NetworkTransform update
m_Rigidbody.isKinematic = true;
}

private void FixedUpdate()
/// <summary>
/// For owner authoritative (i.e. ClientNetworkTransform)
/// we adjust our authority when we gain ownership
/// </summary>
public override void OnGainedOwnership()
{
UpdateOwnershipAuthority();
}

/// <summary>
/// For owner authoritative(i.e. ClientNetworkTransform)
/// we adjust our authority when we have lost ownership
/// </summary>
public override void OnLostOwnership()
{
if (IsSpawned)
{
if (HasAuthority != m_IsAuthority)
{
m_IsAuthority = HasAuthority;
UpdateRigidbodyKinematicMode();
}
}
UpdateOwnershipAuthority();
}

// Puts the rigidbody in a kinematic non-interpolated mode on everyone but the server.
private void UpdateRigidbodyKinematicMode()
/// <summary>
/// Sets the authority differently depending upon
/// whether it is server or owner authoritative
/// </summary>
private void UpdateOwnershipAuthority()
{
if (m_IsAuthority == false)
if (m_IsServerAuthoritative)
{
m_OriginalKinematic = m_Rigidbody.isKinematic;
m_Rigidbody.isKinematic = true;

m_OriginalInterpolation = m_Rigidbody.interpolation;
// Set interpolation to none, the NetworkTransform component interpolates the position of the object.
m_Rigidbody.interpolation = RigidbodyInterpolation2D.None;
m_IsAuthority = NetworkManager.IsServer;
}
else
{
// Resets the rigidbody back to it's non replication only state. Happens on shutdown and when authority is lost
m_Rigidbody.isKinematic = m_OriginalKinematic;
m_Rigidbody.interpolation = m_OriginalInterpolation;
m_IsAuthority = IsOwner;
}

// If you have authority then you are not kinematic
m_Rigidbody.isKinematic = !m_IsAuthority;

// Set interpolation of the Rigidbody2D based on authority
// With authority: let local transform handle interpolation
// Without authority: let the NetworkTransform handle interpolation
m_Rigidbody.interpolation = m_IsAuthority ? m_OriginalInterpolation : RigidbodyInterpolation2D.None;
}

/// <inheritdoc />
public override void OnNetworkSpawn()
{
m_IsAuthority = HasAuthority;
m_OriginalKinematic = m_Rigidbody.isKinematic;
m_OriginalInterpolation = m_Rigidbody.interpolation;
UpdateRigidbodyKinematicMode();
UpdateOwnershipAuthority();
}

/// <inheritdoc />
public override void OnNetworkDespawn()
{
UpdateRigidbodyKinematicMode();
UpdateOwnershipAuthority();
}
}
}
Expand Down
32 changes: 20 additions & 12 deletions Components/NetworkTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,15 @@ private bool ApplyTransformToNetworkStateWithInfo(ref NetworkTransformState netw
var scale = transformToUse.localScale;
networkState.IsSynchronizing = isSynchronization;

// All of the checks below, up to the delta position checking portion, are to determine if the
// authority changed a property during runtime that requires a full synchronizing.
if (InLocalSpace != networkState.InLocalSpace)
{
networkState.InLocalSpace = InLocalSpace;
isDirty = true;
networkState.IsTeleportingNextFrame = true;
}

// Check for parenting when synchronizing and/or teleporting
if (isSynchronization || networkState.IsTeleportingNextFrame)
{
Expand All @@ -1691,11 +1700,13 @@ private bool ApplyTransformToNetworkStateWithInfo(ref NetworkTransformState netw
// values are applied.
var hasParentNetworkObject = false;

var parentNetworkObject = (NetworkObject)null;

// If the NetworkObject belonging to this NetworkTransform instance has a parent
// (i.e. this handles nested NetworkTransforms under a parent at some layer above)
if (NetworkObject.transform.parent != null)
{
var parentNetworkObject = NetworkObject.transform.parent.GetComponent<NetworkObject>();
parentNetworkObject = NetworkObject.transform.parent.GetComponent<NetworkObject>();

// In-scene placed NetworkObjects parented under a GameObject with no
// NetworkObject preserve their lossyScale when synchronizing.
Expand All @@ -1716,28 +1727,25 @@ private bool ApplyTransformToNetworkStateWithInfo(ref NetworkTransformState netw
// the NetworkTransform is using world or local space synchronization.
// WorldPositionStays: (always use world space)
// !WorldPositionStays: (always use local space)
if (isSynchronization)
// Exception: If it is an in-scene placed NetworkObject and it is parented under a GameObject
// then always use local space unless AutoObjectParentSync is disabled and the NetworkTransform
// is synchronizing in world space.
if (isSynchronization && networkState.IsParented)
{
if (NetworkObject.WorldPositionStays())
var parentedUnderGameObject = NetworkObject.transform.parent != null && !parentNetworkObject && NetworkObject.IsSceneObject.Value;
if (NetworkObject.WorldPositionStays() && (!parentedUnderGameObject || (parentedUnderGameObject && !NetworkObject.AutoObjectParentSync && !InLocalSpace)))
{
position = transformToUse.position;
networkState.InLocalSpace = false;
}
else
{
position = transformToUse.localPosition;
networkState.InLocalSpace = true;
}
}
}

// All of the checks below, up to the delta position checking portion, are to determine if the
// authority changed a property during runtime that requires a full synchronizing.
if (InLocalSpace != networkState.InLocalSpace)
{
networkState.InLocalSpace = InLocalSpace;
isDirty = true;
networkState.IsTeleportingNextFrame = true;
}

if (Interpolate != networkState.UseInterpolation)
{
networkState.UseInterpolation = Interpolate;
Expand Down
51 changes: 33 additions & 18 deletions Runtime/Connection/NetworkConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,11 @@ internal void InvokeOnClientConnectedCallback(ulong clientId)
{
continue;
}

peerClientIds[idx] = peerId;
++idx;
if (peerClientIds.Length > idx)
{
peerClientIds[idx] = peerId;
++idx;
}
}

try
Expand Down Expand Up @@ -491,24 +493,32 @@ internal void DisconnectEventHandler(ulong transportClientId)
// 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)
Expand Down Expand Up @@ -900,9 +910,14 @@ internal NetworkClient AddClient(ulong clientId)

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;
}

Expand Down
Loading

0 comments on commit 896943c

Please sign in to comment.