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)
409 lines
19 KiB
C#
409 lines
19 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using Unity.Netcode.Editor.Configuration;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace Unity.Netcode.Editor
|
|
{
|
|
/// <summary>
|
|
/// This <see cref="CustomEditor"/> handles the translation between the <see cref="NetworkConfig"/> and
|
|
/// the <see cref="NetworkManager"/> properties.
|
|
/// </summary>
|
|
[CustomEditor(typeof(NetworkManager), true)]
|
|
[CanEditMultipleObjects]
|
|
public class NetworkManagerEditor : UnityEditor.Editor
|
|
{
|
|
private static GUIStyle s_CenteredWordWrappedLabelStyle;
|
|
private static GUIStyle s_HelpBoxStyle;
|
|
|
|
// Properties
|
|
private SerializedProperty m_RunInBackgroundProperty;
|
|
private SerializedProperty m_LogLevelProperty;
|
|
|
|
// NetworkConfig
|
|
private SerializedProperty m_NetworkConfigProperty;
|
|
|
|
// NetworkConfig fields
|
|
private SerializedProperty m_PlayerPrefabProperty;
|
|
private SerializedProperty m_ProtocolVersionProperty;
|
|
private SerializedProperty m_NetworkTransportProperty;
|
|
private SerializedProperty m_TickRateProperty;
|
|
private SerializedProperty m_MaxObjectUpdatesPerTickProperty;
|
|
private SerializedProperty m_ClientConnectionBufferTimeoutProperty;
|
|
private SerializedProperty m_ConnectionApprovalProperty;
|
|
private SerializedProperty m_EnsureNetworkVariableLengthSafetyProperty;
|
|
private SerializedProperty m_ForceSamePrefabsProperty;
|
|
private SerializedProperty m_EnableSceneManagementProperty;
|
|
private SerializedProperty m_RecycleNetworkIdsProperty;
|
|
private SerializedProperty m_NetworkIdRecycleDelayProperty;
|
|
private SerializedProperty m_RpcHashSizeProperty;
|
|
private SerializedProperty m_LoadSceneTimeOutProperty;
|
|
private SerializedProperty m_PrefabsList;
|
|
|
|
private NetworkManager m_NetworkManager;
|
|
private bool m_Initialized;
|
|
|
|
private readonly List<Type> m_TransportTypes = new List<Type>();
|
|
private string[] m_TransportNames = { "Select transport..." };
|
|
|
|
private void ReloadTransports()
|
|
{
|
|
m_TransportTypes.Clear();
|
|
|
|
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
|
|
|
|
foreach (var assembly in assemblies)
|
|
{
|
|
var types = assembly.GetTypes();
|
|
|
|
foreach (var type in types)
|
|
{
|
|
if (type.IsSubclassOf(typeof(NetworkTransport)) && !type.IsSubclassOf(typeof(TestingNetworkTransport)) && type != typeof(TestingNetworkTransport))
|
|
{
|
|
m_TransportTypes.Add(type);
|
|
}
|
|
}
|
|
}
|
|
|
|
m_TransportNames = new string[m_TransportTypes.Count + 1];
|
|
m_TransportNames[0] = "Select transport...";
|
|
|
|
for (int i = 0; i < m_TransportTypes.Count; i++)
|
|
{
|
|
m_TransportNames[i + 1] = m_TransportTypes[i].Name;
|
|
}
|
|
}
|
|
|
|
private void Initialize()
|
|
{
|
|
if (m_Initialized)
|
|
{
|
|
return;
|
|
}
|
|
|
|
m_Initialized = true;
|
|
m_NetworkManager = (NetworkManager)target;
|
|
|
|
// Base properties
|
|
m_RunInBackgroundProperty = serializedObject.FindProperty(nameof(NetworkManager.RunInBackground));
|
|
m_LogLevelProperty = serializedObject.FindProperty(nameof(NetworkManager.LogLevel));
|
|
m_NetworkConfigProperty = serializedObject.FindProperty(nameof(NetworkManager.NetworkConfig));
|
|
|
|
// NetworkConfig properties
|
|
m_PlayerPrefabProperty = m_NetworkConfigProperty.FindPropertyRelative(nameof(NetworkConfig.PlayerPrefab));
|
|
m_ProtocolVersionProperty = m_NetworkConfigProperty.FindPropertyRelative("ProtocolVersion");
|
|
m_NetworkTransportProperty = m_NetworkConfigProperty.FindPropertyRelative("NetworkTransport");
|
|
m_TickRateProperty = m_NetworkConfigProperty.FindPropertyRelative("TickRate");
|
|
m_ClientConnectionBufferTimeoutProperty = m_NetworkConfigProperty.FindPropertyRelative("ClientConnectionBufferTimeout");
|
|
m_ConnectionApprovalProperty = m_NetworkConfigProperty.FindPropertyRelative("ConnectionApproval");
|
|
m_EnsureNetworkVariableLengthSafetyProperty = m_NetworkConfigProperty.FindPropertyRelative("EnsureNetworkVariableLengthSafety");
|
|
m_ForceSamePrefabsProperty = m_NetworkConfigProperty.FindPropertyRelative("ForceSamePrefabs");
|
|
m_EnableSceneManagementProperty = m_NetworkConfigProperty.FindPropertyRelative("EnableSceneManagement");
|
|
m_RecycleNetworkIdsProperty = m_NetworkConfigProperty.FindPropertyRelative("RecycleNetworkIds");
|
|
m_NetworkIdRecycleDelayProperty = m_NetworkConfigProperty.FindPropertyRelative("NetworkIdRecycleDelay");
|
|
m_RpcHashSizeProperty = m_NetworkConfigProperty.FindPropertyRelative("RpcHashSize");
|
|
m_LoadSceneTimeOutProperty = m_NetworkConfigProperty.FindPropertyRelative("LoadSceneTimeOut");
|
|
m_PrefabsList = m_NetworkConfigProperty
|
|
.FindPropertyRelative(nameof(NetworkConfig.Prefabs))
|
|
.FindPropertyRelative(nameof(NetworkPrefabs.NetworkPrefabsLists));
|
|
|
|
ReloadTransports();
|
|
}
|
|
|
|
private void CheckNullProperties()
|
|
{
|
|
// Base properties
|
|
m_RunInBackgroundProperty = serializedObject.FindProperty(nameof(NetworkManager.RunInBackground));
|
|
m_LogLevelProperty = serializedObject.FindProperty(nameof(NetworkManager.LogLevel));
|
|
m_NetworkConfigProperty = serializedObject.FindProperty(nameof(NetworkManager.NetworkConfig));
|
|
|
|
// NetworkConfig properties
|
|
m_PlayerPrefabProperty = m_NetworkConfigProperty.FindPropertyRelative(nameof(NetworkConfig.PlayerPrefab));
|
|
m_ProtocolVersionProperty = m_NetworkConfigProperty.FindPropertyRelative("ProtocolVersion");
|
|
m_NetworkTransportProperty = m_NetworkConfigProperty.FindPropertyRelative("NetworkTransport");
|
|
m_TickRateProperty = m_NetworkConfigProperty.FindPropertyRelative("TickRate");
|
|
m_ClientConnectionBufferTimeoutProperty = m_NetworkConfigProperty.FindPropertyRelative("ClientConnectionBufferTimeout");
|
|
m_ConnectionApprovalProperty = m_NetworkConfigProperty.FindPropertyRelative("ConnectionApproval");
|
|
m_EnsureNetworkVariableLengthSafetyProperty = m_NetworkConfigProperty.FindPropertyRelative("EnsureNetworkVariableLengthSafety");
|
|
m_ForceSamePrefabsProperty = m_NetworkConfigProperty.FindPropertyRelative("ForceSamePrefabs");
|
|
m_EnableSceneManagementProperty = m_NetworkConfigProperty.FindPropertyRelative("EnableSceneManagement");
|
|
m_RecycleNetworkIdsProperty = m_NetworkConfigProperty.FindPropertyRelative("RecycleNetworkIds");
|
|
m_NetworkIdRecycleDelayProperty = m_NetworkConfigProperty.FindPropertyRelative("NetworkIdRecycleDelay");
|
|
m_RpcHashSizeProperty = m_NetworkConfigProperty.FindPropertyRelative("RpcHashSize");
|
|
m_LoadSceneTimeOutProperty = m_NetworkConfigProperty.FindPropertyRelative("LoadSceneTimeOut");
|
|
m_PrefabsList = m_NetworkConfigProperty
|
|
.FindPropertyRelative(nameof(NetworkConfig.Prefabs))
|
|
.FindPropertyRelative(nameof(NetworkPrefabs.NetworkPrefabsLists));
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override void OnInspectorGUI()
|
|
{
|
|
Initialize();
|
|
CheckNullProperties();
|
|
|
|
#if !MULTIPLAYER_TOOLS
|
|
DrawInstallMultiplayerToolsTip();
|
|
#endif
|
|
|
|
if (!m_NetworkManager.IsServer && !m_NetworkManager.IsClient)
|
|
{
|
|
serializedObject.Update();
|
|
EditorGUILayout.PropertyField(m_RunInBackgroundProperty);
|
|
EditorGUILayout.PropertyField(m_LogLevelProperty);
|
|
EditorGUILayout.Space();
|
|
|
|
EditorGUILayout.PropertyField(m_PlayerPrefabProperty);
|
|
EditorGUILayout.Space();
|
|
|
|
if (m_NetworkManager.NetworkConfig.HasOldPrefabList())
|
|
{
|
|
EditorGUILayout.HelpBox("Network Prefabs serialized in old format. Migrate to new format to edit the list.", MessageType.Info);
|
|
if (GUILayout.Button(new GUIContent("Migrate Prefab List", "Converts the old format Network Prefab list to a new Scriptable Object")))
|
|
{
|
|
// Default directory
|
|
var directory = "Assets/";
|
|
var assetPath = AssetDatabase.GetAssetPath(m_NetworkManager);
|
|
if (assetPath == "")
|
|
{
|
|
assetPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(m_NetworkManager);
|
|
}
|
|
|
|
if (assetPath != "")
|
|
{
|
|
directory = Path.GetDirectoryName(assetPath);
|
|
}
|
|
else
|
|
{
|
|
#if UNITY_2021_1_OR_NEWER
|
|
var prefabStage = UnityEditor.SceneManagement.PrefabStageUtility.GetPrefabStage(m_NetworkManager.gameObject);
|
|
#else
|
|
var prefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetPrefabStage(m_NetworkManager.gameObject);
|
|
#endif
|
|
if (prefabStage != null)
|
|
{
|
|
var prefabPath = prefabStage.assetPath;
|
|
if (!string.IsNullOrEmpty(prefabPath))
|
|
{
|
|
directory = Path.GetDirectoryName(prefabPath);
|
|
}
|
|
}
|
|
if (m_NetworkManager.gameObject.scene != null)
|
|
{
|
|
var scenePath = m_NetworkManager.gameObject.scene.path;
|
|
if (!string.IsNullOrEmpty(scenePath))
|
|
{
|
|
directory = Path.GetDirectoryName(scenePath);
|
|
}
|
|
}
|
|
}
|
|
var networkPrefabs = m_NetworkManager.NetworkConfig.MigrateOldNetworkPrefabsToNetworkPrefabsList();
|
|
string path = Path.Combine(directory, $"NetworkPrefabs-{m_NetworkManager.GetInstanceID()}.asset");
|
|
Debug.Log("Saving migrated Network Prefabs List to " + path);
|
|
AssetDatabase.CreateAsset(networkPrefabs, path);
|
|
EditorUtility.SetDirty(m_NetworkManager);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (m_NetworkManager.NetworkConfig.Prefabs.NetworkPrefabsLists.Count == 0)
|
|
{
|
|
EditorGUILayout.HelpBox("You have no prefab list selected. You will have to add your prefabs manually at runtime for netcode to work.", MessageType.Warning);
|
|
}
|
|
EditorGUILayout.PropertyField(m_PrefabsList);
|
|
}
|
|
EditorGUILayout.Space();
|
|
|
|
EditorGUILayout.LabelField("General", EditorStyles.boldLabel);
|
|
EditorGUILayout.PropertyField(m_ProtocolVersionProperty);
|
|
|
|
EditorGUILayout.PropertyField(m_NetworkTransportProperty);
|
|
|
|
if (m_NetworkTransportProperty.objectReferenceValue == null)
|
|
{
|
|
EditorGUILayout.HelpBox("You have no transport selected. A transport is required for netcode to work. Which one do you want?", MessageType.Warning);
|
|
|
|
int selection = EditorGUILayout.Popup(0, m_TransportNames);
|
|
|
|
if (selection > 0)
|
|
{
|
|
ReloadTransports();
|
|
|
|
var transportComponent = m_NetworkManager.gameObject.GetComponent(m_TransportTypes[selection - 1]) ?? m_NetworkManager.gameObject.AddComponent(m_TransportTypes[selection - 1]);
|
|
m_NetworkTransportProperty.objectReferenceValue = transportComponent;
|
|
|
|
Repaint();
|
|
}
|
|
}
|
|
|
|
EditorGUILayout.PropertyField(m_TickRateProperty);
|
|
|
|
EditorGUILayout.LabelField("Performance", EditorStyles.boldLabel);
|
|
|
|
EditorGUILayout.PropertyField(m_EnsureNetworkVariableLengthSafetyProperty);
|
|
|
|
EditorGUILayout.LabelField("Connection", EditorStyles.boldLabel);
|
|
EditorGUILayout.PropertyField(m_ConnectionApprovalProperty);
|
|
|
|
using (new EditorGUI.DisabledScope(!m_NetworkManager.NetworkConfig.ConnectionApproval))
|
|
{
|
|
EditorGUILayout.PropertyField(m_ClientConnectionBufferTimeoutProperty);
|
|
}
|
|
|
|
EditorGUILayout.LabelField("Spawning", EditorStyles.boldLabel);
|
|
EditorGUILayout.PropertyField(m_ForceSamePrefabsProperty);
|
|
|
|
|
|
EditorGUILayout.PropertyField(m_RecycleNetworkIdsProperty);
|
|
|
|
using (new EditorGUI.DisabledScope(!m_NetworkManager.NetworkConfig.RecycleNetworkIds))
|
|
{
|
|
EditorGUILayout.PropertyField(m_NetworkIdRecycleDelayProperty);
|
|
}
|
|
|
|
EditorGUILayout.LabelField("Bandwidth", EditorStyles.boldLabel);
|
|
EditorGUILayout.PropertyField(m_RpcHashSizeProperty);
|
|
|
|
EditorGUILayout.LabelField("Scene Management", EditorStyles.boldLabel);
|
|
EditorGUILayout.PropertyField(m_EnableSceneManagementProperty);
|
|
|
|
using (new EditorGUI.DisabledScope(!m_NetworkManager.NetworkConfig.EnableSceneManagement))
|
|
{
|
|
EditorGUILayout.PropertyField(m_LoadSceneTimeOutProperty);
|
|
}
|
|
|
|
serializedObject.ApplyModifiedProperties();
|
|
|
|
|
|
// Start buttons below
|
|
{
|
|
string buttonDisabledReasonSuffix = "";
|
|
|
|
if (!EditorApplication.isPlaying)
|
|
{
|
|
buttonDisabledReasonSuffix = ". This can only be done in play mode";
|
|
GUI.enabled = false;
|
|
}
|
|
|
|
if (GUILayout.Button(new GUIContent("Start Host", "Starts a host instance" + buttonDisabledReasonSuffix)))
|
|
{
|
|
m_NetworkManager.StartHost();
|
|
}
|
|
|
|
if (GUILayout.Button(new GUIContent("Start Server", "Starts a server instance" + buttonDisabledReasonSuffix)))
|
|
{
|
|
m_NetworkManager.StartServer();
|
|
}
|
|
|
|
if (GUILayout.Button(new GUIContent("Start Client", "Starts a client instance" + buttonDisabledReasonSuffix)))
|
|
{
|
|
m_NetworkManager.StartClient();
|
|
}
|
|
|
|
if (!EditorApplication.isPlaying)
|
|
{
|
|
GUI.enabled = true;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
string instanceType = string.Empty;
|
|
|
|
if (m_NetworkManager.IsHost)
|
|
{
|
|
instanceType = "Host";
|
|
}
|
|
else if (m_NetworkManager.IsServer)
|
|
{
|
|
instanceType = "Server";
|
|
}
|
|
else if (m_NetworkManager.IsClient)
|
|
{
|
|
instanceType = "Client";
|
|
}
|
|
|
|
EditorGUILayout.HelpBox("You cannot edit the NetworkConfig when a " + instanceType + " is running.", MessageType.Info);
|
|
|
|
if (GUILayout.Button(new GUIContent("Stop " + instanceType, "Stops the " + instanceType + " instance.")))
|
|
{
|
|
m_NetworkManager.Shutdown();
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void DrawInstallMultiplayerToolsTip()
|
|
{
|
|
const string getToolsText = "Access additional tools for multiplayer development by installing the Multiplayer Tools package in the Package Manager.";
|
|
const string openDocsButtonText = "Open Docs";
|
|
const string dismissButtonText = "Dismiss";
|
|
const string targetUrl = "https://docs-multiplayer.unity3d.com/tools/current/install-tools";
|
|
const string infoIconName = "console.infoicon";
|
|
|
|
if (NetcodeForGameObjectsEditorSettings.GetNetcodeInstallMultiplayerToolTips() != 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (s_CenteredWordWrappedLabelStyle == null)
|
|
{
|
|
s_CenteredWordWrappedLabelStyle = new GUIStyle(GUI.skin.label)
|
|
{
|
|
wordWrap = true,
|
|
alignment = TextAnchor.MiddleLeft
|
|
};
|
|
}
|
|
|
|
if (s_HelpBoxStyle == null)
|
|
{
|
|
s_HelpBoxStyle = new GUIStyle(EditorStyles.helpBox)
|
|
{
|
|
padding = new RectOffset(10, 10, 10, 10)
|
|
};
|
|
}
|
|
|
|
var openDocsButtonStyle = GUI.skin.button;
|
|
var dismissButtonStyle = EditorStyles.linkLabel;
|
|
|
|
GUILayout.BeginHorizontal();
|
|
GUILayout.FlexibleSpace();
|
|
GUILayout.BeginHorizontal(s_HelpBoxStyle, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(false), GUILayout.MaxWidth(800));
|
|
{
|
|
GUILayout.Label(new GUIContent(EditorGUIUtility.IconContent(infoIconName)), GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true));
|
|
GUILayout.Space(4);
|
|
GUILayout.Label(getToolsText, s_CenteredWordWrappedLabelStyle, GUILayout.ExpandHeight(true));
|
|
|
|
GUILayout.Space(4);
|
|
|
|
GUILayout.BeginVertical();
|
|
GUILayout.FlexibleSpace();
|
|
if (GUILayout.Button(openDocsButtonText, openDocsButtonStyle, GUILayout.Width(90), GUILayout.Height(30)))
|
|
{
|
|
Application.OpenURL(targetUrl);
|
|
}
|
|
GUILayout.FlexibleSpace();
|
|
GUILayout.EndVertical();
|
|
|
|
GUILayout.Space(4);
|
|
|
|
GUILayout.BeginVertical();
|
|
GUILayout.FlexibleSpace();
|
|
if (GUILayout.Button(dismissButtonText, dismissButtonStyle, GUILayout.ExpandWidth(false)))
|
|
{
|
|
NetcodeForGameObjectsEditorSettings.SetNetcodeInstallMultiplayerToolTips(1);
|
|
}
|
|
EditorGUIUtility.AddCursorRect(GUILayoutUtility.GetLastRect(), MouseCursor.Link);
|
|
GUILayout.FlexibleSpace();
|
|
GUILayout.EndVertical();
|
|
}
|
|
GUILayout.EndHorizontal();
|
|
GUILayout.FlexibleSpace();
|
|
GUILayout.EndHorizontal();
|
|
|
|
GUILayout.Space(10);
|
|
}
|
|
}
|
|
}
|