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:
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -49,6 +50,8 @@ namespace Unity.Netcode
|
||||
/// </summary>
|
||||
public class NetworkPrefabHandler
|
||||
{
|
||||
private NetworkManager m_NetworkManager;
|
||||
|
||||
/// <summary>
|
||||
/// Links a network prefab asset to a class with the INetworkPrefabInstanceHandler interface
|
||||
/// </summary>
|
||||
@@ -60,6 +63,8 @@ namespace Unity.Netcode
|
||||
/// </summary>
|
||||
private readonly Dictionary<uint, uint> m_PrefabInstanceToPrefabAsset = new Dictionary<uint, uint>();
|
||||
|
||||
internal static string PrefabDebugHelper(NetworkPrefab networkPrefab) => $"{nameof(NetworkPrefab)} \"{networkPrefab.Prefab.name}\"";
|
||||
|
||||
/// <summary>
|
||||
/// Use a <see cref="GameObject"/> to register a class that implements the <see cref="INetworkPrefabInstanceHandler"/> interface with the <see cref="NetworkPrefabHandler"/>
|
||||
/// </summary>
|
||||
@@ -132,23 +137,23 @@ namespace Unity.Netcode
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.Exception($"{targetNetworkObject.name} does not have a {nameof(NetworkObject)} component!");
|
||||
throw new Exception($"{targetNetworkObject.name} does not have a {nameof(NetworkObject)} component!");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.Exception($"{sourceNetworkPrefab.name} does not have a {nameof(NetworkObject)} component!");
|
||||
throw new Exception($"{sourceNetworkPrefab.name} does not have a {nameof(NetworkObject)} component!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.Exception($"You should only call {nameof(RegisterHostGlobalObjectIdHashValues)} as a Host!");
|
||||
throw new Exception($"You should only call {nameof(RegisterHostGlobalObjectIdHashValues)} as a Host!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.Exception($"You can only call {nameof(RegisterHostGlobalObjectIdHashValues)} once NetworkManager is listening!");
|
||||
throw new Exception($"You can only call {nameof(RegisterHostGlobalObjectIdHashValues)} once NetworkManager is listening!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,6 +195,7 @@ namespace Unity.Netcode
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
m_PrefabInstanceToPrefabAsset.Remove(networkPrefabHashKey);
|
||||
}
|
||||
|
||||
@@ -201,30 +207,21 @@ namespace Unity.Netcode
|
||||
/// </summary>
|
||||
/// <param name="networkPrefab"></param>
|
||||
/// <returns>true or false</returns>
|
||||
internal bool ContainsHandler(GameObject networkPrefab)
|
||||
{
|
||||
return ContainsHandler(networkPrefab.GetComponent<NetworkObject>().GlobalObjectIdHash);
|
||||
}
|
||||
internal bool ContainsHandler(GameObject networkPrefab) => ContainsHandler(networkPrefab.GetComponent<NetworkObject>().GlobalObjectIdHash);
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a <see cref="NetworkObject"/> is registered to an <see cref="INetworkPrefabInstanceHandler"/> implementation
|
||||
/// </summary>
|
||||
/// <param name="networkObject"></param>
|
||||
/// <returns>true or false</returns>
|
||||
internal bool ContainsHandler(NetworkObject networkObject)
|
||||
{
|
||||
return ContainsHandler(networkObject.GlobalObjectIdHash);
|
||||
}
|
||||
internal bool ContainsHandler(NetworkObject networkObject) => ContainsHandler(networkObject.GlobalObjectIdHash);
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a <see cref="NetworkObject.GlobalObjectIdHash"/> is registered to an <see cref="INetworkPrefabInstanceHandler"/> implementation
|
||||
/// </summary>
|
||||
/// <param name="networkPrefabHash"></param>
|
||||
/// <returns>true or false</returns>
|
||||
internal bool ContainsHandler(uint networkPrefabHash)
|
||||
{
|
||||
return m_PrefabAssetToPrefabHandler.ContainsKey(networkPrefabHash) || m_PrefabInstanceToPrefabAsset.ContainsKey(networkPrefabHash);
|
||||
}
|
||||
internal bool ContainsHandler(uint networkPrefabHash) => m_PrefabAssetToPrefabHandler.ContainsKey(networkPrefabHash) || m_PrefabInstanceToPrefabAsset.ContainsKey(networkPrefabHash);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the source NetworkPrefab's <see cref="NetworkObject.GlobalObjectIdHash"/>
|
||||
@@ -237,9 +234,10 @@ namespace Unity.Netcode
|
||||
{
|
||||
return networkPrefabHash;
|
||||
}
|
||||
else if (m_PrefabInstanceToPrefabAsset.ContainsKey(networkPrefabHash))
|
||||
|
||||
if (m_PrefabInstanceToPrefabAsset.TryGetValue(networkPrefabHash, out var hash))
|
||||
{
|
||||
return m_PrefabInstanceToPrefabAsset[networkPrefabHash];
|
||||
return hash;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -256,9 +254,9 @@ namespace Unity.Netcode
|
||||
/// <returns></returns>
|
||||
internal NetworkObject HandleNetworkPrefabSpawn(uint networkPrefabAssetHash, ulong ownerClientId, Vector3 position, Quaternion rotation)
|
||||
{
|
||||
if (m_PrefabAssetToPrefabHandler.ContainsKey(networkPrefabAssetHash))
|
||||
if (m_PrefabAssetToPrefabHandler.TryGetValue(networkPrefabAssetHash, out var prefabInstanceHandler))
|
||||
{
|
||||
var networkObjectInstance = m_PrefabAssetToPrefabHandler[networkPrefabAssetHash].Instantiate(ownerClientId, position, rotation);
|
||||
var networkObjectInstance = prefabInstanceHandler.Instantiate(ownerClientId, position, rotation);
|
||||
|
||||
//Now we must make sure this alternate PrefabAsset spawned in place of the prefab asset with the networkPrefabAssetHash (GlobalObjectIdHash)
|
||||
//is registered and linked to the networkPrefabAssetHash so during the HandleNetworkPrefabDestroy process we can identify the alternate prefab asset.
|
||||
@@ -282,19 +280,146 @@ namespace Unity.Netcode
|
||||
var networkObjectInstanceHash = networkObjectInstance.GlobalObjectIdHash;
|
||||
|
||||
// Do we have custom overrides registered?
|
||||
if (m_PrefabInstanceToPrefabAsset.ContainsKey(networkObjectInstanceHash))
|
||||
if (m_PrefabInstanceToPrefabAsset.TryGetValue(networkObjectInstanceHash, out var networkPrefabAssetHash))
|
||||
{
|
||||
var networkPrefabAssetHash = m_PrefabInstanceToPrefabAsset[networkObjectInstanceHash];
|
||||
if (m_PrefabAssetToPrefabHandler.ContainsKey(networkPrefabAssetHash))
|
||||
if (m_PrefabAssetToPrefabHandler.TryGetValue(networkPrefabAssetHash, out var prefabInstanceHandler))
|
||||
{
|
||||
m_PrefabAssetToPrefabHandler[networkPrefabAssetHash].Destroy(networkObjectInstance);
|
||||
prefabInstanceHandler.Destroy(networkObjectInstance);
|
||||
}
|
||||
}
|
||||
else // Otherwise the NetworkObject is the source NetworkPrefab
|
||||
if (m_PrefabAssetToPrefabHandler.ContainsKey(networkObjectInstanceHash))
|
||||
if (m_PrefabAssetToPrefabHandler.TryGetValue(networkObjectInstanceHash, out var prefabInstanceHandler))
|
||||
{
|
||||
m_PrefabAssetToPrefabHandler[networkObjectInstanceHash].Destroy(networkObjectInstance);
|
||||
prefabInstanceHandler.Destroy(networkObjectInstance);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the <see cref="GameObject"/> to use as the override as could be defined within the NetworkPrefab list
|
||||
/// Note: This should be used to create <see cref="GameObject"/> pools (with <see cref="NetworkObject"/> components)
|
||||
/// under the scenario where you are using the Host model as it spawns everything locally. As such, the override
|
||||
/// will not be applied when spawning locally on a Host.
|
||||
/// Related Classes and Interfaces:
|
||||
/// <see cref="INetworkPrefabInstanceHandler"/>
|
||||
/// </summary>
|
||||
/// <param name="gameObject">the <see cref="GameObject"/> to be checked for a <see cref="NetworkManager"/> defined NetworkPrefab override</param>
|
||||
/// <returns>a <see cref="GameObject"/> that is either the override or if no overrides exist it returns the same as the one passed in as a parameter</returns>
|
||||
public GameObject GetNetworkPrefabOverride(GameObject gameObject)
|
||||
{
|
||||
if (gameObject.TryGetComponent<NetworkObject>(out var networkObject))
|
||||
{
|
||||
if (m_NetworkManager.NetworkConfig.Prefabs.NetworkPrefabOverrideLinks.ContainsKey(networkObject.GlobalObjectIdHash))
|
||||
{
|
||||
switch (m_NetworkManager.NetworkConfig.Prefabs.NetworkPrefabOverrideLinks[networkObject.GlobalObjectIdHash].Override)
|
||||
{
|
||||
case NetworkPrefabOverride.Hash:
|
||||
case NetworkPrefabOverride.Prefab:
|
||||
{
|
||||
return m_NetworkManager.NetworkConfig.Prefabs.NetworkPrefabOverrideLinks[networkObject.GlobalObjectIdHash].OverridingTargetPrefab;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return gameObject;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new prefab to the network prefab list.
|
||||
/// This can be any GameObject with a NetworkObject component, from any source (addressables, asset
|
||||
/// bundles, Resource.Load, dynamically created, etc)
|
||||
///
|
||||
/// There are three limitations to this method:
|
||||
/// - If you have NetworkConfig.ForceSamePrefabs enabled, you can only do this before starting
|
||||
/// networking, and the server and all connected clients must all have the same exact set of prefabs
|
||||
/// added via this method before connecting
|
||||
/// - Adding a prefab on the server does not automatically add it on the client - it's up to you
|
||||
/// to make sure the client and server are synchronized via whatever method makes sense for your game
|
||||
/// (RPCs, configurations, deterministic loading, etc)
|
||||
/// - If the server sends a Spawn message to a client that has not yet added a prefab for, the spawn message
|
||||
/// and any other relevant messages will be held for a configurable time (default 1 second, configured via
|
||||
/// NetworkConfig.SpawnTimeout) before an error is logged. This is intended to enable the SDK to gracefully
|
||||
/// handle unexpected conditions (slow disks, slow network, etc) that slow down asset loading. This timeout
|
||||
/// should not be relied on and code shouldn't be written around it - your code should be written so that
|
||||
/// the asset is expected to be loaded before it's needed.
|
||||
/// </summary>
|
||||
/// <param name="prefab"></param>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public void AddNetworkPrefab(GameObject prefab)
|
||||
{
|
||||
if (m_NetworkManager.IsListening && m_NetworkManager.NetworkConfig.ForceSamePrefabs)
|
||||
{
|
||||
throw new Exception($"All prefabs must be registered before starting {nameof(NetworkManager)} when {nameof(NetworkConfig.ForceSamePrefabs)} is enabled.");
|
||||
}
|
||||
|
||||
var networkObject = prefab.GetComponent<NetworkObject>();
|
||||
if (!networkObject)
|
||||
{
|
||||
throw new Exception($"All {nameof(NetworkPrefab)}s must contain a {nameof(NetworkObject)} component.");
|
||||
}
|
||||
|
||||
var networkPrefab = new NetworkPrefab { Prefab = prefab };
|
||||
bool added = m_NetworkManager.NetworkConfig.Prefabs.Add(networkPrefab);
|
||||
if (m_NetworkManager.IsListening && added)
|
||||
{
|
||||
m_NetworkManager.DeferredMessageManager.ProcessTriggers(IDeferredNetworkMessageManager.TriggerType.OnAddPrefab, networkObject.GlobalObjectIdHash);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove a prefab from the prefab list.
|
||||
/// As with AddNetworkPrefab, this is specific to the client it's called on -
|
||||
/// calling it on the server does not automatically remove anything on any of the
|
||||
/// client processes.
|
||||
///
|
||||
/// Like AddNetworkPrefab, when NetworkConfig.ForceSamePrefabs is enabled,
|
||||
/// this cannot be called after connecting.
|
||||
/// </summary>
|
||||
/// <param name="prefab"></param>
|
||||
public void RemoveNetworkPrefab(GameObject prefab)
|
||||
{
|
||||
if (m_NetworkManager.IsListening && m_NetworkManager.NetworkConfig.ForceSamePrefabs)
|
||||
{
|
||||
throw new Exception($"Prefabs cannot be removed after starting {nameof(NetworkManager)} when {nameof(NetworkConfig.ForceSamePrefabs)} is enabled.");
|
||||
}
|
||||
|
||||
var globalObjectIdHash = prefab.GetComponent<NetworkObject>().GlobalObjectIdHash;
|
||||
m_NetworkManager.NetworkConfig.Prefabs.Remove(prefab);
|
||||
if (ContainsHandler(globalObjectIdHash))
|
||||
{
|
||||
RemoveHandler(globalObjectIdHash);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If one exists, registers the player prefab
|
||||
/// </summary>
|
||||
internal void RegisterPlayerPrefab()
|
||||
{
|
||||
var networkConfig = m_NetworkManager.NetworkConfig;
|
||||
// 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)
|
||||
{
|
||||
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.Prefabs.NetworkPrefabOverrideLinks.ContainsKey(playerPrefabNetworkObject.GlobalObjectIdHash))
|
||||
{
|
||||
//Then add a new entry for the player prefab
|
||||
AddNetworkPrefab(networkConfig.PlayerPrefab);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Provide the name of the prefab with issues so the user can more easily find the prefab and fix it
|
||||
Debug.LogError($"{nameof(NetworkConfig.PlayerPrefab)} (\"{networkConfig.PlayerPrefab.name}\") has no NetworkObject assigned to it!.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void Initialize(NetworkManager networkManager)
|
||||
{
|
||||
m_NetworkManager = networkManager;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user