Skip to content

refactor: Migrate menu settings to project settings [MTT-5001] #2285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ Additional documentation and release notes are available at [Multiplayer Documen

## [Unreleased]

### Added
- Added `NetworkObject` auto-add helper and Multiplayer Tools install reminder settings to Project Settings. (#2285)

### Fixed

- Fixed issue where in-scene placed `NetworkObjects` were not honoring the `AutoObjectParentSync` property. (#2281)
- Fixed the issue where `NetworkManager.OnClientConnectedCallback` was being invoked before in-scene placed `NetworkObject`s had been spawned when starting `NetworkManager` as a host. (#2277)
- Creating a `FastBufferReader` with `Allocator.None` will not result in extra memory being allocated for the buffer (since it's owned externally in that scenario). (#2265)

### Removed
- Removed the `NetworkObject` auto-add and Multiplayer Tools install reminder settings from the Menu interface. (#2285)


## [1.1.0] - 2022-10-21

### Added
Expand Down
8 changes: 8 additions & 0 deletions com.unity.netcode.gameobjects/Editor/Configuration.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using UnityEditor;


namespace Unity.Netcode.Editor.Configuration
{
internal class NetcodeForGameObjectsSettings
{
internal const string AutoAddNetworkObjectIfNoneExists = "AutoAdd-NetworkObject-When-None-Exist";
internal const string InstallMultiplayerToolsTipDismissedPlayerPrefKey = "Netcode_Tip_InstallMPTools_Dismissed";

internal static int GetNetcodeInstallMultiplayerToolTips()
{
if (EditorPrefs.HasKey(InstallMultiplayerToolsTipDismissedPlayerPrefKey))
{
return EditorPrefs.GetInt(InstallMultiplayerToolsTipDismissedPlayerPrefKey);
}
return 0;
}

internal static void SetNetcodeInstallMultiplayerToolTips(int toolTipPrefSetting)
{
EditorPrefs.SetInt(InstallMultiplayerToolsTipDismissedPlayerPrefKey, toolTipPrefSetting);
}

internal static bool GetAutoAddNetworkObjectSetting()
{
if (EditorPrefs.HasKey(AutoAddNetworkObjectIfNoneExists))
{
return EditorPrefs.GetBool(AutoAddNetworkObjectIfNoneExists);
}
return false;
}

internal static void SetAutoAddNetworkObjectSetting(bool autoAddSetting)
{
EditorPrefs.SetBool(AutoAddNetworkObjectIfNoneExists, autoAddSetting);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using UnityEditor;
using UnityEngine;

namespace Unity.Netcode.Editor.Configuration
{
internal static class NetcodeSettingsProvider
{
[SettingsProvider]
public static SettingsProvider CreateNetcodeSettingsProvider()
{
// First parameter is the path in the Settings window.
// Second parameter is the scope of this setting: it only appears in the Settings window for the Project scope.
var provider = new SettingsProvider("Project/NetcodeForGameObjects", SettingsScope.Project)
{
label = "Netcode for GameObjects",
keywords = new[] { "netcode", "editor" },
guiHandler = OnGuiHandler,
};

return provider;
}

internal static NetcodeSettingsLabel NetworkObjectsSectionLabel = new NetcodeSettingsLabel("NetworkObject Helper Settings", 20);
internal static NetcodeSettingsToggle AutoAddNetworkObjectToggle = new NetcodeSettingsToggle("Auto-Add NetworkObjects", "When enabled, NetworkObjects are automatically added to GameObjects when NetworkBehaviours are added first.", 20);
internal static NetcodeSettingsLabel MultiplayerToolsLabel = new NetcodeSettingsLabel("Multiplayer Tools", 20);
internal static NetcodeSettingsToggle MultiplayerToolTipStatusToggle = new NetcodeSettingsToggle("Multiplayer Tools Install Reminder", "When enabled, the NetworkManager will display " +
"the notification to install the multiplayer tools package.", 20);

private static void OnGuiHandler(string obj)
{
var autoAddNetworkObjectSetting = NetcodeForGameObjectsSettings.GetAutoAddNetworkObjectSetting();
var multiplayerToolsTipStatus = NetcodeForGameObjectsSettings.GetNetcodeInstallMultiplayerToolTips() == 0;
EditorGUI.BeginChangeCheck();
NetworkObjectsSectionLabel.DrawLabel();
autoAddNetworkObjectSetting = AutoAddNetworkObjectToggle.DrawToggle(autoAddNetworkObjectSetting);
MultiplayerToolsLabel.DrawLabel();
multiplayerToolsTipStatus = MultiplayerToolTipStatusToggle.DrawToggle(multiplayerToolsTipStatus);
if (EditorGUI.EndChangeCheck())
{
NetcodeForGameObjectsSettings.SetAutoAddNetworkObjectSetting(autoAddNetworkObjectSetting);
NetcodeForGameObjectsSettings.SetNetcodeInstallMultiplayerToolTips(multiplayerToolsTipStatus ? 0 : 1);
}
}
}

internal class NetcodeSettingsLabel : NetcodeGUISettings
{
private string m_LabelContent;

public void DrawLabel()
{
EditorGUIUtility.labelWidth = m_LabelSize;
GUILayout.Label(m_LabelContent, EditorStyles.boldLabel, m_LayoutWidth);
}

public NetcodeSettingsLabel(string labelText, float layoutOffset = 0.0f)
{
m_LabelContent = labelText;
AdjustLableSize(labelText, layoutOffset);
}
}

internal class NetcodeSettingsToggle : NetcodeGUISettings
{
private GUIContent m_ToggleContent;

public bool DrawToggle(bool currentSetting)
{
EditorGUIUtility.labelWidth = m_LabelSize;
return EditorGUILayout.Toggle(m_ToggleContent, currentSetting, m_LayoutWidth);
}

public NetcodeSettingsToggle(string labelText, string toolTip, float layoutOffset)
{
AdjustLableSize(labelText, layoutOffset);
m_ToggleContent = new GUIContent(labelText, toolTip);
}
}

internal class NetcodeGUISettings
{
private const float k_MaxLabelWidth = 450f;
protected float m_LabelSize { get; private set; }

protected GUILayoutOption m_LayoutWidth { get; private set; }

protected void AdjustLableSize(string labelText, float offset = 0.0f)
{
m_LabelSize = Mathf.Min(k_MaxLabelWidth, EditorStyles.label.CalcSize(new GUIContent(labelText)).x);
m_LayoutWidth = GUILayout.Width(m_LabelSize + offset);
}
}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 3 additions & 19 deletions com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using UnityEngine;
using UnityEditor;
using Unity.Netcode.Editor.Configuration;

namespace Unity.Netcode.Editor
{
Expand Down Expand Up @@ -230,8 +231,6 @@ private void OnEnable()
CheckForNetworkObject((target as NetworkBehaviour).gameObject);
}

internal const string AutoAddNetworkObjectIfNoneExists = "AutoAdd-NetworkObject-When-None-Exist";

/// <summary>
/// Recursively finds the root parent of a <see cref="Transform"/>
/// </summary>
Expand Down Expand Up @@ -308,7 +307,7 @@ public static void CheckForNetworkObject(GameObject gameObject, bool networkObje
// and the user has already turned "Auto-Add NetworkObject" on when first notified about the requirement
// then just send a reminder to the user why the NetworkObject they just deleted seemingly "re-appeared"
// again.
if (networkObjectRemoved && EditorPrefs.HasKey(AutoAddNetworkObjectIfNoneExists) && EditorPrefs.GetBool(AutoAddNetworkObjectIfNoneExists))
if (networkObjectRemoved && NetcodeForGameObjectsSettings.GetAutoAddNetworkObjectSetting())
{
Debug.LogWarning($"{gameObject.name} still has {nameof(NetworkBehaviour)}s and Auto-Add NetworkObjects is enabled. A NetworkObject is being added back to {gameObject.name}.");
Debug.Log($"To reset Auto-Add NetworkObjects: Select the Netcode->General->Reset Auto-Add NetworkObject menu item.");
Expand All @@ -317,7 +316,7 @@ public static void CheckForNetworkObject(GameObject gameObject, bool networkObje
// Notify and provide the option to add it one time, always add a NetworkObject, or do nothing and let the user manually add it
if (EditorUtility.DisplayDialog($"{nameof(NetworkBehaviour)}s require a {nameof(NetworkObject)}",
$"{gameObject.name} does not have a {nameof(NetworkObject)} component. Would you like to add one now?", "Yes", "No (manually add it)",
DialogOptOutDecisionType.ForThisMachine, AutoAddNetworkObjectIfNoneExists))
DialogOptOutDecisionType.ForThisMachine, NetcodeForGameObjectsSettings.AutoAddNetworkObjectIfNoneExists))
{
gameObject.AddComponent<NetworkObject>();
var activeScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
Expand All @@ -327,20 +326,5 @@ public static void CheckForNetworkObject(GameObject gameObject, bool networkObje
}
}
}

/// <summary>
/// This allows users to reset the Auto-Add NetworkObject preference
/// so the next time they add a NetworkBehaviour to a GameObject without
/// a NetworkObject it will display the dialog box again and not
/// automatically add a NetworkObject.
/// </summary>
[MenuItem("Netcode/General/Reset Auto-Add NetworkObject", false, 1)]
private static void ResetMultiplayerToolsTipStatus()
{
if (EditorPrefs.HasKey(AutoAddNetworkObjectIfNoneExists))
{
EditorPrefs.SetBool(AutoAddNetworkObjectIfNoneExists, false);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using UnityEditor;
using UnityEngine;
using UnityEditorInternal;
using Unity.Netcode.Editor.Configuration;

namespace Unity.Netcode.Editor
{
Expand All @@ -14,7 +15,6 @@ namespace Unity.Netcode.Editor
[CanEditMultipleObjects]
public class NetworkManagerEditor : UnityEditor.Editor
{
internal const string InstallMultiplayerToolsTipDismissedPlayerPrefKey = "Netcode_Tip_InstallMPTools_Dismissed";
private static GUIStyle s_CenteredWordWrappedLabelStyle;
private static GUIStyle s_HelpBoxStyle;

Expand Down Expand Up @@ -359,7 +359,7 @@ private static void DrawInstallMultiplayerToolsTip()
const string targetUrl = "https://docs-multiplayer.unity3d.com/netcode/current/tools/install-tools";
const string infoIconName = "console.infoicon";

if (PlayerPrefs.GetInt(InstallMultiplayerToolsTipDismissedPlayerPrefKey, 0) != 0)
if (NetcodeForGameObjectsSettings.GetNetcodeInstallMultiplayerToolTips() != 0)
{
return;
}
Expand Down Expand Up @@ -405,7 +405,7 @@ private static void DrawInstallMultiplayerToolsTip()
GUILayout.FlexibleSpace();
if (GUILayout.Button(dismissButtonText, dismissButtonStyle, GUILayout.ExpandWidth(false)))
{
PlayerPrefs.SetInt(InstallMultiplayerToolsTipDismissedPlayerPrefKey, 1);
NetcodeForGameObjectsSettings.SetNetcodeInstallMultiplayerToolTips(1);
}
EditorGUIUtility.AddCursorRect(GUILayoutUtility.GetLastRect(), MouseCursor.Link);
GUILayout.FlexibleSpace();
Expand Down
3 changes: 0 additions & 3 deletions com.unity.netcode.gameobjects/Tests/Editor/UI.meta

This file was deleted.

15 changes: 0 additions & 15 deletions com.unity.netcode.gameobjects/Tests/Editor/UI/UITestHelpers.cs

This file was deleted.

This file was deleted.