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
114 lines
4.0 KiB
C#
114 lines
4.0 KiB
C#
#if COM_UNITY_MODULES_PHYSICS2D
|
|
using UnityEngine;
|
|
|
|
namespace Unity.Netcode.Components
|
|
{
|
|
/// <summary>
|
|
/// NetworkRigidbody allows for the use of <see cref="Rigidbody2D"/> on network objects. By controlling the kinematic
|
|
/// mode of the rigidbody and disabling it on all peers but the authoritative one.
|
|
/// </summary>
|
|
[RequireComponent(typeof(Rigidbody2D))]
|
|
[RequireComponent(typeof(NetworkTransform))]
|
|
[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 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>
|
|
/// 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 void SetupRigidBody()
|
|
{
|
|
m_Rigidbody = GetComponent<Rigidbody2D>();
|
|
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;
|
|
}
|
|
|
|
/// <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()
|
|
{
|
|
UpdateOwnershipAuthority();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the authority differently depending upon
|
|
/// whether it is server or owner authoritative
|
|
/// </summary>
|
|
private void UpdateOwnershipAuthority()
|
|
{
|
|
if (m_IsServerAuthoritative)
|
|
{
|
|
m_IsAuthority = NetworkManager.IsServer;
|
|
}
|
|
else
|
|
{
|
|
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()
|
|
{
|
|
UpdateOwnershipAuthority();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override void OnNetworkDespawn()
|
|
{
|
|
UpdateOwnershipAuthority();
|
|
}
|
|
}
|
|
}
|
|
#endif // COM_UNITY_MODULES_PHYSICS2D
|