com.unity.netcode.gameobjects@1.5.1

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.5.1] - 2023-06-07

### Added

- Added support for serializing `NativeArray<>` and `NativeList<>` in `FastBufferReader`/`FastBufferWriter`, `BufferSerializer`, `NetworkVariable`, and RPCs. (To use `NativeList<>`, add `UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT` to your Scripting Define Symbols in `Project Settings > Player`) (#2375)
- The location of the automatically-created default network prefab list can now be configured (#2544)
- Added: Message size limits (max single message and max fragmented message) can now be set using NetworkManager.MaximumTransmissionUnitSize and NetworkManager.MaximumFragmentedMessageSize for transports that don't work with the default values (#2530)
- Added `NetworkObject.SpawnWithObservers` property (default is true) that when set to false will spawn a `NetworkObject` with no observers and will not be spawned on any client until `NetworkObject.NetworkShow` is invoked. (#2568)

### Fixed

- Fixed: Fixed a null reference in codegen in some projects (#2581)
- Fixed issue where the `OnClientDisconnected` client identifier was incorrect after a pending client connection was denied. (#2569)
- Fixed warning "Runtime Network Prefabs was not empty at initialization time." being erroneously logged when no runtime network prefabs had been added (#2565)
- Fixed issue where some temporary debug console logging was left in a merged PR. (#2562)
- Fixed the "Generate Default Network Prefabs List" setting not loading correctly and always reverting to being checked. (#2545)
- Fixed issue where users could not use NetworkSceneManager.VerifySceneBeforeLoading to exclude runtime generated scenes from client synchronization. (#2550)
- Fixed missing value on `NetworkListEvent` for `EventType.RemoveAt` events.  (#2542,#2543)
- Fixed issue where parenting a NetworkTransform under a transform with a scale other than Vector3.one would result in incorrect values on non-authoritative instances. (#2538)
- Fixed issue where a server would include scene migrated and then despawned NetworkObjects to a client that was being synchronized. (#2532)
- Fixed the inspector throwing exceptions when attempting to render `NetworkVariable`s of enum types. (#2529)
- Making a `NetworkVariable` with an `INetworkSerializable` type that doesn't meet the `new()` constraint will now create a compile-time error instead of an editor crash (#2528)
- Fixed Multiplayer Tools package installation docs page link on the NetworkManager popup. (#2526)
- Fixed an exception and error logging when two different objects are shown and hidden on the same frame (#2524)
- Fixed a memory leak in `UnityTransport` that occurred if `StartClient` failed. (#2518)
- Fixed issue where a client could throw an exception if abruptly disconnected from a network session with one or more spawned `NetworkObject`(s). (#2510)
- Fixed issue where invalid endpoint addresses were not being detected and returning false from NGO UnityTransport. (#2496)
- Fixed some errors that could occur if a connection is lost and the loss is detected when attempting to write to the socket. (#2495)

## Changed

- Adding network prefabs before NetworkManager initialization is now supported. (#2565)
- Connecting clients being synchronized now switch to the server's active scene before spawning and synchronizing NetworkObjects. (#2532)
- Updated `UnityTransport` dependency on `com.unity.transport` to 1.3.4. (#2533)
- Improved performance of NetworkBehaviour initialization by replacing reflection when initializing NetworkVariables with compile-time code generation, which should help reduce hitching during additive scene loads. (#2522)
This commit is contained in:
Unity Technologies
2023-06-07 00:00:00 +00:00
parent b5abc3ff7c
commit 4d70c198bd
119 changed files with 11328 additions and 3164 deletions

View File

@@ -62,7 +62,7 @@ namespace Unity.Netcode
private static unsafe ReaderHandle* CreateHandle(byte* buffer, int length, int offset, Allocator copyAllocator, Allocator internalAllocator)
{
ReaderHandle* readerHandle = null;
ReaderHandle* readerHandle;
if (copyAllocator == Allocator.None)
{
readerHandle = (ReaderHandle*)UnsafeUtility.Malloc(sizeof(ReaderHandle), UnsafeUtility.AlignOf<byte>(), internalAllocator);
@@ -461,6 +461,42 @@ namespace Unity.Netcode
}
}
/// <summary>
/// Read a NativeArray of INetworkSerializables
/// </summary>
/// <param name="value">INetworkSerializable instance</param>
/// <param name="allocator">The allocator to use to construct the resulting NativeArray</param>
/// <typeparam name="T">the array to read the values of type `T` into</typeparam>
/// <exception cref="NotImplementedException"></exception>
public void ReadNetworkSerializable<T>(out NativeArray<T> value, Allocator allocator) where T : unmanaged, INetworkSerializable
{
ReadValueSafe(out int size);
value = new NativeArray<T>(size, allocator);
for (var i = 0; i < size; ++i)
{
ReadNetworkSerializable(out T item);
value[i] = item;
}
}
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
/// <summary>
/// Read a NativeList of INetworkSerializables
/// </summary>
/// <param name="value">INetworkSerializable instance</param>
/// <typeparam name="T">the array to read the values of type `T` into</typeparam>
/// <exception cref="NotImplementedException"></exception>
public void ReadNetworkSerializableInPlace<T>(ref NativeList<T> value) where T : unmanaged, INetworkSerializable
{
ReadValueSafe(out int size);
value.Resize(size, NativeArrayOptions.UninitializedMemory);
for (var i = 0; i < size; ++i)
{
ReadNetworkSerializable(out value.ElementAt(i));
}
}
#endif
/// <summary>
/// Read an INetworkSerializable in-place, without constructing a new one
/// Note that this will NOT check for null before calling NetworkSerialize
@@ -757,6 +793,44 @@ namespace Unity.Netcode
ReadBytesSafe(bytes, sizeInBytes);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void ReadUnmanaged<T>(out NativeArray<T> value, Allocator allocator) where T : unmanaged
{
ReadUnmanaged(out int sizeInTs);
int sizeInBytes = sizeInTs * sizeof(T);
value = new NativeArray<T>(sizeInTs, allocator);
byte* bytes = (byte*)value.GetUnsafePtr();
ReadBytes(bytes, sizeInBytes);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void ReadUnmanagedSafe<T>(out NativeArray<T> value, Allocator allocator) where T : unmanaged
{
ReadUnmanagedSafe(out int sizeInTs);
int sizeInBytes = sizeInTs * sizeof(T);
value = new NativeArray<T>(sizeInTs, allocator);
byte* bytes = (byte*)value.GetUnsafePtr();
ReadBytesSafe(bytes, sizeInBytes);
}
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void ReadUnmanagedInPlace<T>(ref NativeList<T> value) where T : unmanaged
{
ReadUnmanaged(out int sizeInTs);
int sizeInBytes = sizeInTs * sizeof(T);
value.Resize(sizeInTs, NativeArrayOptions.UninitializedMemory);
byte* bytes = (byte*)value.GetUnsafePtr();
ReadBytes(bytes, sizeInBytes);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void ReadUnmanagedSafeInPlace<T>(ref NativeList<T> value) where T : unmanaged
{
ReadUnmanagedSafe(out int sizeInTs);
int sizeInBytes = sizeInTs * sizeof(T);
value.Resize(sizeInTs, NativeArrayOptions.UninitializedMemory);
byte* bytes = (byte*)value.GetUnsafePtr();
ReadBytesSafe(bytes, sizeInBytes);
}
#endif
/// <summary>
/// Read a NetworkSerializable value
@@ -800,6 +874,19 @@ namespace Unity.Netcode
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T[] value, FastBufferWriter.ForNetworkSerializable unused = default) where T : INetworkSerializable, new() => ReadNetworkSerializable(out value);
/// <summary>
/// Read a NetworkSerializable NativeArray
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read</param>
/// <param name="allocator">The allocator to use to construct the resulting NativeArray</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out NativeArray<T> value, Allocator allocator, FastBufferWriter.ForNetworkSerializable unused = default) where T : unmanaged, INetworkSerializable => ReadNetworkSerializable(out value, allocator);
/// <summary>
/// Read a struct
@@ -819,6 +906,72 @@ namespace Unity.Netcode
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => ReadUnmanaged(out value);
/// <summary>
/// Read a struct NativeArray
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read</param>
/// <param name="allocator">The allocator to use to construct the resulting NativeArray</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out NativeArray<T> value, Allocator allocator, FastBufferWriter.ForGeneric unused = default) where T : unmanaged
{
if (typeof(INetworkSerializable).IsAssignableFrom(typeof(T)))
{
// This calls WriteNetworkSerializable in a way that doesn't require
// any boxing.
NetworkVariableSerialization<NativeArray<T>>.Serializer.ReadWithAllocator(this, out value, allocator);
}
else
{
ReadUnmanaged(out value, allocator);
}
}
/// <summary>
/// Read a struct NativeArray using a Temp allocator. Equivalent to ReadValue(out value, Allocator.Temp)
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueTemp<T>(out NativeArray<T> value, FastBufferWriter.ForGeneric unused = default) where T : unmanaged
{
if (typeof(INetworkSerializable).IsAssignableFrom(typeof(T)))
{
// This calls WriteNetworkSerializable in a way that doesn't require
// any boxing.
NetworkVariableSerialization<NativeArray<T>>.Serializer.ReadWithAllocator(this, out value, Allocator.Temp);
}
else
{
ReadUnmanaged(out value, Allocator.Temp);
}
}
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
/// <summary>
/// Read a struct NativeList
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueInPlace<T>(ref NativeList<T> value, FastBufferWriter.ForGeneric unused = default) where T : unmanaged
{
if (typeof(INetworkSerializable).IsAssignableFrom(typeof(T)))
{
// This calls WriteNetworkSerializable in a way that doesn't require
// any boxing.
NetworkVariableSerialization<NativeList<T>>.Serializer.Read(this, ref value);
}
else
{
ReadUnmanagedInPlace(ref value);
}
}
#endif
/// <summary>
/// Read a struct
///
@@ -843,6 +996,81 @@ namespace Unity.Netcode
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a struct NativeArray
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read</param>
/// <param name="allocator">The allocator to use to construct the resulting NativeArray</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out NativeArray<T> value, Allocator allocator, FastBufferWriter.ForGeneric unused = default) where T : unmanaged
{
if (typeof(INetworkSerializable).IsAssignableFrom(typeof(T)))
{
// This calls WriteNetworkSerializable in a way that doesn't require
// any boxing.
NetworkVariableSerialization<NativeArray<T>>.Serializer.ReadWithAllocator(this, out value, allocator);
}
else
{
ReadUnmanagedSafe(out value, allocator);
}
}
/// <summary>
/// Read a struct NativeArray using a Temp allocator. Equivalent to ReadValueSafe(out value, Allocator.Temp)
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafeTemp<T>(out NativeArray<T> value, FastBufferWriter.ForGeneric unused = default) where T : unmanaged
{
if (typeof(INetworkSerializable).IsAssignableFrom(typeof(T)))
{
// This calls WriteNetworkSerializable in a way that doesn't require
// any boxing.
NetworkVariableSerialization<NativeArray<T>>.Serializer.ReadWithAllocator(this, out value, Allocator.Temp);
}
else
{
ReadUnmanagedSafe(out value, Allocator.Temp);
}
}
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
/// <summary>
/// Read a struct NativeList
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafeInPlace<T>(ref NativeList<T> value, FastBufferWriter.ForGeneric unused = default) where T : unmanaged
{
if (typeof(INetworkSerializable).IsAssignableFrom(typeof(T)))
{
// This calls WriteNetworkSerializable in a way that doesn't require
// any boxing.
NetworkVariableSerialization<NativeList<T>>.Serializer.Read(this, ref value);
}
else
{
ReadUnmanagedSafeInPlace(ref value);
}
}
#endif
/// <summary>
/// Read a primitive value (int, bool, etc)
/// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly
@@ -880,7 +1108,7 @@ namespace Unity.Netcode
public void ReadValueSafe<T>(out T value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a primitive value (int, bool, etc)
/// Read a primitive value (int, bool, etc) array
/// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly
/// on values that are not primitives.
///
@@ -936,6 +1164,7 @@ namespace Unity.Netcode
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T[] value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Vector2
/// </summary>
@@ -1346,5 +1575,94 @@ namespace Unity.Netcode
value.Length = length;
ReadBytesSafe(value.GetUnsafePtr(), length);
}
/// <summary>
/// Read a FixedString NativeArray.
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
/// <param name="allocator">The allocator to use to construct the resulting NativeArray</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void ReadValueSafe<T>(out NativeArray<T> value, Allocator allocator)
where T : unmanaged, INativeList<byte>, IUTF8Bytes
{
ReadUnmanagedSafe(out int length);
value = new NativeArray<T>(length, allocator);
var ptr = (T*)value.GetUnsafePtr();
for (var i = 0; i < length; ++i)
{
ReadValueSafeInPlace(ref ptr[i]);
}
}
/// <summary>
/// Read a FixedString NativeArray using a Temp allocator. Equivalent to ReadValueSafe(out value, Allocator.Temp)
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void ReadValueSafeTemp<T>(out NativeArray<T> value)
where T : unmanaged, INativeList<byte>, IUTF8Bytes
{
ReadUnmanagedSafe(out int length);
value = new NativeArray<T>(length, Allocator.Temp);
var ptr = (T*)value.GetUnsafePtr();
for (var i = 0; i < length; ++i)
{
ReadValueSafeInPlace(ref ptr[i]);
}
}
/// <summary>
/// Read a FixedString NativeArray using a Temp allocator. Equivalent to ReadValueSafe(out value, Allocator.Temp)
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T[] value, FastBufferWriter.ForFixedStrings unused = default)
where T : unmanaged, INativeList<byte>, IUTF8Bytes
{
ReadUnmanagedSafe(out int length);
value = new T[length];
for (var i = 0; i < length; ++i)
{
ReadValueSafeInPlace(ref value[i]);
}
}
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
/// <summary>
/// Read a FixedString NativeList.
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafeInPlace<T>(ref NativeList<T> value)
where T : unmanaged, INativeList<byte>, IUTF8Bytes
{
ReadUnmanagedSafe(out int length);
value.Resize(length, NativeArrayOptions.UninitializedMemory);
for (var i = 0; i < length; ++i)
{
ReadValueSafeInPlace(ref value.ElementAt(i));
}
}
#endif
}
}