From 505261c72cc80c68662cafdfac8eba36ef4a672c Mon Sep 17 00:00:00 2001 From: Noel Stephens Date: Tue, 3 Sep 2024 13:37:41 -0500 Subject: [PATCH] fix: allow the check for a NetworkObject component on NetworkBehaviours to be disabled (Back Port) (#3034) * update Add editor UI and additional NGO setting. * update Add check to see if we should perform the check for NetworkObject. * update Add the changelog entry --- com.unity.netcode.gameobjects/CHANGELOG.md | 2 ++ .../NetcodeForGameObjectsSettings.cs | 18 ++++++++++++++- .../Configuration/NetcodeSettingsProvider.cs | 22 ++++++++++++++++--- .../Editor/NetworkBehaviourEditor.cs | 6 +++++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index 3fa9d4f596..ba36d21c3e 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -10,6 +10,8 @@ Additional documentation and release notes are available at [Multiplayer Documen ### Added +- Added "Check for NetworkObject Component" property to the Multiplayer->Netcode for GameObjects project settings. When disabled, this will bypass the in-editor `NetworkObject` check on `NetworkBehaviour` components. (#3034) + ### Fixed - Fixed issue where collections v2.2.x was not supported when using UTP v2.2.x within Unity v2022.3. (#3033) diff --git a/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeForGameObjectsSettings.cs b/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeForGameObjectsSettings.cs index 4fc4b0c69d..70dfc02021 100644 --- a/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeForGameObjectsSettings.cs +++ b/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeForGameObjectsSettings.cs @@ -5,6 +5,7 @@ namespace Unity.Netcode.Editor.Configuration internal class NetcodeForGameObjectsEditorSettings { internal const string AutoAddNetworkObjectIfNoneExists = "AutoAdd-NetworkObject-When-None-Exist"; + internal const string CheckForNetworkObject = "NetworkBehaviour-Check-For-NetworkObject"; internal const string InstallMultiplayerToolsTipDismissedPlayerPrefKey = "Netcode_Tip_InstallMPTools_Dismissed"; internal static int GetNetcodeInstallMultiplayerToolTips() @@ -28,7 +29,7 @@ internal static bool GetAutoAddNetworkObjectSetting() { return EditorPrefs.GetBool(AutoAddNetworkObjectIfNoneExists); } - + // Default for this is false return false; } @@ -36,5 +37,20 @@ internal static void SetAutoAddNetworkObjectSetting(bool autoAddSetting) { EditorPrefs.SetBool(AutoAddNetworkObjectIfNoneExists, autoAddSetting); } + + internal static bool GetCheckForNetworkObjectSetting() + { + if (EditorPrefs.HasKey(CheckForNetworkObject)) + { + return EditorPrefs.GetBool(CheckForNetworkObject); + } + // Default for this is true + return true; + } + + internal static void SetCheckForNetworkObjectSetting(bool checkForNetworkObject) + { + EditorPrefs.SetBool(CheckForNetworkObject, checkForNetworkObject); + } } } diff --git a/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeSettingsProvider.cs b/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeSettingsProvider.cs index ce8023bd6b..0f78c43e09 100644 --- a/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeSettingsProvider.cs +++ b/com.unity.netcode.gameobjects/Editor/Configuration/NetcodeSettingsProvider.cs @@ -81,6 +81,7 @@ private static void OnDeactivate() internal static NetcodeSettingsLabel NetworkObjectsSectionLabel; internal static NetcodeSettingsToggle AutoAddNetworkObjectToggle; + internal static NetcodeSettingsToggle CheckForNetworkObjectToggle; internal static NetcodeSettingsLabel MultiplayerToolsLabel; internal static NetcodeSettingsToggle MultiplayerToolTipStatusToggle; @@ -103,6 +104,11 @@ private static void CheckForInitialize() AutoAddNetworkObjectToggle = new NetcodeSettingsToggle("Auto-Add NetworkObject Component", "When enabled, NetworkObject components are automatically added to GameObjects when NetworkBehaviour components are added first.", 20); } + if (CheckForNetworkObjectToggle == null) + { + CheckForNetworkObjectToggle = new NetcodeSettingsToggle("Check for NetworkObject Component", "When disabled, the automatic check on NetworkBehaviours for an associated NetworkObject component will not be performed and Auto-Add NetworkObject Component will be disabled.", 20); + } + if (MultiplayerToolsLabel == null) { MultiplayerToolsLabel = new NetcodeSettingsLabel("Multiplayer Tools", 20); @@ -120,6 +126,7 @@ private static void OnGuiHandler(string obj) CheckForInitialize(); var autoAddNetworkObjectSetting = NetcodeForGameObjectsEditorSettings.GetAutoAddNetworkObjectSetting(); + var checkForNetworkObjectSetting = NetcodeForGameObjectsEditorSettings.GetCheckForNetworkObjectSetting(); var multiplayerToolsTipStatus = NetcodeForGameObjectsEditorSettings.GetNetcodeInstallMultiplayerToolTips() == 0; var settings = NetcodeForGameObjectsProjectSettings.instance; var generateDefaultPrefabs = settings.GenerateDefaultNetworkPrefabs; @@ -134,7 +141,12 @@ private static void OnGuiHandler(string obj) { GUILayout.BeginVertical("Box"); NetworkObjectsSectionLabel.DrawLabel(); - autoAddNetworkObjectSetting = AutoAddNetworkObjectToggle.DrawToggle(autoAddNetworkObjectSetting); + autoAddNetworkObjectSetting = AutoAddNetworkObjectToggle.DrawToggle(autoAddNetworkObjectSetting, checkForNetworkObjectSetting); + checkForNetworkObjectSetting = CheckForNetworkObjectToggle.DrawToggle(checkForNetworkObjectSetting); + if (autoAddNetworkObjectSetting && !checkForNetworkObjectSetting) + { + autoAddNetworkObjectSetting = false; + } GUILayout.EndVertical(); GUILayout.BeginVertical("Box"); @@ -184,6 +196,7 @@ private static void OnGuiHandler(string obj) if (EditorGUI.EndChangeCheck()) { NetcodeForGameObjectsEditorSettings.SetAutoAddNetworkObjectSetting(autoAddNetworkObjectSetting); + NetcodeForGameObjectsEditorSettings.SetCheckForNetworkObjectSetting(checkForNetworkObjectSetting); NetcodeForGameObjectsEditorSettings.SetNetcodeInstallMultiplayerToolTips(multiplayerToolsTipStatus ? 0 : 1); settings.GenerateDefaultNetworkPrefabs = generateDefaultPrefabs; settings.TempNetworkPrefabsPath = networkPrefabsPath; @@ -213,10 +226,13 @@ internal class NetcodeSettingsToggle : NetcodeGUISettings { private GUIContent m_ToggleContent; - public bool DrawToggle(bool currentSetting) + public bool DrawToggle(bool currentSetting, bool enabled = true) { EditorGUIUtility.labelWidth = m_LabelSize; - return EditorGUILayout.Toggle(m_ToggleContent, currentSetting, m_LayoutWidth); + GUI.enabled = enabled; + var returnValue = EditorGUILayout.Toggle(m_ToggleContent, currentSetting, m_LayoutWidth); + GUI.enabled = true; + return returnValue; } public NetcodeSettingsToggle(string labelText, string toolTip, float layoutOffset) diff --git a/com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs b/com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs index 7d57afb055..c98870c591 100644 --- a/com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs +++ b/com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs @@ -352,6 +352,12 @@ public static void CheckForNetworkObject(GameObject gameObject, bool networkObje return; } + // If this automatic check is disabled, then do not perform this check. + if (!NetcodeForGameObjectsEditorSettings.GetCheckForNetworkObjectSetting()) + { + return; + } + // Now get the root parent transform to the current GameObject (or itself) var rootTransform = GetRootParentTransform(gameObject.transform); if (!rootTransform.TryGetComponent(out var networkManager))