com.unity.netcode.gameobjects@1.3.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.3.1] - 2023-03-27

### Added

- Added detection and graceful handling of corrupt packets for additional safety. (#2419)

### Changed

- The UTP component UI has been updated to be more user-friendly for new users by adding a simple toggle to switch between local-only (127.0.0.1) and remote (0.0.0.0) binding modes, using the toggle "Allow Remote Connections" (#2408)
- Updated `UnityTransport` dependency on `com.unity.transport` to 1.3.3. (#2450)
- `NetworkShow()` of `NetworkObject`s are delayed until the end of the frame to ensure consistency of delta-driven variables like `NetworkList`.
- Dirty `NetworkObject` are reset at end-of-frame and not at serialization time.
- `NetworkHide()` of an object that was just `NetworkShow()`n produces a warning, as remote clients will _not_ get a spawn/despawn pair.
- Renamed the NetworkTransform.SetState parameter `shouldGhostsInterpolate` to `teleportDisabled` for better clarity of what that parameter does. (#2228)
- Network prefabs are now stored in a ScriptableObject that can be shared between NetworkManagers, and have been exposed for public access. By default, a Default Prefabs List is created that contains all NetworkObject prefabs in the project, and new NetworkManagers will default to using that unless that option is turned off in the Netcode for GameObjects settings. Existing NetworkManagers will maintain their existing lists, which can be migrated to the new format via a button in their inspector. (#2322)

### Fixed

- Fixed issue where changes to a layer's weight would not synchronize unless a state transition was occurring.(#2399)
- Fixed issue where `NetworkManager.LocalClientId` was returning the `NetworkTransport.ServerClientId` as opposed to the `NetworkManager.m_LocalClientId`. (#2398)
- Fixed issue where a dynamically spawned `NetworkObject` parented under an in-scene placed `NetworkObject` would have its `InScenePlaced` value changed to `true`. This would result in a soft synchronization error for late joining clients. (#2396)
- Fixed a UTP test that was failing when you install Unity Transport package 2.0.0 or newer. (#2347)
- Fixed issue where `NetcodeSettingsProvider` would throw an exception in Unity 2020.3.x versions. (#2345)
- Fixed server side issue where, depending upon component ordering, some NetworkBehaviour components might not have their OnNetworkDespawn method invoked if the client side disconnected. (#2323)
- Fixed a case where data corruption could occur when using UnityTransport when reaching a certain level of send throughput. (#2332)
- Fixed an issue in `UnityTransport` where an exception would be thrown if starting a Relay host/server on WebGL. This exception should only be thrown if using direct connections (where WebGL can't act as a host/server). (#2321)
- Fixed `NetworkAnimator` issue where it was not checking for `AnimatorStateTtansition.destinationStateMachine` and any possible sub-states defined within it. (#2309)
- Fixed `NetworkAnimator` issue where the host client was receiving the ClientRpc animation updates when the host was the owner.(#2309)
- Fixed `NetworkAnimator` issue with using pooled objects and when specific properties are cleaned during despawn and destroy.(#2309)
- Fixed issue where `NetworkAnimator` was checking for animation changes when the associated `NetworkObject` was not spawned.(#2309)
- Corrected an issue with the documentation for BufferSerializer (#2401)
This commit is contained in:
Unity Technologies
2023-03-27 00:00:00 +00:00
parent fe02ca682e
commit 8060718e04
69 changed files with 3128 additions and 888 deletions

View File

@@ -1,15 +1,25 @@
using System.Runtime.CompilerServices;
#if UNITY_EDITOR
[assembly: InternalsVisibleTo("Unity.Netcode.EditorTests")]
[assembly: InternalsVisibleTo("Unity.Netcode.Editor.CodeGen")]
[assembly: InternalsVisibleTo("Unity.Netcode.Editor")]
[assembly: InternalsVisibleTo("TestProject.EditorTests")]
[assembly: InternalsVisibleTo("Unity.Netcode.Editor.CodeGen")]
#endif
[assembly: InternalsVisibleTo("TestProject.ToolsIntegration.RuntimeTests")]
[assembly: InternalsVisibleTo("TestProject.RuntimeTests")]
#endif // UNITY_EDITOR
#if MULTIPLAYER_TOOLS
[assembly: InternalsVisibleTo("Unity.Multiplayer.Tools.Adapters.Ngo1WithUtp2")]
#endif // MULTIPLAYER_TOOLS
#if COM_UNITY_NETCODE_ADAPTER_UTP
[assembly: InternalsVisibleTo("Unity.Netcode.Adapter.UTP")]
#endif // COM_UNITY_NETCODE_ADAPTER_UTP
#if UNITY_INCLUDE_TESTS
[assembly: InternalsVisibleTo("Unity.Netcode.RuntimeTests")]
[assembly: InternalsVisibleTo("Unity.Netcode.TestHelpers.Runtime")]
[assembly: InternalsVisibleTo("Unity.Netcode.Adapter.UTP")]
[assembly: InternalsVisibleTo("Unity.Multiplayer.Tools.Adapters.Ngo1WithUtp2")]
[assembly: InternalsVisibleTo("TestProject.RuntimeTests")]
#if UNITY_EDITOR
[assembly: InternalsVisibleTo("Unity.Netcode.EditorTests")]
[assembly: InternalsVisibleTo("TestProject.EditorTests")]
#endif // UNITY_EDITOR
#if MULTIPLAYER_TOOLS
[assembly: InternalsVisibleTo("TestProject.ToolsIntegration.RuntimeTests")]
#endif // MULTIPLAYER_TOOLS
#endif // UNITY_INCLUDE_TESTS

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using Unity.Collections;
using UnityEngine.Serialization;
namespace Unity.Netcode
{
@@ -30,20 +31,8 @@ namespace Unity.Netcode
[Tooltip("When set, NetworkManager will automatically create and spawn the assigned player prefab. This can be overridden by adding it to the NetworkPrefabs list and selecting override.")]
public GameObject PlayerPrefab;
/// <summary>
/// A list of prefabs that can be dynamically spawned.
/// </summary>
[SerializeField]
[Tooltip("The prefabs that can be spawned across the network")]
internal List<NetworkPrefab> NetworkPrefabs = new List<NetworkPrefab>();
/// <summary>
/// This dictionary provides a quick way to check and see if a NetworkPrefab has a NetworkPrefab override.
/// Generated at runtime and OnValidate
/// </summary>
internal Dictionary<uint, NetworkPrefab> NetworkPrefabOverrideLinks = new Dictionary<uint, NetworkPrefab>();
internal Dictionary<uint, uint> OverrideToNetworkPrefab = new Dictionary<uint, uint>();
public NetworkPrefabs Prefabs = new NetworkPrefabs();
/// <summary>
@@ -239,7 +228,7 @@ namespace Unity.Netcode
if (ForceSamePrefabs)
{
var sortedDictionary = NetworkPrefabOverrideLinks.OrderBy(x => x.Key);
var sortedDictionary = Prefabs.NetworkPrefabOverrideLinks.OrderBy(x => x.Key);
foreach (var sortedEntry in sortedDictionary)
{
@@ -273,6 +262,79 @@ namespace Unity.Netcode
{
return hash == GetConfig();
}
internal void InitializePrefabs()
{
if (HasOldPrefabList())
{
MigrateOldNetworkPrefabsToNetworkPrefabsList();
}
Prefabs.Initialize();
}
#region Legacy Network Prefab List
[NonSerialized]
private bool m_DidWarnOldPrefabList = false;
private void WarnOldPrefabList()
{
if (!m_DidWarnOldPrefabList)
{
Debug.LogWarning("Using Legacy Network Prefab List. Consider Migrating.");
m_DidWarnOldPrefabList = true;
}
}
/// <summary>
/// Returns true if the old List&lt;NetworkPrefab&gt; serialized data is present.
/// </summary>
/// <remarks>
/// Internal use only to help migrate projects. <seealso cref="MigrateOldNetworkPrefabsToNetworkPrefabsList"/></remarks>
internal bool HasOldPrefabList()
{
return OldPrefabList?.Count > 0;
}
/// <summary>
/// Migrate the old format List&lt;NetworkPrefab&gt; prefab registration to the new NetworkPrefabsList ScriptableObject.
/// </summary>
/// <remarks>
/// OnAfterDeserialize cannot instantiate new objects (e.g. NetworkPrefabsList SO) since it executes in a thread, so we have to do it later.
/// Since NetworkConfig isn't a Unity.Object it doesn't get an Awake callback, so we have to do this in NetworkManager and expose this API.
/// </remarks>
internal NetworkPrefabsList MigrateOldNetworkPrefabsToNetworkPrefabsList()
{
if (OldPrefabList == null || OldPrefabList.Count == 0)
{
return null;
}
if (Prefabs == null)
{
throw new Exception("Prefabs field is null.");
}
Prefabs.NetworkPrefabsLists.Add(ScriptableObject.CreateInstance<NetworkPrefabsList>());
if (OldPrefabList?.Count > 0)
{
// Migrate legacy types/fields
foreach (var networkPrefab in OldPrefabList)
{
Prefabs.NetworkPrefabsLists[Prefabs.NetworkPrefabsLists.Count - 1].Add(networkPrefab);
}
}
OldPrefabList = null;
return Prefabs.NetworkPrefabsLists[Prefabs.NetworkPrefabsLists.Count - 1];
}
[FormerlySerializedAs("NetworkPrefabs")]
[SerializeField]
internal List<NetworkPrefab> OldPrefabList;
#endregion
}
}

View File

@@ -3,10 +3,23 @@ using UnityEngine;
namespace Unity.Netcode
{
internal enum NetworkPrefabOverride
/// <summary>
/// The method of NetworkPrefab override used to identify the source prefab
/// </summary>
public enum NetworkPrefabOverride
{
/// <summary>
/// No oeverride is present
/// </summary>
None,
/// <summary>
/// Override the prefab when the given SourcePrefabToOverride is requested
/// </summary>
Prefab,
/// <summary>
/// Override the prefab when the given SourceHashToOverride is requested
/// Used in situations where the server assets do not exist in client builds
/// </summary>
Hash
}
@@ -14,10 +27,10 @@ namespace Unity.Netcode
/// Class that represents a NetworkPrefab
/// </summary>
[Serializable]
internal class NetworkPrefab
public class NetworkPrefab
{
/// <summary>
/// The override setttings for this NetworkPrefab
/// The override settings for this NetworkPrefab
/// </summary>
public NetworkPrefabOverride Override;
@@ -41,5 +54,168 @@ namespace Unity.Netcode
/// The prefab to replace (override) the source prefab with
/// </summary>
public GameObject OverridingTargetPrefab;
public bool Equals(NetworkPrefab other)
{
return Override == other.Override &&
Prefab == other.Prefab &&
SourcePrefabToOverride == other.SourcePrefabToOverride &&
SourceHashToOverride == other.SourceHashToOverride &&
OverridingTargetPrefab == other.OverridingTargetPrefab;
}
public uint SourcePrefabGlobalObjectIdHash
{
get
{
switch (Override)
{
case NetworkPrefabOverride.None:
if (Prefab != null && Prefab.TryGetComponent(out NetworkObject no))
{
return no.GlobalObjectIdHash;
}
throw new InvalidOperationException("Prefab field isn't set or isn't a Network Object");
case NetworkPrefabOverride.Prefab:
if (SourcePrefabToOverride != null && SourcePrefabToOverride.TryGetComponent(out no))
{
return no.GlobalObjectIdHash;
}
throw new InvalidOperationException("Source Prefab field isn't set or isn't a Network Object");
case NetworkPrefabOverride.Hash:
return SourceHashToOverride;
default:
throw new ArgumentOutOfRangeException();
}
}
}
public uint TargetPrefabGlobalObjectIdHash
{
get
{
switch (Override)
{
case NetworkPrefabOverride.None:
return 0;
case NetworkPrefabOverride.Prefab:
case NetworkPrefabOverride.Hash:
if (OverridingTargetPrefab != null && OverridingTargetPrefab.TryGetComponent(out NetworkObject no))
{
return no.GlobalObjectIdHash;
}
throw new InvalidOperationException("Target Prefab field isn't set or isn't a Network Object");
default:
throw new ArgumentOutOfRangeException();
}
}
}
public bool Validate(int index = -1)
{
NetworkObject networkObject;
if (Override == NetworkPrefabOverride.None)
{
if (Prefab == null)
{
NetworkLog.LogWarning($"{nameof(NetworkPrefab)} cannot be null ({nameof(NetworkPrefab)} at index: {index})");
return false;
}
networkObject = Prefab.GetComponent<NetworkObject>();
if (networkObject == null)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
{
NetworkLog.LogWarning($"{NetworkManager.PrefabDebugHelper(this)} is missing " +
$"a {nameof(NetworkObject)} component (entry will be ignored).");
}
return false;
}
return true;
}
// Validate source prefab override values first
switch (Override)
{
case NetworkPrefabOverride.Hash:
{
if (SourceHashToOverride == 0)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
{
NetworkLog.LogWarning($"{nameof(NetworkPrefab)} {nameof(SourceHashToOverride)} is zero " +
"(entry will be ignored).");
}
return false;
}
break;
}
case NetworkPrefabOverride.Prefab:
{
if (SourcePrefabToOverride == null)
{
// This is a leftover side-effect from NetworkManager's OnValidate. It's a usability
// adjustment to automatically set the "Prefab" field as the source prefab when a user
// swaps from the default Inspector to the override one.
if (Prefab != null)
{
SourcePrefabToOverride = Prefab;
}
else if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
{
NetworkLog.LogWarning($"{nameof(NetworkPrefab)} {nameof(SourcePrefabToOverride)} is null (entry will be ignored).");
return false;
}
}
if (!SourcePrefabToOverride.TryGetComponent(out networkObject))
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
{
NetworkLog.LogWarning($"{nameof(NetworkPrefab)} ({SourcePrefabToOverride.name}) " +
$"is missing a {nameof(NetworkObject)} component (entry will be ignored).");
}
return false;
}
break;
}
}
// Validate target prefab override values next
if (OverridingTargetPrefab == null)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
{
NetworkLog.LogWarning($"{nameof(NetworkPrefab)} {nameof(OverridingTargetPrefab)} is null!");
}
switch (Override)
{
case NetworkPrefabOverride.Hash:
{
Debug.LogWarning($"{nameof(NetworkPrefab)} override entry {SourceHashToOverride} will be removed and ignored.");
break;
}
case NetworkPrefabOverride.Prefab:
{
Debug.LogWarning($"{nameof(NetworkPrefab)} override entry ({SourcePrefabToOverride.name}) will be removed and ignored.");
break;
}
}
return false;
}
return true;
}
public override string ToString()
{
return $"{{SourceHash: {SourceHashToOverride}, TargetHash: {TargetPrefabGlobalObjectIdHash}}}";
}
}
}

View File

@@ -0,0 +1,297 @@
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace Unity.Netcode
{
/// <summary>
/// A class that represents the runtime aspect of network prefabs.
/// This class contains processed prefabs from the NetworkPrefabsList, as
/// well as additional modifications (additions and removals) made at runtime.
/// </summary>
[Serializable]
public class NetworkPrefabs
{
/// <summary>
/// Edit-time scripted object containing a list of NetworkPrefabs.
/// </summary>
/// <remarks>
/// This field can be null if no prefabs are pre-configured.
/// Runtime usages of <see cref="NetworkPrefabs"/> should not depend on this edit-time field for execution.
/// </remarks>
[SerializeField]
public List<NetworkPrefabsList> NetworkPrefabsLists = new List<NetworkPrefabsList>();
/// <summary>
/// This dictionary provides a quick way to check and see if a NetworkPrefab has a NetworkPrefab override.
/// Generated at runtime and OnValidate
/// </summary>
[NonSerialized]
public Dictionary<uint, NetworkPrefab> NetworkPrefabOverrideLinks = new Dictionary<uint, NetworkPrefab>();
[NonSerialized]
public Dictionary<uint, uint> OverrideToNetworkPrefab = new Dictionary<uint, uint>();
public IReadOnlyList<NetworkPrefab> Prefabs => m_Prefabs;
[NonSerialized]
private List<NetworkPrefab> m_Prefabs = new List<NetworkPrefab>();
private void AddTriggeredByNetworkPrefabList(NetworkPrefab networkPrefab)
{
if (AddPrefabRegistration(networkPrefab))
{
m_Prefabs.Add(networkPrefab);
}
}
private void RemoveTriggeredByNetworkPrefabList(NetworkPrefab networkPrefab)
{
m_Prefabs.Remove(networkPrefab);
}
~NetworkPrefabs()
{
foreach (var list in NetworkPrefabsLists)
{
list.OnAdd -= AddTriggeredByNetworkPrefabList;
list.OnRemove -= RemoveTriggeredByNetworkPrefabList;
}
}
/// <summary>
/// Processes the <see cref="NetworkPrefabsList"/> if one is present for use during runtime execution,
/// else processes <see cref="Prefabs"/>.
/// </summary>
public void Initialize(bool warnInvalid = true)
{
if (NetworkPrefabsLists.Count != 0 && m_Prefabs.Count > 0)
{
NetworkLog.LogWarning("Runtime Network Prefabs was not empty at initialization time. Network " +
"Prefab registrations made before initialization will be replaced by NetworkPrefabsList.");
m_Prefabs.Clear();
}
foreach (var list in NetworkPrefabsLists)
{
list.OnAdd += AddTriggeredByNetworkPrefabList;
list.OnRemove += RemoveTriggeredByNetworkPrefabList;
}
NetworkPrefabOverrideLinks.Clear();
OverrideToNetworkPrefab.Clear();
var prefabs = NetworkPrefabsLists.Count != 0 ? new List<NetworkPrefab>() : m_Prefabs;
if (NetworkPrefabsLists.Count != 0)
{
foreach (var list in NetworkPrefabsLists)
{
foreach (var networkPrefab in list.PrefabList)
{
prefabs.Add(networkPrefab);
}
}
}
m_Prefabs = new List<NetworkPrefab>();
List<NetworkPrefab> removeList = null;
if (warnInvalid)
{
removeList = new List<NetworkPrefab>();
}
foreach (var networkPrefab in prefabs)
{
if (AddPrefabRegistration(networkPrefab))
{
m_Prefabs.Add(networkPrefab);
}
else
{
removeList?.Add(networkPrefab);
}
}
// Clear out anything that is invalid or not used
if (removeList?.Count > 0)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
{
var sb = new StringBuilder("Removing invalid prefabs from Network Prefab registration: ");
sb.Append(string.Join(", ", removeList));
NetworkLog.LogWarning(sb.ToString());
}
}
}
/// <summary>
/// Add a new NetworkPrefab instance to the list
/// </summary>
/// <remarks>
/// The framework does not synchronize this list between clients. Any runtime changes must be handled manually.
///
/// Any modifications made here are not persisted. Permanent configuration changes should be done
/// through the <see cref="NetworkPrefabsList"/> scriptable object property.
/// </remarks>
public bool Add(NetworkPrefab networkPrefab)
{
if (AddPrefabRegistration(networkPrefab))
{
m_Prefabs.Add(networkPrefab);
return true;
}
return false;
}
/// <summary>
/// Remove a NetworkPrefab instance from the list
/// </summary>
/// <remarks>
/// The framework does not synchronize this list between clients. Any runtime changes must be handled manually.
///
/// Any modifications made here are not persisted. Permanent configuration changes should be done
/// through the <see cref="NetworkPrefabsList"/> scriptable object property.
/// </remarks>
public void Remove(NetworkPrefab prefab)
{
if (prefab == null)
{
throw new ArgumentNullException(nameof(prefab));
}
m_Prefabs.Remove(prefab);
OverrideToNetworkPrefab.Remove(prefab.TargetPrefabGlobalObjectIdHash);
NetworkPrefabOverrideLinks.Remove(prefab.SourcePrefabGlobalObjectIdHash);
}
/// <summary>
/// Remove a NetworkPrefab instance with matching <see cref="NetworkPrefab.Prefab"/> from the list
/// </summary>
/// <remarks>
/// The framework does not synchronize this list between clients. Any runtime changes must be handled manually.
///
/// Any modifications made here are not persisted. Permanent configuration changes should be done
/// through the <see cref="NetworkPrefabsList"/> scriptable object property.
/// </remarks>
public void Remove(GameObject prefab)
{
if (prefab == null)
{
throw new ArgumentNullException(nameof(prefab));
}
for (int i = 0; i < m_Prefabs.Count; i++)
{
if (m_Prefabs[i].Prefab == prefab)
{
Remove(m_Prefabs[i]);
return;
}
}
}
/// <summary>
/// Check if the given GameObject is present as a prefab within the list
/// </summary>
/// <param name="prefab">The prefab to check</param>
/// <returns>Whether or not the prefab exists</returns>
public bool Contains(GameObject prefab)
{
for (int i = 0; i < m_Prefabs.Count; i++)
{
if (m_Prefabs[i].Prefab == prefab)
{
return true;
}
}
return false;
}
/// <summary>
/// Check if the given NetworkPrefab is present within the list
/// </summary>
/// <param name="prefab">The prefab to check</param>
/// <returns>Whether or not the prefab exists</returns>
public bool Contains(NetworkPrefab prefab)
{
for (int i = 0; i < m_Prefabs.Count; i++)
{
if (m_Prefabs[i].Equals(prefab))
{
return true;
}
}
return false;
}
/// <summary>
/// Configures <see cref="NetworkPrefabOverrideLinks"/> and <see cref="OverrideToNetworkPrefab"/> for the given <see cref="NetworkPrefab"/>
/// </summary>
private bool AddPrefabRegistration(NetworkPrefab networkPrefab)
{
if (networkPrefab == null)
{
return false;
}
// Safeguard validation check since this method is called from outside of NetworkConfig and we can't control what's passed in.
if (!networkPrefab.Validate())
{
return false;
}
uint source = networkPrefab.SourcePrefabGlobalObjectIdHash;
uint target = networkPrefab.TargetPrefabGlobalObjectIdHash;
// Make sure the prefab isn't already registered.
if (NetworkPrefabOverrideLinks.ContainsKey(source))
{
var networkObject = networkPrefab.Prefab.GetComponent<NetworkObject>();
// This should never happen, but in the case it somehow does log an error and remove the duplicate entry
Debug.LogError($"{nameof(NetworkPrefab)} ({networkObject.name}) has a duplicate {nameof(NetworkObject.GlobalObjectIdHash)} source entry value of: {source}!");
return false;
}
// If we don't have an override configured, registration is simple!
if (networkPrefab.Override == NetworkPrefabOverride.None)
{
NetworkPrefabOverrideLinks.Add(source, networkPrefab);
return true;
}
// Make sure we don't have several overrides targeting the same prefab. Apparently we don't support that... shame.
if (OverrideToNetworkPrefab.ContainsKey(target))
{
var networkObject = networkPrefab.Prefab.GetComponent<NetworkObject>();
// This can happen if a user tries to make several GlobalObjectIdHash values point to the same target
Debug.LogError($"{nameof(NetworkPrefab)} (\"{networkObject.name}\") has a duplicate {nameof(NetworkObject.GlobalObjectIdHash)} target entry value of: {target}!");
return false;
}
switch (networkPrefab.Override)
{
case NetworkPrefabOverride.Prefab:
{
NetworkPrefabOverrideLinks.Add(source, networkPrefab);
OverrideToNetworkPrefab.Add(target, source);
}
break;
case NetworkPrefabOverride.Hash:
{
NetworkPrefabOverrideLinks.Add(source, networkPrefab);
OverrideToNetworkPrefab.Add(target, source);
}
break;
}
return true;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 230fc75f5639e46dc91734aa67d56a3e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,95 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
namespace Unity.Netcode
{
/// <summary>
/// A ScriptableObject for holding a network prefabs list, which can be
/// shared between multiple NetworkManagers.
///
/// When NetworkManagers hold references to this list, modifications to the
/// list at runtime will be picked up by all NetworkManagers that reference it.
/// </summary>
[CreateAssetMenu(fileName = "NetworkPrefabsList", menuName = "Netcode/Network Prefabs List")]
public class NetworkPrefabsList : ScriptableObject
{
internal delegate void OnAddDelegate(NetworkPrefab prefab);
internal OnAddDelegate OnAdd;
internal delegate void OnRemoveDelegate(NetworkPrefab prefab);
internal OnRemoveDelegate OnRemove;
[SerializeField]
internal bool IsDefault;
[FormerlySerializedAs("Prefabs")]
[SerializeField]
internal List<NetworkPrefab> List = new List<NetworkPrefab>();
/// <summary>
/// Read-only view into the prefabs list, enabling iterating and examining the list.
/// Actually modifying the list should be done using <see cref="Add"/>
/// and <see cref="Remove"/>.
/// </summary>
public IReadOnlyList<NetworkPrefab> PrefabList => List;
/// <summary>
/// Adds a prefab to the prefab list. Performing this here will apply the operation to all
/// <see cref="NetworkManager"/>s that reference this list.
/// </summary>
/// <param name="prefab"></param>
public void Add(NetworkPrefab prefab)
{
List.Add(prefab);
OnAdd?.Invoke(prefab);
}
/// <summary>
/// Removes a prefab from the prefab list. Performing this here will apply the operation to all
/// <see cref="NetworkManager"/>s that reference this list.
/// </summary>
/// <param name="prefab"></param>
public void Remove(NetworkPrefab prefab)
{
List.Remove(prefab);
OnRemove?.Invoke(prefab);
}
/// <summary>
/// Check if the given GameObject is present as a prefab within the list
/// </summary>
/// <param name="prefab">The prefab to check</param>
/// <returns>Whether or not the prefab exists</returns>
public bool Contains(GameObject prefab)
{
for (int i = 0; i < List.Count; i++)
{
if (List[i].Prefab == prefab)
{
return true;
}
}
return false;
}
/// <summary>
/// Check if the given NetworkPrefab is present within the list
/// </summary>
/// <param name="prefab">The prefab to check</param>
/// <returns>Whether or not the prefab exists</returns>
public bool Contains(NetworkPrefab prefab)
{
for (int i = 0; i < List.Count; i++)
{
if (List[i].Equals(prefab))
{
return true;
}
}
return false;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e651dbb3fbac04af2b8f5abf007ddc23
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -314,18 +314,18 @@ namespace Unity.Netcode
/// <summary>
/// Gets if we are executing as server
/// </summary>
protected bool IsServer { get; private set; }
public bool IsServer { get; private set; }
/// <summary>
/// Gets if we are executing as client
/// </summary>
protected bool IsClient { get; private set; }
public bool IsClient { get; private set; }
/// <summary>
/// Gets if we are executing as Host, I.E Server and Client
/// </summary>
protected bool IsHost { get; private set; }
public bool IsHost { get; private set; }
/// <summary>
/// Gets Whether or not the object has a owner
@@ -570,11 +570,11 @@ namespace Unity.Netcode
if (list == null)
{
list = new List<FieldInfo>();
list.AddRange(type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance));
list.AddRange(type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
}
else
{
list.AddRange(type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance));
list.AddRange(type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
}
if (type.BaseType != null && type.BaseType != typeof(NetworkBehaviour))

View File

@@ -72,6 +72,23 @@ namespace Unity.Netcode
}
}
}
foreach (var dirtyObj in m_DirtyNetworkObjects)
{
for (int k = 0; k < dirtyObj.ChildNetworkBehaviours.Count; k++)
{
var behaviour = dirtyObj.ChildNetworkBehaviours[k];
for (int i = 0; i < behaviour.NetworkVariableFields.Count; i++)
{
if (behaviour.NetworkVariableFields[i].IsDirty() &&
!behaviour.NetworkVariableIndexesToResetSet.Contains(i))
{
behaviour.NetworkVariableIndexesToResetSet.Add(i);
behaviour.NetworkVariableIndexesToReset.Add(i);
}
}
}
}
// Now, reset all the no-longer-dirty variables
foreach (var dirtyobj in m_DirtyNetworkObjects)
{

View File

@@ -67,6 +67,9 @@ namespace Unity.Netcode
internal Dictionary<ulong, ConnectionApprovalResponse> ClientsToApprove = new Dictionary<ulong, ConnectionApprovalResponse>();
// Stores the objects that need to be shown at end-of-frame
internal Dictionary<ulong, List<NetworkObject>> ObjectsToShowToClient = new Dictionary<ulong, List<NetworkObject>>();
/// <summary>
/// The <see cref="NetworkPrefabHandler"/> instance created after starting the <see cref="NetworkManager"/>
/// </summary>
@@ -140,15 +143,55 @@ namespace Unity.Netcode
public bool OnVerifyCanReceive(ulong senderId, Type messageType, FastBufferReader messageContent, ref NetworkContext context)
{
if (m_NetworkManager.PendingClients.TryGetValue(senderId, out PendingClient client) &&
(client.ConnectionState == PendingClient.State.PendingApproval || (client.ConnectionState == PendingClient.State.PendingConnection && messageType != typeof(ConnectionRequestMessage))))
if (m_NetworkManager.IsServer)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
if (messageType == typeof(ConnectionApprovedMessage))
{
NetworkLog.LogWarning($"Message received from {nameof(senderId)}={senderId} before it has been accepted");
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
{
NetworkLog.LogError($"A {nameof(ConnectionApprovedMessage)} was received from a client on the server side. This should not happen. Please report this to the Netcode for GameObjects team at https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues and include the following data: Message Size: {messageContent.Length}. Message Content: {MessagingSystem.ByteArrayToString(messageContent.ToArray(), 0, messageContent.Length)}");
}
return false;
}
if (m_NetworkManager.PendingClients.TryGetValue(senderId, out PendingClient client) &&
(client.ConnectionState == PendingClient.State.PendingApproval || (client.ConnectionState == PendingClient.State.PendingConnection && messageType != typeof(ConnectionRequestMessage))))
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
{
NetworkLog.LogWarning($"Message received from {nameof(senderId)}={senderId} before it has been accepted");
}
return false;
}
return false;
if (m_NetworkManager.ConnectedClients.TryGetValue(senderId, out NetworkClient connectedClient) && messageType == typeof(ConnectionRequestMessage))
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
{
NetworkLog.LogError($"A {nameof(ConnectionRequestMessage)} was received from a client when the connection has already been established. This should not happen. Please report this to the Netcode for GameObjects team at https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues and include the following data: Message Size: {messageContent.Length}. Message Content: {MessagingSystem.ByteArrayToString(messageContent.ToArray(), 0, messageContent.Length)}");
}
return false;
}
}
else
{
if (messageType == typeof(ConnectionRequestMessage))
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
{
NetworkLog.LogError($"A {nameof(ConnectionRequestMessage)} was received from the server on the client side. This should not happen. Please report this to the Netcode for GameObjects team at https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues and include the following data: Message Size: {messageContent.Length}. Message Content: {MessagingSystem.ByteArrayToString(messageContent.ToArray(), 0, messageContent.Length)}");
}
return false;
}
if (m_NetworkManager.IsConnectedClient && messageType == typeof(ConnectionApprovedMessage))
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
{
NetworkLog.LogError($"A {nameof(ConnectionApprovedMessage)} was received from the server when the connection has already been established. This should not happen. Please report this to the Netcode for GameObjects team at https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues and include the following data: Message Size: {messageContent.Length}. Message Content: {MessagingSystem.ByteArrayToString(messageContent.ToArray(), 0, messageContent.Length)}");
}
return false;
}
}
return !m_NetworkManager.m_StopProcessingMessages;
@@ -195,14 +238,14 @@ namespace Unity.Netcode
{
if (gameObject.TryGetComponent<NetworkObject>(out var networkObject))
{
if (NetworkConfig.NetworkPrefabOverrideLinks.ContainsKey(networkObject.GlobalObjectIdHash))
if (NetworkConfig.Prefabs.NetworkPrefabOverrideLinks.ContainsKey(networkObject.GlobalObjectIdHash))
{
switch (NetworkConfig.NetworkPrefabOverrideLinks[networkObject.GlobalObjectIdHash].Override)
switch (NetworkConfig.Prefabs.NetworkPrefabOverrideLinks[networkObject.GlobalObjectIdHash].Override)
{
case NetworkPrefabOverride.Hash:
case NetworkPrefabOverride.Prefab:
{
return NetworkConfig.NetworkPrefabOverrideLinks[networkObject.GlobalObjectIdHash].OverridingTargetPrefab;
return NetworkConfig.Prefabs.NetworkPrefabOverrideLinks[networkObject.GlobalObjectIdHash].OverridingTargetPrefab;
}
}
}
@@ -280,7 +323,7 @@ namespace Unity.Netcode
/// </summary>
public ulong LocalClientId
{
get => IsServer ? NetworkConfig.NetworkTransport.ServerClientId : m_LocalClientId;
get => m_LocalClientId;
internal set => m_LocalClientId = value;
}
@@ -508,6 +551,19 @@ namespace Unity.Netcode
internal static event Action OnSingletonReady;
#if UNITY_EDITOR
internal delegate void ResetNetworkManagerDelegate(NetworkManager manager);
internal static ResetNetworkManagerDelegate OnNetworkManagerReset;
#endif
private void Reset()
{
#if UNITY_EDITOR
OnNetworkManagerReset?.Invoke(this);
#endif
}
#if UNITY_EDITOR
internal void OnValidate()
{
@@ -533,72 +589,38 @@ namespace Unity.Netcode
}
// During OnValidate we will always clear out NetworkPrefabOverrideLinks and rebuild it
NetworkConfig.NetworkPrefabOverrideLinks.Clear();
NetworkConfig.Prefabs.NetworkPrefabOverrideLinks.Clear();
var prefabs = NetworkConfig.Prefabs.Prefabs;
// Check network prefabs and assign to dictionary for quick look up
for (int i = 0; i < NetworkConfig.NetworkPrefabs.Count; i++)
for (int i = 0; i < prefabs.Count; i++)
{
var networkPrefab = NetworkConfig.NetworkPrefabs[i];
var networkPrefab = prefabs[i];
var networkPrefabGo = networkPrefab?.Prefab;
if (networkPrefabGo != null)
if (networkPrefabGo == null)
{
if (!networkPrefabGo.TryGetComponent<NetworkObject>(out var networkObject))
continue;
}
var networkObject = networkPrefabGo.GetComponent<NetworkObject>();
if (networkObject == null)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
{
NetworkLog.LogError($"Cannot register {PrefabDebugHelper(networkPrefab)}, it does not have a {nameof(NetworkObject)} component at its root");
}
continue;
}
{
var childNetworkObjects = new List<NetworkObject>();
networkPrefabGo.GetComponentsInChildren(true, childNetworkObjects);
if (childNetworkObjects.Count > 1) // total count = 1 root NetworkObject + n child NetworkObjects
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
{
NetworkLog.LogError($"Cannot register {PrefabDebugHelper(networkPrefab)}, it does not have a {nameof(NetworkObject)} component at its root");
}
}
else
{
{
var childNetworkObjects = new List<NetworkObject>();
networkPrefabGo.GetComponentsInChildren(true, childNetworkObjects);
if (childNetworkObjects.Count > 1) // total count = 1 root NetworkObject + n child NetworkObjects
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
{
NetworkLog.LogWarning($"{PrefabDebugHelper(networkPrefab)} has child {nameof(NetworkObject)}(s) but they will not be spawned across the network (unsupported {nameof(NetworkPrefab)} setup)");
}
}
}
// Default to the standard NetworkPrefab.Prefab's NetworkObject first
var globalObjectIdHash = networkObject.GlobalObjectIdHash;
// Now check to see if it has an override
switch (networkPrefab.Override)
{
case NetworkPrefabOverride.Prefab:
{
if (NetworkConfig.NetworkPrefabs[i].SourcePrefabToOverride == null &&
NetworkConfig.NetworkPrefabs[i].Prefab != null)
{
if (networkPrefab.SourcePrefabToOverride == null)
{
networkPrefab.SourcePrefabToOverride = networkPrefabGo;
}
globalObjectIdHash = networkPrefab.SourcePrefabToOverride.GetComponent<NetworkObject>().GlobalObjectIdHash;
}
break;
}
case NetworkPrefabOverride.Hash:
globalObjectIdHash = networkPrefab.SourceHashToOverride;
break;
}
// Add to the NetworkPrefabOverrideLinks or handle a new (blank) entries
if (!NetworkConfig.NetworkPrefabOverrideLinks.ContainsKey(globalObjectIdHash))
{
NetworkConfig.NetworkPrefabOverrideLinks.Add(globalObjectIdHash, networkPrefab);
}
else
{
// Duplicate entries can happen when adding a new entry into a list of existing entries
// Either this is user error or a new entry, either case we replace it with a new, blank, NetworkPrefab under this condition
NetworkConfig.NetworkPrefabs[i] = new NetworkPrefab();
NetworkLog.LogWarning($"{PrefabDebugHelper(networkPrefab)} has child {nameof(NetworkObject)}(s) but they will not be spawned across the network (unsupported {nameof(NetworkPrefab)} setup)");
}
}
}
@@ -640,22 +662,9 @@ namespace Unity.Netcode
}
var networkPrefab = new NetworkPrefab { Prefab = prefab };
NetworkConfig.NetworkPrefabs.Add(networkPrefab);
if (IsListening)
bool added = NetworkConfig.Prefabs.Add(networkPrefab);
if (IsListening && added)
{
var sourcePrefabGlobalObjectIdHash = (uint)0;
var targetPrefabGlobalObjectIdHash = (uint)0;
if (!ShouldAddPrefab(networkPrefab, out sourcePrefabGlobalObjectIdHash, out targetPrefabGlobalObjectIdHash))
{
NetworkConfig.NetworkPrefabs.Remove(networkPrefab);
return;
}
if (!AddPrefabRegistration(networkPrefab, sourcePrefabGlobalObjectIdHash, targetPrefabGlobalObjectIdHash))
{
NetworkConfig.NetworkPrefabs.Remove(networkPrefab);
return;
}
DeferredMessageManager.ProcessTriggers(IDeferredMessageManager.TriggerType.OnAddPrefab, networkObject.GlobalObjectIdHash);
}
}
@@ -678,221 +687,14 @@ namespace Unity.Netcode
}
var globalObjectIdHash = prefab.GetComponent<NetworkObject>().GlobalObjectIdHash;
for (var i = 0; i < NetworkConfig.NetworkPrefabs.Count; ++i)
{
if (NetworkConfig.NetworkPrefabs[i].Prefab.GetComponent<NetworkObject>().GlobalObjectIdHash == globalObjectIdHash)
{
NetworkConfig.NetworkPrefabs.RemoveAt(i);
break;
}
}
NetworkConfig.Prefabs.Remove(prefab);
if (PrefabHandler.ContainsHandler(globalObjectIdHash))
{
PrefabHandler.RemoveHandler(globalObjectIdHash);
}
if (NetworkConfig.NetworkPrefabOverrideLinks.TryGetValue(globalObjectIdHash, out var targetPrefab))
{
NetworkConfig.NetworkPrefabOverrideLinks.Remove(globalObjectIdHash);
var targetHash = targetPrefab.Prefab.GetComponent<NetworkObject>().GlobalObjectIdHash;
if (NetworkConfig.OverrideToNetworkPrefab.ContainsKey(targetHash))
{
NetworkConfig.OverrideToNetworkPrefab.Remove(targetHash);
}
}
}
private bool ShouldAddPrefab(NetworkPrefab networkPrefab, out uint sourcePrefabGlobalObjectIdHash, out uint targetPrefabGlobalObjectIdHash, int index = -1)
{
sourcePrefabGlobalObjectIdHash = 0;
targetPrefabGlobalObjectIdHash = 0;
var networkObject = (NetworkObject)null;
if (networkPrefab == null || (networkPrefab.Prefab == null && networkPrefab.Override == NetworkPrefabOverride.None))
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
{
NetworkLog.LogWarning(
$"{nameof(NetworkPrefab)} cannot be null ({nameof(NetworkPrefab)} at index: {index})");
}
return false;
}
else if (networkPrefab.Override == NetworkPrefabOverride.None)
{
if (!networkPrefab.Prefab.TryGetComponent(out networkObject))
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
{
NetworkLog.LogWarning($"{PrefabDebugHelper(networkPrefab)} is missing " +
$"a {nameof(NetworkObject)} component (entry will be ignored).");
}
return false;
}
// Otherwise get the GlobalObjectIdHash value
sourcePrefabGlobalObjectIdHash = networkObject.GlobalObjectIdHash;
}
else // Validate Overrides
{
// Validate source prefab override values first
switch (networkPrefab.Override)
{
case NetworkPrefabOverride.Hash:
{
if (networkPrefab.SourceHashToOverride == 0)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
{
NetworkLog.LogWarning($"{nameof(NetworkPrefab)} {nameof(NetworkPrefab.SourceHashToOverride)} is zero " +
"(entry will be ignored).");
}
return false;
}
sourcePrefabGlobalObjectIdHash = networkPrefab.SourceHashToOverride;
break;
}
case NetworkPrefabOverride.Prefab:
{
if (networkPrefab.SourcePrefabToOverride == null)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
{
NetworkLog.LogWarning($"{nameof(NetworkPrefab)} {nameof(NetworkPrefab.SourcePrefabToOverride)} is null (entry will be ignored).");
}
Debug.LogWarning($"{nameof(NetworkPrefab)} override entry {networkPrefab.SourceHashToOverride} will be removed and ignored.");
return false;
}
else
{
if (!networkPrefab.SourcePrefabToOverride.TryGetComponent(out networkObject))
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
{
NetworkLog.LogWarning($"{nameof(NetworkPrefab)} ({networkPrefab.SourcePrefabToOverride.name}) " +
$"is missing a {nameof(NetworkObject)} component (entry will be ignored).");
}
Debug.LogWarning($"{nameof(NetworkPrefab)} override entry (\"{networkPrefab.SourcePrefabToOverride.name}\") will be removed and ignored.");
return false;
}
sourcePrefabGlobalObjectIdHash = networkObject.GlobalObjectIdHash;
}
break;
}
}
// Validate target prefab override values next
if (networkPrefab.OverridingTargetPrefab == null)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
{
NetworkLog.LogWarning($"{nameof(NetworkPrefab)} {nameof(NetworkPrefab.OverridingTargetPrefab)} is null!");
}
switch (networkPrefab.Override)
{
case NetworkPrefabOverride.Hash:
{
Debug.LogWarning($"{nameof(NetworkPrefab)} override entry {networkPrefab.SourceHashToOverride} will be removed and ignored.");
break;
}
case NetworkPrefabOverride.Prefab:
{
Debug.LogWarning($"{nameof(NetworkPrefab)} override entry ({networkPrefab.SourcePrefabToOverride.name}) will be removed and ignored.");
break;
}
}
return false;
}
else
{
targetPrefabGlobalObjectIdHash = networkPrefab.OverridingTargetPrefab.GetComponent<NetworkObject>().GlobalObjectIdHash;
}
}
return true;
}
internal bool AddPrefabRegistration(NetworkPrefab networkPrefab, uint sourcePrefabGlobalObjectIdHash, uint targetPrefabGlobalObjectIdHash)
{
// Assign the appropriate GlobalObjectIdHash to the appropriate NetworkPrefab
if (!NetworkConfig.NetworkPrefabOverrideLinks.ContainsKey(sourcePrefabGlobalObjectIdHash))
{
if (networkPrefab.Override == NetworkPrefabOverride.None)
{
NetworkConfig.NetworkPrefabOverrideLinks.Add(sourcePrefabGlobalObjectIdHash, networkPrefab);
}
else
{
if (!NetworkConfig.OverrideToNetworkPrefab.ContainsKey(targetPrefabGlobalObjectIdHash))
{
switch (networkPrefab.Override)
{
case NetworkPrefabOverride.Prefab:
{
NetworkConfig.NetworkPrefabOverrideLinks.Add(sourcePrefabGlobalObjectIdHash, networkPrefab);
NetworkConfig.OverrideToNetworkPrefab.Add(targetPrefabGlobalObjectIdHash, sourcePrefabGlobalObjectIdHash);
}
break;
case NetworkPrefabOverride.Hash:
{
NetworkConfig.NetworkPrefabOverrideLinks.Add(sourcePrefabGlobalObjectIdHash, networkPrefab);
NetworkConfig.OverrideToNetworkPrefab.Add(targetPrefabGlobalObjectIdHash, sourcePrefabGlobalObjectIdHash);
}
break;
}
}
else
{
var networkObject = networkPrefab.Prefab.GetComponent<NetworkObject>();
// This can happen if a user tries to make several GlobalObjectIdHash values point to the same target
Debug.LogError($"{nameof(NetworkPrefab)} (\"{networkObject.name}\") has a duplicate {nameof(NetworkObject.GlobalObjectIdHash)} target entry value of: {targetPrefabGlobalObjectIdHash}! Removing entry from list!");
return false;
}
}
}
else
{
var networkObject = networkPrefab.Prefab.GetComponent<NetworkObject>();
// This should never happen, but in the case it somehow does log an error and remove the duplicate entry
Debug.LogError($"{nameof(NetworkPrefab)} ({networkObject.name}) has a duplicate {nameof(NetworkObject.GlobalObjectIdHash)} source entry value of: {sourcePrefabGlobalObjectIdHash}! Removing entry from list!");
return false;
}
return true;
}
private void InitializePrefabs(int startIdx = 0)
{
// This is used to remove entries not needed or invalid
var removeEmptyPrefabs = new List<int>();
// Build the NetworkPrefabOverrideLinks dictionary
for (int i = startIdx; i < NetworkConfig.NetworkPrefabs.Count; i++)
{
var sourcePrefabGlobalObjectIdHash = (uint)0;
var targetPrefabGlobalObjectIdHash = (uint)0;
if (!ShouldAddPrefab(NetworkConfig.NetworkPrefabs[i], out sourcePrefabGlobalObjectIdHash, out targetPrefabGlobalObjectIdHash, i))
{
removeEmptyPrefabs.Add(i);
continue;
}
if (!AddPrefabRegistration(NetworkConfig.NetworkPrefabs[i], sourcePrefabGlobalObjectIdHash, targetPrefabGlobalObjectIdHash))
{
removeEmptyPrefabs.Add(i);
continue;
}
}
// Clear out anything that is invalid or not used (for invalid entries we already logged warnings to the user earlier)
// Iterate backwards so indices don't shift as we remove
for (int i = removeEmptyPrefabs.Count - 1; i >= 0; i--)
{
NetworkConfig.NetworkPrefabs.RemoveAt(removeEmptyPrefabs[i]);
}
removeEmptyPrefabs.Clear();
}
private void Initialize(bool server)
internal void Initialize(bool server)
{
// Don't allow the user to start a network session if the NetworkManager is
// still parented under another GameObject
@@ -982,11 +784,7 @@ namespace Unity.Netcode
this.RegisterNetworkUpdate(NetworkUpdateStage.PreUpdate);
// Always clear our prefab override links before building
NetworkConfig.NetworkPrefabOverrideLinks.Clear();
NetworkConfig.OverrideToNetworkPrefab.Clear();
InitializePrefabs();
NetworkConfig.InitializePrefabs();
// If we have a player prefab, then we need to verify it is in the list of NetworkPrefabOverrideLinks for client side spawning.
if (NetworkConfig.PlayerPrefab != null)
@@ -994,15 +792,11 @@ namespace Unity.Netcode
if (NetworkConfig.PlayerPrefab.TryGetComponent<NetworkObject>(out var playerPrefabNetworkObject))
{
//In the event there is no NetworkPrefab entry (i.e. no override for default player prefab)
if (!NetworkConfig.NetworkPrefabOverrideLinks.ContainsKey(playerPrefabNetworkObject
if (!NetworkConfig.Prefabs.NetworkPrefabOverrideLinks.ContainsKey(playerPrefabNetworkObject
.GlobalObjectIdHash))
{
//Then add a new entry for the player prefab
var playerNetworkPrefab = new NetworkPrefab();
playerNetworkPrefab.Prefab = NetworkConfig.PlayerPrefab;
NetworkConfig.NetworkPrefabs.Insert(0, playerNetworkPrefab);
NetworkConfig.NetworkPrefabOverrideLinks.Add(playerPrefabNetworkObject.GlobalObjectIdHash,
playerNetworkPrefab);
AddNetworkPrefab(NetworkConfig.PlayerPrefab);
}
}
else
@@ -1048,6 +842,7 @@ namespace Unity.Netcode
IsServer = true;
IsClient = false;
IsListening = true;
LocalClientId = ServerClientId;
try
{
@@ -1282,6 +1077,8 @@ namespace Unity.Netcode
private void Awake()
{
NetworkConfig?.InitializePrefabs();
UnityEngine.SceneManagement.SceneManager.sceneUnloaded += OnSceneUnloaded;
}
@@ -1663,6 +1460,17 @@ namespace Unity.Netcode
// Do NetworkVariable updates
BehaviourUpdater.NetworkBehaviourUpdate(this);
// Handle NetworkObjects to show
foreach (var client in ObjectsToShowToClient)
{
ulong clientId = client.Key;
foreach (var networkObject in client.Value)
{
SpawnManager.SendSpawnCallForObject(clientId, networkObject);
}
}
ObjectsToShowToClient.Clear();
int timeSyncFrequencyTicks = (int)(k_TimeSyncFrequency * NetworkConfig.TickRate);
if (IsServer && NetworkTickSystem.ServerTime.Tick % timeSyncFrequencyTicks == 0)
{
@@ -2082,7 +1890,12 @@ namespace Unity.Netcode
}
else
{
Destroy(playerObject.gameObject);
// Call despawn to assure NetworkBehaviour.OnNetworkDespawn is invoked
// on the server-side (when the client side disconnected).
// This prevents the issue (when just destroying the GameObject) where
// any NetworkBehaviour component(s) destroyed before the NetworkObject
// would not have OnNetworkDespawn invoked.
SpawnManager.DespawnObject(playerObject, true);
}
}
else
@@ -2334,5 +2147,37 @@ namespace Unity.Netcode
NetworkMetrics.TrackObjectSpawnSent(clientPair.Key, ConnectedClients[clientId].PlayerObject, size);
}
}
internal void MarkObjectForShowingTo(NetworkObject networkObject, ulong clientId)
{
if (!ObjectsToShowToClient.ContainsKey(clientId))
{
ObjectsToShowToClient.Add(clientId, new List<NetworkObject>());
}
ObjectsToShowToClient[clientId].Add(networkObject);
}
// returns whether any matching objects would have become visible and were returned to hidden state
internal bool RemoveObjectFromShowingTo(NetworkObject networkObject, ulong clientId)
{
var ret = false;
if (!ObjectsToShowToClient.ContainsKey(clientId))
{
return false;
}
// probably overkill, but deals with multiple entries
while (ObjectsToShowToClient[clientId].Contains(networkObject))
{
Debug.LogWarning(
"Object was shown and hidden from the same client in the same Network frame. As a result, the client will _not_ receive a NetworkSpawn");
ObjectsToShowToClient[clientId].Remove(networkObject);
ret = true;
}
networkObject.Observers.Remove(clientId);
return ret;
}
}
}

View File

@@ -265,9 +265,8 @@ namespace Unity.Netcode
throw new VisibilityChangeException("The object is already visible");
}
NetworkManager.MarkObjectForShowingTo(this, clientId);
Observers.Add(clientId);
NetworkManager.SpawnManager.SendSpawnCallForObject(clientId, this);
}
@@ -351,26 +350,28 @@ namespace Unity.Netcode
throw new NotServerException("Only server can change visibility");
}
if (!Observers.Contains(clientId))
{
throw new VisibilityChangeException("The object is already hidden");
}
if (clientId == NetworkManager.ServerClientId)
{
throw new VisibilityChangeException("Cannot hide an object from the server");
}
Observers.Remove(clientId);
var message = new DestroyObjectMessage
if (!NetworkManager.RemoveObjectFromShowingTo(this, clientId))
{
NetworkObjectId = NetworkObjectId,
DestroyGameObject = !IsSceneObject.Value
};
// Send destroy call
var size = NetworkManager.SendMessage(ref message, NetworkDelivery.ReliableSequenced, clientId);
NetworkManager.NetworkMetrics.TrackObjectDestroySent(clientId, this, size);
if (!Observers.Contains(clientId))
{
throw new VisibilityChangeException("The object is already hidden");
}
Observers.Remove(clientId);
var message = new DestroyObjectMessage
{
NetworkObjectId = NetworkObjectId,
DestroyGameObject = !IsSceneObject.Value
};
// Send destroy call
var size = NetworkManager.SendMessage(ref message, NetworkDelivery.ReliableSequenced, clientId);
NetworkManager.NetworkMetrics.TrackObjectDestroySent(clientId, this, size);
}
}
/// <summary>
@@ -1454,9 +1455,9 @@ namespace Unity.Netcode
var globalObjectIdHash = NetworkManager.PrefabHandler.GetSourceGlobalObjectIdHash(GlobalObjectIdHash);
return globalObjectIdHash == 0 ? GlobalObjectIdHash : globalObjectIdHash;
}
else if (NetworkManager.NetworkConfig.OverrideToNetworkPrefab.ContainsKey(GlobalObjectIdHash))
if (NetworkManager.NetworkConfig.Prefabs.OverrideToNetworkPrefab.TryGetValue(GlobalObjectIdHash, out uint hash))
{
return NetworkManager.NetworkConfig.OverrideToNetworkPrefab[GlobalObjectIdHash];
return hash;
}
}

View File

@@ -5,9 +5,26 @@ namespace Unity.Netcode
/// </summary>
internal struct BatchHeader : INetworkSerializeByMemcpy
{
internal const ushort MagicValue = 0x1160;
/// <summary>
/// A magic number to detect corrupt messages.
/// Always set to k_MagicValue
/// </summary>
public ushort Magic;
/// <summary>
/// Total number of bytes in the batch.
/// </summary>
public int BatchSize;
/// <summary>
/// Hash of the message to detect corrupt messages.
/// </summary>
public ulong BatchHash;
/// <summary>
/// Total number of messages in the batch.
/// </summary>
public ushort BatchSize;
public ushort BatchCount;
}
}

View File

@@ -64,6 +64,16 @@ namespace Unity.Netcode
shouldWrite = false;
}
// The object containing the behaviour we're about to process is about to be shown to this client
// As a result, the client will get the fully serialized NetworkVariable and would be confused by
// an extraneous delta
if (NetworkBehaviour.NetworkManager.ObjectsToShowToClient.ContainsKey(TargetClientId) &&
NetworkBehaviour.NetworkManager.ObjectsToShowToClient[TargetClientId]
.Contains(NetworkBehaviour.NetworkObject))
{
shouldWrite = false;
}
if (NetworkBehaviour.NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety)
{
if (!shouldWrite)
@@ -96,12 +106,6 @@ namespace Unity.Netcode
networkVariable.WriteDelta(writer);
}
if (!NetworkBehaviour.NetworkVariableIndexesToResetSet.Contains(i))
{
NetworkBehaviour.NetworkVariableIndexesToResetSet.Add(i);
NetworkBehaviour.NetworkVariableIndexesToReset.Add(i);
}
NetworkBehaviour.NetworkManager.NetworkMetrics.TrackNetworkVariableDeltaSent(
TargetClientId,
NetworkBehaviour.NetworkObject,

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
@@ -41,7 +42,7 @@ namespace Unity.Netcode
{
Writer = new FastBufferWriter(writerSize, writerAllocator, maxWriterSize);
NetworkDelivery = delivery;
BatchHeader = default;
BatchHeader = new BatchHeader { Magic = BatchHeader.MagicValue };
}
}
@@ -204,6 +205,17 @@ namespace Unity.Netcode
return m_LocalVersions[messageType];
}
internal static string ByteArrayToString(byte[] ba, int offset, int count)
{
var hex = new StringBuilder(ba.Length * 2);
for (int i = offset; i < offset + count; ++i)
{
hex.AppendFormat("{0:x2} ", ba[i]);
}
return hex.ToString();
}
internal void HandleIncomingData(ulong clientId, ArraySegment<byte> data, float receiveTime)
{
unsafe
@@ -214,18 +226,38 @@ namespace Unity.Netcode
new FastBufferReader(nativeData + data.Offset, Allocator.None, data.Count);
if (!batchReader.TryBeginRead(sizeof(BatchHeader)))
{
NetworkLog.LogWarning("Received a packet too small to contain a BatchHeader. Ignoring it.");
NetworkLog.LogError("Received a packet too small to contain a BatchHeader. Ignoring it.");
return;
}
batchReader.ReadValue(out BatchHeader batchHeader);
for (var hookIdx = 0; hookIdx < m_Hooks.Count; ++hookIdx)
if (batchHeader.Magic != BatchHeader.MagicValue)
{
m_Hooks[hookIdx].OnBeforeReceiveBatch(clientId, batchHeader.BatchSize, batchReader.Length);
NetworkLog.LogError($"Received a packet with an invalid Magic Value. Please report this to the Netcode for GameObjects team at https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues and include the following data: Offset: {data.Offset}, Size: {data.Count}, Full receive array: {ByteArrayToString(data.Array, 0, data.Array.Length)}");
return;
}
for (var messageIdx = 0; messageIdx < batchHeader.BatchSize; ++messageIdx)
if (batchHeader.BatchSize != data.Count)
{
NetworkLog.LogError($"Received a packet with an invalid Batch Size Value. Please report this to the Netcode for GameObjects team at https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues and include the following data: Offset: {data.Offset}, Size: {data.Count}, Expected Size: {batchHeader.BatchSize}, Full receive array: {ByteArrayToString(data.Array, 0, data.Array.Length)}");
return;
}
var hash = XXHash.Hash64(batchReader.GetUnsafePtrAtCurrentPosition(), batchReader.Length - batchReader.Position);
if (hash != batchHeader.BatchHash)
{
NetworkLog.LogError($"Received a packet with an invalid Hash Value. Please report this to the Netcode for GameObjects team at https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues and include the following data: Received Hash: {batchHeader.BatchHash}, Calculated Hash: {hash}, Offset: {data.Offset}, Size: {data.Count}, Full receive array: {ByteArrayToString(data.Array, 0, data.Array.Length)}");
return;
}
for (var hookIdx = 0; hookIdx < m_Hooks.Count; ++hookIdx)
{
m_Hooks[hookIdx].OnBeforeReceiveBatch(clientId, batchHeader.BatchCount, batchReader.Length);
}
for (var messageIdx = 0; messageIdx < batchHeader.BatchCount; ++messageIdx)
{
var messageHeader = new MessageHeader();
@@ -237,7 +269,7 @@ namespace Unity.Netcode
}
catch (OverflowException)
{
NetworkLog.LogWarning("Received a batch that didn't have enough data for all of its batches, ending early!");
NetworkLog.LogError("Received a batch that didn't have enough data for all of its batches, ending early!");
throw;
}
@@ -245,7 +277,7 @@ namespace Unity.Netcode
if (!batchReader.TryBeginRead((int)messageHeader.MessageSize))
{
NetworkLog.LogWarning("Received a message that claimed a size larger than the packet, ending early!");
NetworkLog.LogError("Received a message that claimed a size larger than the packet, ending early!");
return;
}
m_IncomingMessageQueue.Add(new ReceiveQueueItem
@@ -263,7 +295,7 @@ namespace Unity.Netcode
}
for (var hookIdx = 0; hookIdx < m_Hooks.Count; ++hookIdx)
{
m_Hooks[hookIdx].OnAfterReceiveBatch(clientId, batchHeader.BatchSize, batchReader.Length);
m_Hooks[hookIdx].OnAfterReceiveBatch(clientId, batchHeader.BatchCount, batchReader.Length);
}
}
}
@@ -650,7 +682,7 @@ namespace Unity.Netcode
writeQueueItem.Writer.WriteBytes(headerSerializer.GetUnsafePtr(), headerSerializer.Length);
writeQueueItem.Writer.WriteBytes(tmpSerializer.GetUnsafePtr(), tmpSerializer.Length);
writeQueueItem.BatchHeader.BatchSize++;
writeQueueItem.BatchHeader.BatchCount++;
for (var hookIdx = 0; hookIdx < m_Hooks.Count; ++hookIdx)
{
m_Hooks[hookIdx].OnAfterSendMessage(clientId, ref message, delivery, tmpSerializer.Length + headerSerializer.Length);
@@ -745,7 +777,7 @@ namespace Unity.Netcode
for (var i = 0; i < sendQueueItem.Length; ++i)
{
ref var queueItem = ref sendQueueItem.ElementAt(i);
if (queueItem.BatchHeader.BatchSize == 0)
if (queueItem.BatchHeader.BatchCount == 0)
{
queueItem.Writer.Dispose();
continue;
@@ -753,23 +785,28 @@ namespace Unity.Netcode
for (var hookIdx = 0; hookIdx < m_Hooks.Count; ++hookIdx)
{
m_Hooks[hookIdx].OnBeforeSendBatch(clientId, queueItem.BatchHeader.BatchSize, queueItem.Writer.Length, queueItem.NetworkDelivery);
m_Hooks[hookIdx].OnBeforeSendBatch(clientId, queueItem.BatchHeader.BatchCount, queueItem.Writer.Length, queueItem.NetworkDelivery);
}
queueItem.Writer.Seek(0);
#if UNITY_EDITOR || DEVELOPMENT_BUILD
// Skipping the Verify and sneaking the write mark in because we know it's fine.
queueItem.Writer.Handle->AllowedWriteMark = 2;
queueItem.Writer.Handle->AllowedWriteMark = sizeof(BatchHeader);
#endif
queueItem.BatchHeader.BatchHash = XXHash.Hash64(queueItem.Writer.GetUnsafePtr() + sizeof(BatchHeader), queueItem.Writer.Length - sizeof(BatchHeader));
queueItem.BatchHeader.BatchSize = queueItem.Writer.Length;
queueItem.Writer.WriteValue(queueItem.BatchHeader);
try
{
m_MessageSender.Send(clientId, queueItem.NetworkDelivery, queueItem.Writer);
for (var hookIdx = 0; hookIdx < m_Hooks.Count; ++hookIdx)
{
m_Hooks[hookIdx].OnAfterSendBatch(clientId, queueItem.BatchHeader.BatchSize, queueItem.Writer.Length, queueItem.NetworkDelivery);
m_Hooks[hookIdx].OnAfterSendBatch(clientId, queueItem.BatchHeader.BatchCount, queueItem.Writer.Length, queueItem.NetworkDelivery);
}
}
finally

View File

@@ -18,6 +18,11 @@ namespace Unity.Netcode
/// </summary>
private protected NetworkBehaviour m_NetworkBehaviour;
public NetworkBehaviour GetBehaviour()
{
return m_NetworkBehaviour;
}
/// <summary>
/// Initializes the NetworkVariable
/// </summary>

View File

@@ -862,16 +862,6 @@ namespace Unity.Netcode
SceneEventProgressTracking.Add(sceneEventProgress.Guid, sceneEventProgress);
if (!isUnloading)
{
// The Condition: While a scene is asynchronously loaded in single loading scene mode, if any new NetworkObjects are spawned
// they need to be moved into the do not destroy temporary scene
// When it is set: Just before starting the asynchronous loading call
// When it is unset: After the scene has loaded, the PopulateScenePlacedObjects is called, and all NetworkObjects in the do
// not destroy temporary scene are moved into the active scene
IsSpawnedObjectsPendingInDontDestroyOnLoad = true;
}
m_IsSceneEventActive = true;
// Set our callback delegate handler for completion
@@ -1162,6 +1152,13 @@ namespace Unity.Netcode
if (sceneEventData.LoadSceneMode == LoadSceneMode.Single)
{
// The Condition: While a scene is asynchronously loaded in single loading scene mode, if any new NetworkObjects are spawned
// they need to be moved into the do not destroy temporary scene
// When it is set: Just before starting the asynchronous loading call
// When it is unset: After the scene has loaded, the PopulateScenePlacedObjects is called, and all NetworkObjects in the do
// not destroy temporary scene are moved into the active scene
IsSpawnedObjectsPendingInDontDestroyOnLoad = true;
// Destroy current scene objects before switching.
m_NetworkManager.SpawnManager.ServerDestroySpawnedSceneObjects();

View File

@@ -7,14 +7,10 @@ namespace Unity.Netcode
/// <summary>
/// Two-way serializer wrapping FastBufferReader or FastBufferWriter.
///
/// Implemented as a ref struct for two reasons:
/// 1. The BufferSerializer cannot outlive the FBR/FBW it wraps or using it will cause a crash
/// 2. The BufferSerializer must always be passed by reference and can't be copied
/// Implemented as a ref struct to help enforce the requirement that
/// the BufferSerializer cannot outlive the FBR/FBW it wraps or using it will cause a crash
///
/// Ref structs help enforce both of those rules: they can't ref live the stack context in which they were
/// created, and they're always passed by reference no matter what.
///
/// BufferSerializer doesn't wrapp FastBufferReader or FastBufferWriter directly because it can't.
/// BufferSerializer doesn't wrap FastBufferReader or FastBufferWriter directly because it can't.
/// ref structs can't implement interfaces, and in order to be able to have two different implementations with
/// the same interface (which allows us to avoid an "if(IsReader)" on every call), the thing directly wrapping
/// the struct has to implement an interface. So IReaderWriter exists as the interface,

View File

@@ -300,7 +300,7 @@ namespace Unity.Netcode
{
return true;
}
if (NetworkManager.NetworkConfig.NetworkPrefabOverrideLinks.TryGetValue(sceneObject.Hash, out var networkPrefab))
if (NetworkManager.NetworkConfig.Prefabs.NetworkPrefabOverrideLinks.TryGetValue(sceneObject.Hash, out var networkPrefab))
{
switch (networkPrefab.Override)
{
@@ -352,17 +352,17 @@ namespace Unity.Netcode
{
// See if there is a valid registered NetworkPrefabOverrideLink associated with the provided prefabHash
GameObject networkPrefabReference = null;
if (NetworkManager.NetworkConfig.NetworkPrefabOverrideLinks.ContainsKey(globalObjectIdHash))
if (NetworkManager.NetworkConfig.Prefabs.NetworkPrefabOverrideLinks.ContainsKey(globalObjectIdHash))
{
switch (NetworkManager.NetworkConfig.NetworkPrefabOverrideLinks[globalObjectIdHash].Override)
switch (NetworkManager.NetworkConfig.Prefabs.NetworkPrefabOverrideLinks[globalObjectIdHash].Override)
{
default:
case NetworkPrefabOverride.None:
networkPrefabReference = NetworkManager.NetworkConfig.NetworkPrefabOverrideLinks[globalObjectIdHash].Prefab;
networkPrefabReference = NetworkManager.NetworkConfig.Prefabs.NetworkPrefabOverrideLinks[globalObjectIdHash].Prefab;
break;
case NetworkPrefabOverride.Hash:
case NetworkPrefabOverride.Prefab:
networkPrefabReference = NetworkManager.NetworkConfig.NetworkPrefabOverrideLinks[globalObjectIdHash].OverridingTargetPrefab;
networkPrefabReference = NetworkManager.NetworkConfig.Prefabs.NetworkPrefabOverrideLinks[globalObjectIdHash].OverridingTargetPrefab;
break;
}
}
@@ -603,6 +603,11 @@ namespace Unity.Netcode
var children = networkObject.GetComponentsInChildren<NetworkObject>();
foreach (var childObject in children)
{
// Do not propagate the in-scene object setting if a child was dynamically spawned.
if (childObject.IsSceneObject.HasValue && !childObject.IsSceneObject.Value)
{
continue;
}
childObject.IsSceneObject = sceneObject;
}
}

View File

@@ -226,7 +226,7 @@ namespace Unity.Netcode.Transports.UTP
{
writer.WriteInt(messageLength);
var messageOffset = HeadIndex + reader.GetBytesRead();
var messageOffset = reader.GetBytesRead();
WriteBytes(ref writer, (byte*)m_Data.GetUnsafePtr() + messageOffset, messageLength);
writerAvailable -= sizeof(int) + messageLength;

View File

@@ -145,7 +145,7 @@ namespace Unity.Netcode.Transports.UTP
// Maximum reliable throughput, assuming the full reliable window can be sent on every
// frame at 60 FPS. This will be a large over-estimation in any realistic scenario.
private const int k_MaxReliableThroughput = (NetworkParameterConstants.MTU * 32 * 60) / 1000; // bytes per millisecond
private const int k_MaxReliableThroughput = (NetworkParameterConstants.MTU * 64 * 60) / 1000; // bytes per millisecond
private static ConnectionAddressData s_DefaultConnectionAddressData = new ConnectionAddressData { Address = "127.0.0.1", Port = 7777, ServerListenAddress = string.Empty };
@@ -303,20 +303,23 @@ namespace Unity.Netcode.Transports.UTP
public ushort Port;
/// <summary>
/// IP address the server will listen on. If not provided, will use 'Address'.
/// IP address the server will listen on. If not provided, will use localhost.
/// </summary>
[Tooltip("IP address the server will listen on. If not provided, will use 'Address'.")]
[Tooltip("IP address the server will listen on. If not provided, will use localhost.")]
[SerializeField]
public string ServerListenAddress;
private static NetworkEndpoint ParseNetworkEndpoint(string ip, ushort port)
private static NetworkEndpoint ParseNetworkEndpoint(string ip, ushort port, bool silent = false)
{
NetworkEndpoint endpoint = default;
if (!NetworkEndpoint.TryParse(ip, port, out endpoint, NetworkFamily.Ipv4) &&
!NetworkEndpoint.TryParse(ip, port, out endpoint, NetworkFamily.Ipv6))
{
Debug.LogError($"Invalid network endpoint: {ip}:{port}.");
if (!silent)
{
Debug.LogError($"Invalid network endpoint: {ip}:{port}.");
}
}
return endpoint;
@@ -330,9 +333,34 @@ namespace Unity.Netcode.Transports.UTP
/// <summary>
/// Endpoint (IP address and port) server will listen/bind on.
/// </summary>
public NetworkEndpoint ListenEndPoint => ParseNetworkEndpoint((ServerListenAddress?.Length == 0) ? Address : ServerListenAddress, Port);
public NetworkEndpoint ListenEndPoint
{
get
{
if (string.IsNullOrEmpty(ServerListenAddress))
{
var ep = NetworkEndpoint.LoopbackIpv4;
// If an address was entered and it's IPv6, switch to using ::1 as the
// default listen address. (Otherwise we always assume IPv4.)
if (!string.IsNullOrEmpty(Address) && ServerEndPoint.Family == NetworkFamily.Ipv6)
{
ep = NetworkEndpoint.LoopbackIpv6;
}
return ep.WithPort(Port);
}
else
{
return ParseNetworkEndpoint(ServerListenAddress, Port);
}
}
}
public bool IsIpv6 => !string.IsNullOrEmpty(Address) && ParseNetworkEndpoint(Address, Port, true).Family == NetworkFamily.Ipv6;
}
/// <summary>
/// The connection (address) data for this <see cref="UnityTransport"/> instance.
/// This is where you can change IP Address, Port, or server's listen address.
@@ -529,14 +557,14 @@ namespace Unity.Netcode.Transports.UTP
int result = m_Driver.Bind(endPoint);
if (result != 0)
{
Debug.LogError("Server failed to bind");
Debug.LogError("Server failed to bind. This is usually caused by another process being bound to the same port.");
return false;
}
result = m_Driver.Listen();
if (result != 0)
{
Debug.LogError("Server failed to listen");
Debug.LogError("Server failed to listen.");
return false;
}
@@ -609,7 +637,7 @@ namespace Unity.Netcode.Transports.UTP
{
Address = ipv4Address,
Port = port,
ServerListenAddress = listenAddress ?? string.Empty
ServerListenAddress = listenAddress ?? ipv4Address
};
SetProtocol(ProtocolType.UnityTransport);
@@ -1153,17 +1181,20 @@ namespace Unity.Netcode.Transports.UTP
m_NetworkSettings = new NetworkSettings(Allocator.Persistent);
#if !UNITY_WEBGL
// If the user sends a message of exactly m_MaxPayloadSize in length, we need to
// account for the overhead of its length when we store it in the send queue.
var fragmentationCapacity = m_MaxPayloadSize + BatchedSendQueue.PerMessageOverhead;
m_NetworkSettings.WithFragmentationStageParameters(payloadCapacity: fragmentationCapacity);
#if !UTP_TRANSPORT_2_0_ABOVE
// Bump the reliable window size to its maximum size of 64. Since NGO makes heavy use of
// reliable delivery, we're better off with the increased window size compared to the
// extra 4 bytes of header that this costs us.
m_NetworkSettings.WithReliableStageParameters(windowSize: 64);
#if !UTP_TRANSPORT_2_0_ABOVE && !UNITY_WEBGL
m_NetworkSettings.WithBaselibNetworkInterfaceParameters(
receiveQueueCapacity: m_MaxPacketQueueSize,
sendQueueCapacity: m_MaxPacketQueueSize);
#endif
#endif
}
@@ -1449,7 +1480,7 @@ namespace Unity.Netcode.Transports.UTP
heartbeatTimeoutMS: transport.m_HeartbeatTimeoutMS);
#if UNITY_WEBGL && !UNITY_EDITOR
if (NetworkManager.IsServer)
if (NetworkManager.IsServer && m_ProtocolType != ProtocolType.RelayUnityTransport)
{
throw new Exception("WebGL as a server is not supported by Unity Transport, outside the Editor.");
}
@@ -1473,7 +1504,7 @@ namespace Unity.Netcode.Transports.UTP
{
if (NetworkManager.IsServer)
{
if (String.IsNullOrEmpty(m_ServerCertificate) || String.IsNullOrEmpty(m_ServerPrivateKey))
if (string.IsNullOrEmpty(m_ServerCertificate) || string.IsNullOrEmpty(m_ServerPrivateKey))
{
throw new Exception("In order to use encrypted communications, when hosting, you must set the server certificate and key.");
}
@@ -1482,11 +1513,11 @@ namespace Unity.Netcode.Transports.UTP
}
else
{
if (String.IsNullOrEmpty(m_ServerCommonName))
if (string.IsNullOrEmpty(m_ServerCommonName))
{
throw new Exception("In order to use encrypted communications, clients must set the server common name.");
}
else if (String.IsNullOrEmpty(m_ClientCaCertificate))
else if (string.IsNullOrEmpty(m_ClientCaCertificate))
{
m_NetworkSettings.WithSecureClientParameters(m_ServerCommonName);
}