Skip to content

Commit a60288f

Browse files
authored
fix: Errors with NetworkVariable<float> and others in inspector (#2714)
Also fixes NetworkVariables with NonSerializedAttribute showing in editor when they should not.
1 parent 6353f59 commit a60288f

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Additional documentation and release notes are available at [Multiplayer Documen
2222
- Rpcs within Generic NetworkBehaviour types can now serialize parameters of the class's generic types (but may not have generic types of their own) (#2720)
2323
- Errors are no longer thrown when entering play mode with domain reload disabled (#2720)
2424
- NetworkSpawn is now correctly called each time when entering play mode with scene reload disabled (#2720)
25+
- NetworkVariables of non-integer types will no longer break the inspector (#2714)
26+
- NetworkVariables with NonSerializedAttribute will not appear in the inspector (#2714)
2527
- Fixed issue where `UnityTransport` would attempt to establish WebSocket connections even if using UDP/DTLS Relay allocations when the build target was WebGL. This only applied to working in the editor since UDP/DTLS can't work in the browser. (#2695)
2628
- Fixed issue where a `NetworkBehaviour` component's `OnNetworkDespawn` was not being invoked on the host-server side for an in-scene placed `NetworkObject` when a scene was unloaded (during a scene transition) and the `NetworkBehaviour` component was positioned/ordered before the `NetworkObject` component. (#2685)
2729
- Fixed issue where `SpawnWithObservers` was not being honored when `NetworkConfig.EnableSceneManagement` was disabled. (#2682)

com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
using Mono.Cecil.Rocks;
99
using Unity.CompilationPipeline.Common.Diagnostics;
1010
using Unity.CompilationPipeline.Common.ILPostProcessing;
11+
#if UNITY_EDITOR
12+
using UnityEditor;
13+
#endif
1114
using UnityEngine;
1215
using ILPPInterface = Unity.CompilationPipeline.Common.ILPostProcessing.ILPostProcessor;
1316
using MethodAttributes = Mono.Cecil.MethodAttributes;
@@ -66,7 +69,7 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
6669
{
6770
m_MainModule = mainModule;
6871

69-
if (ImportReferences(mainModule))
72+
if (ImportReferences(mainModule, compiledAssembly.Defines))
7073
{
7174
// process `NetworkBehaviour` types
7275
try
@@ -107,7 +110,7 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
107110
}
108111
}
109112

110-
CreateNetworkVariableTypeInitializers(assemblyDefinition);
113+
CreateNetworkVariableTypeInitializers(assemblyDefinition, compiledAssembly.Defines);
111114
}
112115
catch (Exception e)
113116
{
@@ -166,7 +169,7 @@ private bool IsSpecialCaseType(TypeReference type)
166169
return false;
167170
}
168171

169-
private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly)
172+
private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly, string[] assemblyDefines)
170173
{
171174
var typeDefinition = new TypeDefinition("__GEN", "NetworkVariableSerializationHelper", TypeAttributes.NotPublic | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit, assembly.MainModule.TypeSystem.Object);
172175

@@ -176,7 +179,15 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly)
176179
MethodAttributes.Static,
177180
assembly.MainModule.TypeSystem.Void);
178181
staticCtorMethodDef.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
179-
staticCtorMethodDef.CustomAttributes.Add(new CustomAttribute(m_RuntimeInitializeOnLoadAttribute_Ctor));
182+
bool isEditor = assemblyDefines.Contains("UNITY_EDITOR");
183+
if (isEditor)
184+
{
185+
staticCtorMethodDef.CustomAttributes.Add(new CustomAttribute(m_InitializeOnLoadAttribute_Ctor));
186+
}
187+
else
188+
{
189+
staticCtorMethodDef.CustomAttributes.Add(new CustomAttribute(m_RuntimeInitializeOnLoadAttribute_Ctor));
190+
}
180191
typeDefinition.Methods.Add(staticCtorMethodDef);
181192

182193

@@ -401,6 +412,7 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly)
401412
private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_ManagedClassEquals_MethodRef;
402413

403414
private MethodReference m_RuntimeInitializeOnLoadAttribute_Ctor;
415+
private MethodReference m_InitializeOnLoadAttribute_Ctor;
404416

405417
private MethodReference m_ExceptionCtorMethodReference;
406418
private MethodReference m_List_NetworkVariableBase_Add;
@@ -505,7 +517,7 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly)
505517
// CodeGen cannot reference the collections assembly to do a typeof() on it due to a bug that causes that to crash.
506518
private const string k_INativeListBool_FullName = "Unity.Collections.INativeList`1<System.Byte>";
507519

508-
private bool ImportReferences(ModuleDefinition moduleDefinition)
520+
private bool ImportReferences(ModuleDefinition moduleDefinition, string[] assemblyDefines)
509521
{
510522
TypeDefinition debugTypeDef = null;
511523
foreach (var unityTypeDef in m_UnityModule.GetAllTypes())
@@ -517,6 +529,13 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
517529
}
518530
}
519531

532+
533+
bool isEditor = assemblyDefines.Contains("UNITY_EDITOR");
534+
if (isEditor)
535+
{
536+
m_InitializeOnLoadAttribute_Ctor = moduleDefinition.ImportReference(typeof(InitializeOnLoadMethodAttribute).GetConstructor(new Type[] { }));
537+
}
538+
520539
m_RuntimeInitializeOnLoadAttribute_Ctor = moduleDefinition.ImportReference(typeof(RuntimeInitializeOnLoadMethodAttribute).GetConstructor(new Type[] { }));
521540

522541
TypeDefinition networkManagerTypeDef = null;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ private void Init(MonoScript script)
3737
for (int i = 0; i < fields.Length; i++)
3838
{
3939
var ft = fields[i].FieldType;
40-
if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkVariable<>) && !fields[i].IsDefined(typeof(HideInInspector), true))
40+
if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkVariable<>) && !fields[i].IsDefined(typeof(HideInInspector), true) && !fields[i].IsDefined(typeof(NonSerializedAttribute), true))
4141
{
4242
m_NetworkVariableNames.Add(ObjectNames.NicifyVariableName(fields[i].Name));
4343
m_NetworkVariableFields.Add(ObjectNames.NicifyVariableName(fields[i].Name), fields[i]);
4444
}
45-
if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkList<>) && !fields[i].IsDefined(typeof(HideInInspector), true))
45+
if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkList<>) && !fields[i].IsDefined(typeof(HideInInspector), true) && !fields[i].IsDefined(typeof(NonSerializedAttribute), true))
4646
{
4747
m_NetworkVariableNames.Add(ObjectNames.NicifyVariableName(fields[i].Name));
4848
m_NetworkVariableFields.Add(ObjectNames.NicifyVariableName(fields[i].Name), fields[i]);

0 commit comments

Comments
 (0)