diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index 1aa0c61e84..a69e3ca8a9 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -22,6 +22,8 @@ Additional documentation and release notes are available at [Multiplayer Documen - 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) - Errors are no longer thrown when entering play mode with domain reload disabled (#2720) - NetworkSpawn is now correctly called each time when entering play mode with scene reload disabled (#2720) +- NetworkVariables of non-integer types will no longer break the inspector (#2714) +- NetworkVariables with NonSerializedAttribute will not appear in the inspector (#2714) - 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) - 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) - Fixed issue where `SpawnWithObservers` was not being honored when `NetworkConfig.EnableSceneManagement` was disabled. (#2682) diff --git a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs index e9ccea6526..c15ea8a027 100644 --- a/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs +++ b/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs @@ -8,6 +8,9 @@ using Mono.Cecil.Rocks; using Unity.CompilationPipeline.Common.Diagnostics; using Unity.CompilationPipeline.Common.ILPostProcessing; +#if UNITY_EDITOR +using UnityEditor; +#endif using UnityEngine; using ILPPInterface = Unity.CompilationPipeline.Common.ILPostProcessing.ILPostProcessor; using MethodAttributes = Mono.Cecil.MethodAttributes; @@ -66,7 +69,7 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly) { m_MainModule = mainModule; - if (ImportReferences(mainModule)) + if (ImportReferences(mainModule, compiledAssembly.Defines)) { // process `NetworkBehaviour` types try @@ -107,7 +110,7 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly) } } - CreateNetworkVariableTypeInitializers(assemblyDefinition); + CreateNetworkVariableTypeInitializers(assemblyDefinition, compiledAssembly.Defines); } catch (Exception e) { @@ -166,7 +169,7 @@ private bool IsSpecialCaseType(TypeReference type) return false; } - private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly) + private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly, string[] assemblyDefines) { var typeDefinition = new TypeDefinition("__GEN", "NetworkVariableSerializationHelper", TypeAttributes.NotPublic | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit, assembly.MainModule.TypeSystem.Object); @@ -176,7 +179,15 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly) MethodAttributes.Static, assembly.MainModule.TypeSystem.Void); staticCtorMethodDef.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); - staticCtorMethodDef.CustomAttributes.Add(new CustomAttribute(m_RuntimeInitializeOnLoadAttribute_Ctor)); + bool isEditor = assemblyDefines.Contains("UNITY_EDITOR"); + if (isEditor) + { + staticCtorMethodDef.CustomAttributes.Add(new CustomAttribute(m_InitializeOnLoadAttribute_Ctor)); + } + else + { + staticCtorMethodDef.CustomAttributes.Add(new CustomAttribute(m_RuntimeInitializeOnLoadAttribute_Ctor)); + } typeDefinition.Methods.Add(staticCtorMethodDef); @@ -401,6 +412,7 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly) private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_ManagedClassEquals_MethodRef; private MethodReference m_RuntimeInitializeOnLoadAttribute_Ctor; + private MethodReference m_InitializeOnLoadAttribute_Ctor; private MethodReference m_ExceptionCtorMethodReference; private MethodReference m_List_NetworkVariableBase_Add; @@ -505,7 +517,7 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly) // CodeGen cannot reference the collections assembly to do a typeof() on it due to a bug that causes that to crash. private const string k_INativeListBool_FullName = "Unity.Collections.INativeList`1"; - private bool ImportReferences(ModuleDefinition moduleDefinition) + private bool ImportReferences(ModuleDefinition moduleDefinition, string[] assemblyDefines) { TypeDefinition debugTypeDef = null; foreach (var unityTypeDef in m_UnityModule.GetAllTypes()) @@ -517,6 +529,13 @@ private bool ImportReferences(ModuleDefinition moduleDefinition) } } + + bool isEditor = assemblyDefines.Contains("UNITY_EDITOR"); + if (isEditor) + { + m_InitializeOnLoadAttribute_Ctor = moduleDefinition.ImportReference(typeof(InitializeOnLoadMethodAttribute).GetConstructor(new Type[] { })); + } + m_RuntimeInitializeOnLoadAttribute_Ctor = moduleDefinition.ImportReference(typeof(RuntimeInitializeOnLoadMethodAttribute).GetConstructor(new Type[] { })); TypeDefinition networkManagerTypeDef = null; diff --git a/com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs b/com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs index 84af9377dd..7d57afb055 100644 --- a/com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs +++ b/com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs @@ -37,12 +37,12 @@ private void Init(MonoScript script) for (int i = 0; i < fields.Length; i++) { var ft = fields[i].FieldType; - if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkVariable<>) && !fields[i].IsDefined(typeof(HideInInspector), true)) + if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkVariable<>) && !fields[i].IsDefined(typeof(HideInInspector), true) && !fields[i].IsDefined(typeof(NonSerializedAttribute), true)) { m_NetworkVariableNames.Add(ObjectNames.NicifyVariableName(fields[i].Name)); m_NetworkVariableFields.Add(ObjectNames.NicifyVariableName(fields[i].Name), fields[i]); } - if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkList<>) && !fields[i].IsDefined(typeof(HideInInspector), true)) + if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkList<>) && !fields[i].IsDefined(typeof(HideInInspector), true) && !fields[i].IsDefined(typeof(NonSerializedAttribute), true)) { m_NetworkVariableNames.Add(ObjectNames.NicifyVariableName(fields[i].Name)); m_NetworkVariableFields.Add(ObjectNames.NicifyVariableName(fields[i].Name), fields[i]);