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
123 lines
6.5 KiB
C#
123 lines
6.5 KiB
C#
using System;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
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}"/>.
|
|
/// 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>
|
|
{
|
|
private NetworkObjectReference m_NetworkObjectReference;
|
|
private ushort m_NetworkBehaviourId;
|
|
private static ushort s_NullId = ushort.MaxValue;
|
|
|
|
/// <summary>
|
|
/// Creates a new instance of the <see cref="NetworkBehaviourReference{T}"/> struct.
|
|
/// </summary>
|
|
/// <param name="networkBehaviour">The <see cref="NetworkBehaviour"/> to reference.</param>
|
|
/// <exception cref="ArgumentException"></exception>
|
|
public NetworkBehaviourReference(NetworkBehaviour networkBehaviour)
|
|
{
|
|
if (networkBehaviour == null)
|
|
{
|
|
m_NetworkObjectReference = new NetworkObjectReference((NetworkObject)null);
|
|
m_NetworkBehaviourId = s_NullId;
|
|
return;
|
|
}
|
|
if (networkBehaviour.NetworkObject == null)
|
|
{
|
|
throw new ArgumentException($"Cannot create {nameof(NetworkBehaviourReference)} from {nameof(NetworkBehaviour)} without a {nameof(NetworkObject)}.");
|
|
}
|
|
|
|
m_NetworkObjectReference = networkBehaviour.NetworkObject;
|
|
m_NetworkBehaviourId = networkBehaviour.NetworkBehaviourId;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tries to get the <see cref="NetworkBehaviour"/> referenced by this reference.
|
|
/// </summary>
|
|
/// <param name="networkBehaviour">The <see cref="NetworkBehaviour"/> which was found. Null if the corresponding <see cref="NetworkObject"/> was not found.</param>
|
|
/// <param name="networkManager">The networkmanager. Uses <see cref="NetworkManager.Singleton"/> to resolve if null.</param>
|
|
/// <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, networkManager);
|
|
return networkBehaviour != null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tries to get the <see cref="NetworkBehaviour"/> referenced by this reference.
|
|
/// </summary>
|
|
/// <param name="networkBehaviour">The <see cref="NetworkBehaviour"/> which was found. Null if the corresponding <see cref="NetworkObject"/> was not found.</param>
|
|
/// <param name="networkManager">The networkmanager. Uses <see cref="NetworkManager.Singleton"/> to resolve if null.</param>
|
|
/// <typeparam name="T">The type of the networkBehaviour for convenience.</typeparam>
|
|
/// <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, networkManager) as T;
|
|
return networkBehaviour != null;
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private static NetworkBehaviour GetInternal(NetworkBehaviourReference networkBehaviourRef, NetworkManager networkManager = null)
|
|
{
|
|
if (networkBehaviourRef.m_NetworkBehaviourId == s_NullId)
|
|
{
|
|
return null;
|
|
}
|
|
if (networkBehaviourRef.m_NetworkObjectReference.TryGet(out NetworkObject networkObject, networkManager))
|
|
{
|
|
return networkObject.GetNetworkBehaviourAtOrderIndex(networkBehaviourRef.m_NetworkBehaviourId);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public bool Equals(NetworkBehaviourReference other)
|
|
{
|
|
return m_NetworkObjectReference.Equals(other.m_NetworkObjectReference) && m_NetworkBehaviourId == other.m_NetworkBehaviourId;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override bool Equals(object obj)
|
|
{
|
|
return obj is NetworkBehaviourReference other && Equals(other);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override int GetHashCode()
|
|
{
|
|
unchecked
|
|
{
|
|
return (m_NetworkObjectReference.GetHashCode() * 397) ^ m_NetworkBehaviourId.GetHashCode();
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
|
|
{
|
|
m_NetworkObjectReference.NetworkSerialize(serializer);
|
|
serializer.SerializeValue(ref m_NetworkBehaviourId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Implicitly convert <see cref="NetworkBehaviourReference"/> to <see cref="NetworkBehaviour"/>.
|
|
/// </summary>
|
|
/// <param name="networkBehaviourRef">The <see cref="NetworkBehaviourReference"/> to convert from.</param>
|
|
/// <returns>The <see cref="NetworkBehaviour"/> this class is holding a reference to</returns>
|
|
public static implicit operator NetworkBehaviour(NetworkBehaviourReference networkBehaviourRef) => GetInternal(networkBehaviourRef);
|
|
|
|
/// <summary>
|
|
/// Implicitly convert <see cref="NetworkBehaviour"/> to <see cref="NetworkBehaviourReference"/>.
|
|
/// </summary>
|
|
/// <param name="networkBehaviour">The <see cref="NetworkBehaviour"/> to convert from.</param>
|
|
/// <returns>The <see cref="NetworkBehaviourReference"/> created from the <see cref="NetworkBehaviour"/> passed in as a parameter</returns>
|
|
public static implicit operator NetworkBehaviourReference(NetworkBehaviour networkBehaviour) => new NetworkBehaviourReference(networkBehaviour);
|
|
}
|
|
}
|