Skip to content

Commit b4b3929

Browse files
refactor: Migrate menu settings to project settings [MTT-5001] (#2285)
* update Migrated the multiplayer tools install reminder over to the new Netcode for GameObjects settings. Deleted the UITestHelpers class completely as it is no longer needed.
1 parent 3492ba9 commit b4b3929

11 files changed

+176
-43
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ Additional documentation and release notes are available at [Multiplayer Documen
99

1010
## [Unreleased]
1111

12+
### Added
13+
- Added `NetworkObject` auto-add helper and Multiplayer Tools install reminder settings to Project Settings. (#2285)
14+
1215
### Fixed
1316

1417
- Fixed issue where in-scene placed `NetworkObjects` were not honoring the `AutoObjectParentSync` property. (#2281)
1518
- 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)
1619
- 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)
1720

21+
### Removed
22+
- Removed the `NetworkObject` auto-add and Multiplayer Tools install reminder settings from the Menu interface. (#2285)
23+
24+
1825
## [1.1.0] - 2022-10-21
1926

2027
### Added

com.unity.netcode.gameobjects/Editor/Configuration.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using UnityEditor;
2+
3+
4+
namespace Unity.Netcode.Editor.Configuration
5+
{
6+
internal class NetcodeForGameObjectsSettings
7+
{
8+
internal const string AutoAddNetworkObjectIfNoneExists = "AutoAdd-NetworkObject-When-None-Exist";
9+
internal const string InstallMultiplayerToolsTipDismissedPlayerPrefKey = "Netcode_Tip_InstallMPTools_Dismissed";
10+
11+
internal static int GetNetcodeInstallMultiplayerToolTips()
12+
{
13+
if (EditorPrefs.HasKey(InstallMultiplayerToolsTipDismissedPlayerPrefKey))
14+
{
15+
return EditorPrefs.GetInt(InstallMultiplayerToolsTipDismissedPlayerPrefKey);
16+
}
17+
return 0;
18+
}
19+
20+
internal static void SetNetcodeInstallMultiplayerToolTips(int toolTipPrefSetting)
21+
{
22+
EditorPrefs.SetInt(InstallMultiplayerToolsTipDismissedPlayerPrefKey, toolTipPrefSetting);
23+
}
24+
25+
internal static bool GetAutoAddNetworkObjectSetting()
26+
{
27+
if (EditorPrefs.HasKey(AutoAddNetworkObjectIfNoneExists))
28+
{
29+
return EditorPrefs.GetBool(AutoAddNetworkObjectIfNoneExists);
30+
}
31+
return false;
32+
}
33+
34+
internal static void SetAutoAddNetworkObjectSetting(bool autoAddSetting)
35+
{
36+
EditorPrefs.SetBool(AutoAddNetworkObjectIfNoneExists, autoAddSetting);
37+
}
38+
}
39+
}

com.unity.netcode.gameobjects/Editor/Configuration/NetcodeForGameObjectsSettings.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Unity.Netcode.Editor.Configuration
5+
{
6+
internal static class NetcodeSettingsProvider
7+
{
8+
[SettingsProvider]
9+
public static SettingsProvider CreateNetcodeSettingsProvider()
10+
{
11+
// First parameter is the path in the Settings window.
12+
// Second parameter is the scope of this setting: it only appears in the Settings window for the Project scope.
13+
var provider = new SettingsProvider("Project/NetcodeForGameObjects", SettingsScope.Project)
14+
{
15+
label = "Netcode for GameObjects",
16+
keywords = new[] { "netcode", "editor" },
17+
guiHandler = OnGuiHandler,
18+
};
19+
20+
return provider;
21+
}
22+
23+
internal static NetcodeSettingsLabel NetworkObjectsSectionLabel = new NetcodeSettingsLabel("NetworkObject Helper Settings", 20);
24+
internal static NetcodeSettingsToggle AutoAddNetworkObjectToggle = new NetcodeSettingsToggle("Auto-Add NetworkObjects", "When enabled, NetworkObjects are automatically added to GameObjects when NetworkBehaviours are added first.", 20);
25+
internal static NetcodeSettingsLabel MultiplayerToolsLabel = new NetcodeSettingsLabel("Multiplayer Tools", 20);
26+
internal static NetcodeSettingsToggle MultiplayerToolTipStatusToggle = new NetcodeSettingsToggle("Multiplayer Tools Install Reminder", "When enabled, the NetworkManager will display " +
27+
"the notification to install the multiplayer tools package.", 20);
28+
29+
private static void OnGuiHandler(string obj)
30+
{
31+
var autoAddNetworkObjectSetting = NetcodeForGameObjectsSettings.GetAutoAddNetworkObjectSetting();
32+
var multiplayerToolsTipStatus = NetcodeForGameObjectsSettings.GetNetcodeInstallMultiplayerToolTips() == 0;
33+
EditorGUI.BeginChangeCheck();
34+
NetworkObjectsSectionLabel.DrawLabel();
35+
autoAddNetworkObjectSetting = AutoAddNetworkObjectToggle.DrawToggle(autoAddNetworkObjectSetting);
36+
MultiplayerToolsLabel.DrawLabel();
37+
multiplayerToolsTipStatus = MultiplayerToolTipStatusToggle.DrawToggle(multiplayerToolsTipStatus);
38+
if (EditorGUI.EndChangeCheck())
39+
{
40+
NetcodeForGameObjectsSettings.SetAutoAddNetworkObjectSetting(autoAddNetworkObjectSetting);
41+
NetcodeForGameObjectsSettings.SetNetcodeInstallMultiplayerToolTips(multiplayerToolsTipStatus ? 0 : 1);
42+
}
43+
}
44+
}
45+
46+
internal class NetcodeSettingsLabel : NetcodeGUISettings
47+
{
48+
private string m_LabelContent;
49+
50+
public void DrawLabel()
51+
{
52+
EditorGUIUtility.labelWidth = m_LabelSize;
53+
GUILayout.Label(m_LabelContent, EditorStyles.boldLabel, m_LayoutWidth);
54+
}
55+
56+
public NetcodeSettingsLabel(string labelText, float layoutOffset = 0.0f)
57+
{
58+
m_LabelContent = labelText;
59+
AdjustLableSize(labelText, layoutOffset);
60+
}
61+
}
62+
63+
internal class NetcodeSettingsToggle : NetcodeGUISettings
64+
{
65+
private GUIContent m_ToggleContent;
66+
67+
public bool DrawToggle(bool currentSetting)
68+
{
69+
EditorGUIUtility.labelWidth = m_LabelSize;
70+
return EditorGUILayout.Toggle(m_ToggleContent, currentSetting, m_LayoutWidth);
71+
}
72+
73+
public NetcodeSettingsToggle(string labelText, string toolTip, float layoutOffset)
74+
{
75+
AdjustLableSize(labelText, layoutOffset);
76+
m_ToggleContent = new GUIContent(labelText, toolTip);
77+
}
78+
}
79+
80+
internal class NetcodeGUISettings
81+
{
82+
private const float k_MaxLabelWidth = 450f;
83+
protected float m_LabelSize { get; private set; }
84+
85+
protected GUILayoutOption m_LayoutWidth { get; private set; }
86+
87+
protected void AdjustLableSize(string labelText, float offset = 0.0f)
88+
{
89+
m_LabelSize = Mathf.Min(k_MaxLabelWidth, EditorStyles.label.CalcSize(new GUIContent(labelText)).x);
90+
m_LayoutWidth = GUILayout.Width(m_LabelSize + offset);
91+
}
92+
}
93+
94+
}

com.unity.netcode.gameobjects/Editor/Configuration/NetcodeSettingsProvider.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Reflection;
44
using UnityEngine;
55
using UnityEditor;
6+
using Unity.Netcode.Editor.Configuration;
67

78
namespace Unity.Netcode.Editor
89
{
@@ -230,8 +231,6 @@ private void OnEnable()
230231
CheckForNetworkObject((target as NetworkBehaviour).gameObject);
231232
}
232233

233-
internal const string AutoAddNetworkObjectIfNoneExists = "AutoAdd-NetworkObject-When-None-Exist";
234-
235234
/// <summary>
236235
/// Recursively finds the root parent of a <see cref="Transform"/>
237236
/// </summary>
@@ -308,7 +307,7 @@ public static void CheckForNetworkObject(GameObject gameObject, bool networkObje
308307
// and the user has already turned "Auto-Add NetworkObject" on when first notified about the requirement
309308
// then just send a reminder to the user why the NetworkObject they just deleted seemingly "re-appeared"
310309
// again.
311-
if (networkObjectRemoved && EditorPrefs.HasKey(AutoAddNetworkObjectIfNoneExists) && EditorPrefs.GetBool(AutoAddNetworkObjectIfNoneExists))
310+
if (networkObjectRemoved && NetcodeForGameObjectsSettings.GetAutoAddNetworkObjectSetting())
312311
{
313312
Debug.LogWarning($"{gameObject.name} still has {nameof(NetworkBehaviour)}s and Auto-Add NetworkObjects is enabled. A NetworkObject is being added back to {gameObject.name}.");
314313
Debug.Log($"To reset Auto-Add NetworkObjects: Select the Netcode->General->Reset Auto-Add NetworkObject menu item.");
@@ -317,7 +316,7 @@ public static void CheckForNetworkObject(GameObject gameObject, bool networkObje
317316
// Notify and provide the option to add it one time, always add a NetworkObject, or do nothing and let the user manually add it
318317
if (EditorUtility.DisplayDialog($"{nameof(NetworkBehaviour)}s require a {nameof(NetworkObject)}",
319318
$"{gameObject.name} does not have a {nameof(NetworkObject)} component. Would you like to add one now?", "Yes", "No (manually add it)",
320-
DialogOptOutDecisionType.ForThisMachine, AutoAddNetworkObjectIfNoneExists))
319+
DialogOptOutDecisionType.ForThisMachine, NetcodeForGameObjectsSettings.AutoAddNetworkObjectIfNoneExists))
321320
{
322321
gameObject.AddComponent<NetworkObject>();
323322
var activeScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
@@ -327,20 +326,5 @@ public static void CheckForNetworkObject(GameObject gameObject, bool networkObje
327326
}
328327
}
329328
}
330-
331-
/// <summary>
332-
/// This allows users to reset the Auto-Add NetworkObject preference
333-
/// so the next time they add a NetworkBehaviour to a GameObject without
334-
/// a NetworkObject it will display the dialog box again and not
335-
/// automatically add a NetworkObject.
336-
/// </summary>
337-
[MenuItem("Netcode/General/Reset Auto-Add NetworkObject", false, 1)]
338-
private static void ResetMultiplayerToolsTipStatus()
339-
{
340-
if (EditorPrefs.HasKey(AutoAddNetworkObjectIfNoneExists))
341-
{
342-
EditorPrefs.SetBool(AutoAddNetworkObjectIfNoneExists, false);
343-
}
344-
}
345329
}
346330
}

com.unity.netcode.gameobjects/Editor/NetworkManagerEditor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UnityEditor;
44
using UnityEngine;
55
using UnityEditorInternal;
6+
using Unity.Netcode.Editor.Configuration;
67

78
namespace Unity.Netcode.Editor
89
{
@@ -14,7 +15,6 @@ namespace Unity.Netcode.Editor
1415
[CanEditMultipleObjects]
1516
public class NetworkManagerEditor : UnityEditor.Editor
1617
{
17-
internal const string InstallMultiplayerToolsTipDismissedPlayerPrefKey = "Netcode_Tip_InstallMPTools_Dismissed";
1818
private static GUIStyle s_CenteredWordWrappedLabelStyle;
1919
private static GUIStyle s_HelpBoxStyle;
2020

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

362-
if (PlayerPrefs.GetInt(InstallMultiplayerToolsTipDismissedPlayerPrefKey, 0) != 0)
362+
if (NetcodeForGameObjectsSettings.GetNetcodeInstallMultiplayerToolTips() != 0)
363363
{
364364
return;
365365
}
@@ -405,7 +405,7 @@ private static void DrawInstallMultiplayerToolsTip()
405405
GUILayout.FlexibleSpace();
406406
if (GUILayout.Button(dismissButtonText, dismissButtonStyle, GUILayout.ExpandWidth(false)))
407407
{
408-
PlayerPrefs.SetInt(InstallMultiplayerToolsTipDismissedPlayerPrefKey, 1);
408+
NetcodeForGameObjectsSettings.SetNetcodeInstallMultiplayerToolTips(1);
409409
}
410410
EditorGUIUtility.AddCursorRect(GUILayoutUtility.GetLastRect(), MouseCursor.Link);
411411
GUILayout.FlexibleSpace();

com.unity.netcode.gameobjects/Tests/Editor/UI.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.

com.unity.netcode.gameobjects/Tests/Editor/UI/UITestHelpers.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)