com.unity.netcode.gameobjects@1.9.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.9.1] - 2024-04-18

### Added
- Added AnticipatedNetworkVariable<T>, which adds support for client anticipation of NetworkVariable values, allowing for more responsive gameplay (#2820)
- Added AnticipatedNetworkTransform, which adds support for client anticipation of NetworkTransforms (#2820)
- Added NetworkVariableBase.ExceedsDirtinessThreshold to allow network variables to throttle updates by only sending updates when the difference between the current and previous values exceeds a threshold. (This is exposed in NetworkVariable<T> with the callback NetworkVariable<T>.CheckExceedsDirtinessThreshold) (#2820)
- Added NetworkVariableUpdateTraits, which add additional throttling support: MinSecondsBetweenUpdates will prevent the NetworkVariable from sending updates more often than the specified time period (even if it exceeds the dirtiness threshold), while MaxSecondsBetweenUpdates will force a dirty NetworkVariable to send an update after the specified time period even if it has not yet exceeded the dirtiness threshold. (#2820)
- 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)
- Network variables wrapping `INetworkSerializable` types can perform delta serialization by setting `UserNetworkVariableSerialization<T>.WriteDelta` and `UserNetworkVariableSerialization<T>.ReadDelta` for those types. The built-in `INetworkSerializable` serializer will continue to be used for all other serialization operations, but if those callbacks are set, it will call into them on all but the initial serialization to perform delta serialization. (This could be useful if you have a large struct where most values do not change regularly and you want to send only the fields that did change.) (#2813)

### Fixed

- Fixed issue where NetworkTransformEditor would throw and exception if you excluded the physics package. (#2871)
- Fixed issue where `NetworkTransform` could not properly synchronize its base position when using half float precision. (#2845)
- Fixed issue where the host was not invoking `OnClientDisconnectCallback` for its own local client when internally shutting down. (#2822)
- Fixed issue where NetworkTransform could potentially attempt to "unregister" a named message prior to it being registered. (#2807)
- Fixed issue where in-scene placed `NetworkObject`s with complex nested children `NetworkObject`s (more than one child in depth) would not synchronize properly if WorldPositionStays was set to true. (#2796)

### Changed

- Changed `NetworkObjectReference` and `NetworkBehaviourReference` to allow null references when constructing and serializing. (#2874)
- Changed `NetworkAnimator` no longer requires the `Animator` component to exist on the same `GameObject`. (#2872)
- Changed `NetworkTransform` to now use `NetworkTransformMessage` as opposed to named messages for NetworkTransformState updates. (#2810)
- Changed `CustomMessageManager` so it no longer attempts to register or "unregister" a null or empty string and will log an error if this condition occurs. (#2807)
This commit is contained in:
Unity Technologies
2024-04-18 00:00:00 +00:00
parent f8ebf679ec
commit 158f26b913
72 changed files with 9955 additions and 1289 deletions

View File

@@ -0,0 +1,14 @@
using Unity.Netcode.Components;
using UnityEditor;
namespace Unity.Netcode.Editor
{
/// <summary>
/// The <see cref="CustomEditor"/> for <see cref="AnticipatedNetworkTransform"/>
/// </summary>
[CustomEditor(typeof(AnticipatedNetworkTransform), true)]
public class AnticipatedNetworkTransformEditor : NetworkTransformEditor
{
public override bool HideInterpolateValue => true;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 34bc168605014eeeadf97b12080e11fa
timeCreated: 1707514321

View File

@@ -21,6 +21,7 @@ namespace Unity.Netcode.Editor.CodeGen
public const string NetcodeModuleName = "Unity.Netcode.Runtime.dll";
public const string RuntimeAssemblyName = "Unity.Netcode.Runtime";
public const string ComponentsAssemblyName = "Unity.Netcode.Components";
public static readonly string NetworkBehaviour_FullName = typeof(NetworkBehaviour).FullName;
public static readonly string INetworkMessage_FullName = typeof(INetworkMessage).FullName;

View File

@@ -17,7 +17,7 @@ namespace Unity.Netcode.Editor.CodeGen
{
public override ILPPInterface GetInstance() => this;
public override bool WillProcess(ICompiledAssembly compiledAssembly) => compiledAssembly.Name == CodeGenHelpers.RuntimeAssemblyName;
public override bool WillProcess(ICompiledAssembly compiledAssembly) => compiledAssembly.Name == CodeGenHelpers.RuntimeAssemblyName || compiledAssembly.Name == CodeGenHelpers.ComponentsAssemblyName;
private readonly List<DiagnosticMessage> m_Diagnostics = new List<DiagnosticMessage>();

View File

@@ -31,6 +31,43 @@ namespace Unity.Netcode.Editor.CodeGen
private readonly List<DiagnosticMessage> m_Diagnostics = new List<DiagnosticMessage>();
public void AddWrappedType(TypeReference wrappedType)
{
if (!m_WrappedNetworkVariableTypes.Contains(wrappedType))
{
m_WrappedNetworkVariableTypes.Add(wrappedType);
var resolved = wrappedType.Resolve();
if (resolved != null)
{
if (resolved.FullName == "System.Collections.Generic.List`1")
{
AddWrappedType(((GenericInstanceType)wrappedType).GenericArguments[0]);
}
if (resolved.FullName == "System.Collections.Generic.HashSet`1")
{
AddWrappedType(((GenericInstanceType)wrappedType).GenericArguments[0]);
}
else if (resolved.FullName == "System.Collections.Generic.Dictionary`2")
{
AddWrappedType(((GenericInstanceType)wrappedType).GenericArguments[0]);
AddWrappedType(((GenericInstanceType)wrappedType).GenericArguments[1]);
}
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
else if (resolved.FullName == "Unity.Collections.NativeHashSet`1")
{
AddWrappedType(((GenericInstanceType)wrappedType).GenericArguments[0]);
}
else if (resolved.FullName == "Unity.Collections.NativeHashMap`2")
{
AddWrappedType(((GenericInstanceType)wrappedType).GenericArguments[0]);
AddWrappedType(((GenericInstanceType)wrappedType).GenericArguments[1]);
}
#endif
}
}
}
public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
{
if (!WillProcess(compiledAssembly))
@@ -87,10 +124,7 @@ namespace Unity.Netcode.Editor.CodeGen
if (attribute.AttributeType.Name == nameof(GenerateSerializationForTypeAttribute))
{
var wrappedType = mainModule.ImportReference((TypeReference)attribute.ConstructorArguments[0].Value);
if (!m_WrappedNetworkVariableTypes.Contains(wrappedType))
{
m_WrappedNetworkVariableTypes.Add(wrappedType);
}
AddWrappedType(wrappedType);
}
}
@@ -101,10 +135,7 @@ namespace Unity.Netcode.Editor.CodeGen
if (attribute.AttributeType.Name == nameof(GenerateSerializationForTypeAttribute))
{
var wrappedType = mainModule.ImportReference((TypeReference)attribute.ConstructorArguments[0].Value);
if (!m_WrappedNetworkVariableTypes.Contains(wrappedType))
{
m_WrappedNetworkVariableTypes.Add(wrappedType);
}
AddWrappedType(wrappedType);
}
}
}
@@ -241,6 +272,36 @@ namespace Unity.Netcode.Editor.CodeGen
serializeMethod?.GenericArguments.Add(wrappedType);
equalityMethod.GenericArguments.Add(wrappedType);
}
else if (type.Resolve().FullName == "System.Collections.Generic.List`1")
{
var wrappedType = ((GenericInstanceType)type).GenericArguments[0];
serializeMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeSerializer_List_MethodRef);
equalityMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeEqualityChecker_List_MethodRef);
serializeMethod.GenericArguments.Add(wrappedType);
equalityMethod.GenericArguments.Add(wrappedType);
}
else if (type.Resolve().FullName == "System.Collections.Generic.HashSet`1")
{
var wrappedType = ((GenericInstanceType)type).GenericArguments[0];
serializeMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeSerializer_HashSet_MethodRef);
equalityMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeEqualityChecker_HashSet_MethodRef);
serializeMethod.GenericArguments.Add(wrappedType);
equalityMethod.GenericArguments.Add(wrappedType);
}
else if (type.Resolve().FullName == "System.Collections.Generic.Dictionary`2")
{
var wrappedKeyType = ((GenericInstanceType)type).GenericArguments[0];
var wrappedValType = ((GenericInstanceType)type).GenericArguments[1];
serializeMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeSerializer_Dictionary_MethodRef);
equalityMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeEqualityChecker_Dictionary_MethodRef);
serializeMethod.GenericArguments.Add(wrappedKeyType);
serializeMethod.GenericArguments.Add(wrappedValType);
equalityMethod.GenericArguments.Add(wrappedKeyType);
equalityMethod.GenericArguments.Add(wrappedValType);
}
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
else if (type.Resolve().FullName == "Unity.Collections.NativeList`1")
{
@@ -267,12 +328,30 @@ namespace Unity.Netcode.Editor.CodeGen
equalityMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeEqualityChecker_UnmanagedValueEqualsList_MethodRef);
}
if (serializeMethod != null)
{
serializeMethod.GenericArguments.Add(wrappedType);
}
serializeMethod?.GenericArguments.Add(wrappedType);
equalityMethod.GenericArguments.Add(wrappedType);
}
else if (type.Resolve().FullName == "Unity.Collections.NativeHashSet`1")
{
var wrappedType = ((GenericInstanceType)type).GenericArguments[0];
serializeMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeSerializer_NativeHashSet_MethodRef);
equalityMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeEqualityChecker_NativeHashSet_MethodRef);
serializeMethod.GenericArguments.Add(wrappedType);
equalityMethod.GenericArguments.Add(wrappedType);
}
else if (type.Resolve().FullName == "Unity.Collections.NativeHashMap`2")
{
var wrappedKeyType = ((GenericInstanceType)type).GenericArguments[0];
var wrappedValType = ((GenericInstanceType)type).GenericArguments[1];
serializeMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeSerializer_NativeHashMap_MethodRef);
equalityMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeEqualityChecker_NativeHashMap_MethodRef);
serializeMethod.GenericArguments.Add(wrappedKeyType);
serializeMethod.GenericArguments.Add(wrappedValType);
equalityMethod.GenericArguments.Add(wrappedKeyType);
equalityMethod.GenericArguments.Add(wrappedValType);
}
#endif
else if (type.IsValueType)
{
@@ -329,6 +408,7 @@ namespace Unity.Netcode.Editor.CodeGen
}
else
{
m_Diagnostics.AddError($"{type}: Managed type in NetworkVariable must implement IEquatable<{type}>");
equalityMethod = new GenericInstanceMethod(m_NetworkVariableSerializationTypes_InitializeEqualityChecker_ManagedClassEquals_MethodRef);
}
@@ -398,7 +478,12 @@ namespace Unity.Netcode.Editor.CodeGen
private MethodReference m_NetworkVariableSerializationTypes_InitializeSerializer_UnmanagedINetworkSerializableArray_MethodRef;
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
private MethodReference m_NetworkVariableSerializationTypes_InitializeSerializer_UnmanagedINetworkSerializableList_MethodRef;
private MethodReference m_NetworkVariableSerializationTypes_InitializeSerializer_NativeHashSet_MethodRef;
private MethodReference m_NetworkVariableSerializationTypes_InitializeSerializer_NativeHashMap_MethodRef;
#endif
private MethodReference m_NetworkVariableSerializationTypes_InitializeSerializer_List_MethodRef;
private MethodReference m_NetworkVariableSerializationTypes_InitializeSerializer_HashSet_MethodRef;
private MethodReference m_NetworkVariableSerializationTypes_InitializeSerializer_Dictionary_MethodRef;
private MethodReference m_NetworkVariableSerializationTypes_InitializeSerializer_ManagedINetworkSerializable_MethodRef;
private MethodReference m_NetworkVariableSerializationTypes_InitializeSerializer_FixedString_MethodRef;
private MethodReference m_NetworkVariableSerializationTypes_InitializeSerializer_FixedStringArray_MethodRef;
@@ -415,7 +500,12 @@ namespace Unity.Netcode.Editor.CodeGen
private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_UnmanagedValueEqualsArray_MethodRef;
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_UnmanagedValueEqualsList_MethodRef;
private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_NativeHashSet_MethodRef;
private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_NativeHashMap_MethodRef;
#endif
private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_List_MethodRef;
private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_HashSet_MethodRef;
private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_Dictionary_MethodRef;
private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_ManagedClassEquals_MethodRef;
private MethodReference m_RuntimeInitializeOnLoadAttribute_Ctor;
@@ -940,7 +1030,22 @@ namespace Unity.Netcode.Editor.CodeGen
case nameof(NetworkVariableSerializationTypes.InitializeSerializer_UnmanagedINetworkSerializableList):
m_NetworkVariableSerializationTypes_InitializeSerializer_UnmanagedINetworkSerializableList_MethodRef = method;
break;
case nameof(NetworkVariableSerializationTypes.InitializeSerializer_NativeHashSet):
m_NetworkVariableSerializationTypes_InitializeSerializer_NativeHashSet_MethodRef = method;
break;
case nameof(NetworkVariableSerializationTypes.InitializeSerializer_NativeHashMap):
m_NetworkVariableSerializationTypes_InitializeSerializer_NativeHashMap_MethodRef = method;
break;
#endif
case nameof(NetworkVariableSerializationTypes.InitializeSerializer_List):
m_NetworkVariableSerializationTypes_InitializeSerializer_List_MethodRef = method;
break;
case nameof(NetworkVariableSerializationTypes.InitializeSerializer_HashSet):
m_NetworkVariableSerializationTypes_InitializeSerializer_HashSet_MethodRef = method;
break;
case nameof(NetworkVariableSerializationTypes.InitializeSerializer_Dictionary):
m_NetworkVariableSerializationTypes_InitializeSerializer_Dictionary_MethodRef = method;
break;
case nameof(NetworkVariableSerializationTypes.InitializeSerializer_ManagedINetworkSerializable):
m_NetworkVariableSerializationTypes_InitializeSerializer_ManagedINetworkSerializable_MethodRef = method;
break;
@@ -971,7 +1076,22 @@ namespace Unity.Netcode.Editor.CodeGen
case nameof(NetworkVariableSerializationTypes.InitializeEqualityChecker_UnmanagedIEquatableList):
m_NetworkVariableSerializationTypes_InitializeEqualityChecker_UnmanagedIEquatableList_MethodRef = method;
break;
case nameof(NetworkVariableSerializationTypes.InitializeEqualityChecker_NativeHashSet):
m_NetworkVariableSerializationTypes_InitializeEqualityChecker_NativeHashSet_MethodRef = method;
break;
case nameof(NetworkVariableSerializationTypes.InitializeEqualityChecker_NativeHashMap):
m_NetworkVariableSerializationTypes_InitializeEqualityChecker_NativeHashMap_MethodRef = method;
break;
#endif
case nameof(NetworkVariableSerializationTypes.InitializeEqualityChecker_List):
m_NetworkVariableSerializationTypes_InitializeEqualityChecker_List_MethodRef = method;
break;
case nameof(NetworkVariableSerializationTypes.InitializeEqualityChecker_HashSet):
m_NetworkVariableSerializationTypes_InitializeEqualityChecker_HashSet_MethodRef = method;
break;
case nameof(NetworkVariableSerializationTypes.InitializeEqualityChecker_Dictionary):
m_NetworkVariableSerializationTypes_InitializeEqualityChecker_Dictionary_MethodRef = method;
break;
case nameof(NetworkVariableSerializationTypes.InitializeEqualityChecker_UnmanagedValueEquals):
m_NetworkVariableSerializationTypes_InitializeEqualityChecker_UnmanagedValueEquals_MethodRef = method;
break;
@@ -1246,10 +1366,7 @@ namespace Unity.Netcode.Editor.CodeGen
continue;
}
var wrappedType = genericInstanceType.GenericArguments[idx];
if (!m_WrappedNetworkVariableTypes.Contains(wrappedType))
{
m_WrappedNetworkVariableTypes.Add(wrappedType);
}
AddWrappedType(wrappedType);
}
}
}
@@ -1282,10 +1399,7 @@ namespace Unity.Netcode.Editor.CodeGen
continue;
}
var wrappedType = genericInstanceType.GenericArguments[idx];
if (!m_WrappedNetworkVariableTypes.Contains(wrappedType))
{
m_WrappedNetworkVariableTypes.Add(wrappedType);
}
AddWrappedType(wrappedType);
}
}
}

View File

@@ -222,7 +222,7 @@ namespace Unity.Netcode.Editor
private void DrawTransportField()
{
#if RELAY_INTEGRATION_AVAILABLE
var useRelay = EditorPrefs.GetBool(k_UseEasyRelayIntegrationKey, false);
var useRelay = EditorPrefs.GetBool(m_UseEasyRelayIntegrationKey, false);
#else
var useRelay = false;
#endif
@@ -257,7 +257,7 @@ namespace Unity.Netcode.Editor
}
#if RELAY_INTEGRATION_AVAILABLE
private readonly string k_UseEasyRelayIntegrationKey = "NetworkManagerUI_UseRelay_" + Application.dataPath.GetHashCode();
private readonly string m_UseEasyRelayIntegrationKey = "NetworkManagerUI_UseRelay_" + Application.dataPath.GetHashCode();
private string m_JoinCode = "";
private string m_StartConnectionError = null;
private string m_Region = "";
@@ -272,7 +272,7 @@ namespace Unity.Netcode.Editor
#if RELAY_INTEGRATION_AVAILABLE
// use editor prefs to persist the setting when entering / leaving play mode / exiting Unity
var useRelay = EditorPrefs.GetBool(k_UseEasyRelayIntegrationKey, false);
var useRelay = EditorPrefs.GetBool(m_UseEasyRelayIntegrationKey, false);
GUILayout.BeginHorizontal();
useRelay = GUILayout.Toggle(useRelay, "Try Relay in the Editor");
@@ -284,7 +284,7 @@ namespace Unity.Netcode.Editor
}
GUILayout.EndHorizontal();
EditorPrefs.SetBool(k_UseEasyRelayIntegrationKey, useRelay);
EditorPrefs.SetBool(m_UseEasyRelayIntegrationKey, useRelay);
if (useRelay && !Application.isPlaying && !CloudProjectSettings.projectBound)
{
EditorGUILayout.HelpBox("To use relay, you need to setup your project in the Project Settings in the Services section.", MessageType.Warning);

View File

@@ -37,6 +37,8 @@ namespace Unity.Netcode.Editor
private static GUIContent s_RotationLabel = EditorGUIUtility.TrTextContent("Rotation");
private static GUIContent s_ScaleLabel = EditorGUIUtility.TrTextContent("Scale");
public virtual bool HideInterpolateValue => false;
/// <inheritdoc/>
public void OnEnable()
{
@@ -137,7 +139,11 @@ namespace Unity.Netcode.Editor
EditorGUILayout.Space();
EditorGUILayout.LabelField("Configurations", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(m_InLocalSpaceProperty);
EditorGUILayout.PropertyField(m_InterpolateProperty);
if (!HideInterpolateValue)
{
EditorGUILayout.PropertyField(m_InterpolateProperty);
}
EditorGUILayout.PropertyField(m_SlerpPosition);
EditorGUILayout.PropertyField(m_UseQuaternionSynchronization);
if (m_UseQuaternionSynchronization.boolValue)
@@ -150,9 +156,10 @@ namespace Unity.Netcode.Editor
}
EditorGUILayout.PropertyField(m_UseHalfFloatPrecision);
#if COM_UNITY_MODULES_PHYSICS
// if rigidbody is present but network rigidbody is not present
var go = ((NetworkTransform)target).gameObject;
#if COM_UNITY_MODULES_PHYSICS
if (go.TryGetComponent<Rigidbody>(out _) && go.TryGetComponent<NetworkRigidbody>(out _) == false)
{
EditorGUILayout.HelpBox("This GameObject contains a Rigidbody but no NetworkRigidbody.\n" +