Skip to content

Commit 1c58326

Browse files
authored
fix: NetworkVariables of enum types breaking the inspector [MTT-6212] (#2529)
1 parent c0d9087 commit 1c58326

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1717
- Fixed issue where a client could throw an exception if abruptly disconnected from a network session with one or more spawned `NetworkObject`(s). (#2510)
1818
- Fixed issue where invalid endpoint addresses were not being detected and returning false from NGO UnityTransport. (#2496)
1919
- Fixed some errors that could occur if a connection is lost and the loss is detected when attempting to write to the socket. (#2495)
20+
- Fixed the inspector throwing exceptions when attempting to render `NetworkVariable`s of enum types. (#2529)
2021
- Fixed an exception and error logging when two different objects are shown and hidden on the same frame (#2524)
2122

2223
## Changed

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,25 @@ private void RenderNetworkVariable(int index)
8181
EditorGUILayout.BeginHorizontal();
8282
if (genericType.IsValueType)
8383
{
84-
var method = typeof(NetworkBehaviourEditor).GetMethod("RenderNetworkContainerValueType", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
84+
var isEquatable = false;
85+
foreach (var iface in genericType.GetInterfaces())
86+
{
87+
if (iface.IsGenericType && iface.GetGenericTypeDefinition() == typeof(IEquatable<>))
88+
{
89+
isEquatable = true;
90+
}
91+
}
92+
93+
MethodInfo method;
94+
if (isEquatable)
95+
{
96+
method = typeof(NetworkBehaviourEditor).GetMethod(nameof(RenderNetworkContainerValueTypeIEquatable), BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
97+
}
98+
else
99+
{
100+
method = typeof(NetworkBehaviourEditor).GetMethod(nameof(RenderNetworkContainerValueType), BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
101+
}
102+
85103
var genericMethod = method.MakeGenericMethod(genericType);
86104
genericMethod.Invoke(this, new[] { (object)index });
87105
}
@@ -94,7 +112,23 @@ private void RenderNetworkVariable(int index)
94112
}
95113
}
96114

97-
private void RenderNetworkContainerValueType<T>(int index) where T : unmanaged, IEquatable<T>
115+
private void RenderNetworkContainerValueType<T>(int index) where T : unmanaged
116+
{
117+
try
118+
{
119+
var networkVariable = (NetworkVariable<T>)m_NetworkVariableFields[m_NetworkVariableNames[index]].GetValue(target);
120+
RenderNetworkVariableValueType(index, networkVariable);
121+
}
122+
catch (Exception e)
123+
{
124+
Debug.Log(e);
125+
throw;
126+
}
127+
128+
EditorGUILayout.EndHorizontal();
129+
}
130+
131+
private void RenderNetworkContainerValueTypeIEquatable<T>(int index) where T : unmanaged, IEquatable<T>
98132
{
99133
try
100134
{
@@ -240,7 +274,7 @@ public override void OnInspectorGUI()
240274
bool expanded = true;
241275
while (property.NextVisible(expanded))
242276
{
243-
if (m_NetworkVariableNames.Contains(property.name))
277+
if (m_NetworkVariableNames.Contains(ObjectNames.NicifyVariableName(property.name)))
244278
{
245279
// Skip rendering of NetworkVars, they have special rendering
246280
continue;

0 commit comments

Comments
 (0)