Skip to content

Commit b8f3929

Browse files
fix: NetcodeSettingsProvider Exception [MTT-5217] (#2345)
* fix This fixes the exception in 2020.3.x versions of Unity where the EditorStyles.label is not yet set when instantiating label and toggle elements during declaration (unlike later versions of Unity where it does).
1 parent e3b837a commit b8f3929

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ Additional documentation and release notes are available at [Multiplayer Documen
1919
- Renamed the NetworkTransform.SetState parameter `shouldGhostsInterpolate` to `teleportDisabled` for better clarity of what that parameter does. (#2228)
2020

2121
### Fixed
22+
- Fixed issue where `NetcodeSettingsProvider` would throw an exception in Unity 2020.3.x versions. (#2345)
2223
- Fixed server side issue where, depending upon component ordering, some NetworkBehaviour components might not have their OnNetworkDespawn method invoked if the client side disconnected. (#2323)
2324
- Fixed a case where data corruption could occur when using UnityTransport when reaching a certain level of send throughput. (#2332)
24-
2525
- Fixed an issue in `UnityTransport` where an exception would be thrown if starting a Relay host/server on WebGL. This exception should only be thrown if using direct connections (where WebGL can't act as a host/server). (#2321)
2626

2727
## [1.2.0] - 2022-11-21

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

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,46 @@ public static SettingsProvider CreateNetcodeSettingsProvider()
2020
return provider;
2121
}
2222

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);
23+
internal static NetcodeSettingsLabel NetworkObjectsSectionLabel;
24+
internal static NetcodeSettingsToggle AutoAddNetworkObjectToggle;
25+
internal static NetcodeSettingsLabel MultiplayerToolsLabel;
26+
internal static NetcodeSettingsToggle MultiplayerToolTipStatusToggle;
27+
28+
/// <summary>
29+
/// Creates an instance of the settings UI Elements if they don't already exist.
30+
/// </summary>
31+
/// <remarks>
32+
/// We have to construct any NetcodeGUISettings derived classes here because in
33+
/// version 2020.x.x EditorStyles.label does not exist yet (higher versions it does)
34+
/// </remarks>
35+
private static void CheckForInitialize()
36+
{
37+
if (NetworkObjectsSectionLabel == null)
38+
{
39+
NetworkObjectsSectionLabel = new NetcodeSettingsLabel("NetworkObject Helper Settings", 20);
40+
}
41+
42+
if (AutoAddNetworkObjectToggle == null)
43+
{
44+
AutoAddNetworkObjectToggle = new NetcodeSettingsToggle("Auto-Add NetworkObjects", "When enabled, NetworkObjects are automatically added to GameObjects when NetworkBehaviours are added first.", 20);
45+
}
46+
47+
if (MultiplayerToolsLabel == null)
48+
{
49+
MultiplayerToolsLabel = new NetcodeSettingsLabel("Multiplayer Tools", 20);
50+
}
51+
52+
if (MultiplayerToolTipStatusToggle == null)
53+
{
54+
MultiplayerToolTipStatusToggle = new NetcodeSettingsToggle("Multiplayer Tools Install Reminder", "When enabled, the NetworkManager will display the notification to install the multiplayer tools package.", 20);
55+
}
56+
}
2857

2958
private static void OnGuiHandler(string obj)
3059
{
60+
// Make sure all NetcodeGUISettings derived classes are instantiated first
61+
CheckForInitialize();
62+
3163
var autoAddNetworkObjectSetting = NetcodeForGameObjectsSettings.GetAutoAddNetworkObjectSetting();
3264
var multiplayerToolsTipStatus = NetcodeForGameObjectsSettings.GetNetcodeInstallMultiplayerToolTips() == 0;
3365
EditorGUI.BeginChangeCheck();
@@ -56,7 +88,7 @@ public void DrawLabel()
5688
public NetcodeSettingsLabel(string labelText, float layoutOffset = 0.0f)
5789
{
5890
m_LabelContent = labelText;
59-
AdjustLableSize(labelText, layoutOffset);
91+
AdjustLabelSize(labelText, layoutOffset);
6092
}
6193
}
6294

@@ -72,7 +104,7 @@ public bool DrawToggle(bool currentSetting)
72104

73105
public NetcodeSettingsToggle(string labelText, string toolTip, float layoutOffset)
74106
{
75-
AdjustLableSize(labelText, layoutOffset);
107+
AdjustLabelSize(labelText, layoutOffset);
76108
m_ToggleContent = new GUIContent(labelText, toolTip);
77109
}
78110
}
@@ -84,11 +116,10 @@ internal class NetcodeGUISettings
84116

85117
protected GUILayoutOption m_LayoutWidth { get; private set; }
86118

87-
protected void AdjustLableSize(string labelText, float offset = 0.0f)
119+
protected void AdjustLabelSize(string labelText, float offset = 0.0f)
88120
{
89121
m_LabelSize = Mathf.Min(k_MaxLabelWidth, EditorStyles.label.CalcSize(new GUIContent(labelText)).x);
90122
m_LayoutWidth = GUILayout.Width(m_LabelSize + offset);
91123
}
92124
}
93-
94125
}

0 commit comments

Comments
 (0)