fix: Errors with NetworkVariable<float> and others in inspector (#2714)

Also fixes NetworkVariables with NonSerializedAttribute showing in editor when they should not.
This commit is contained in:
Kitty Draper 2023-10-04 15:36:50 -05:00 коммит произвёл GitHub
Родитель 6353f59014
Коммит a60288fcc2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 28 добавлений и 7 удалений

Просмотреть файл

@ -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)

Просмотреть файл

@ -8,6 +8,9 @@ using Mono.Cecil.Cil;
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 @@ namespace Unity.Netcode.Editor.CodeGen
{
m_MainModule = mainModule;
if (ImportReferences(mainModule))
if (ImportReferences(mainModule, compiledAssembly.Defines))
{
// process `NetworkBehaviour` types
try
@ -107,7 +110,7 @@ namespace Unity.Netcode.Editor.CodeGen
}
}
CreateNetworkVariableTypeInitializers(assemblyDefinition);
CreateNetworkVariableTypeInitializers(assemblyDefinition, compiledAssembly.Defines);
}
catch (Exception e)
{
@ -166,7 +169,7 @@ namespace Unity.Netcode.Editor.CodeGen
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 @@ namespace Unity.Netcode.Editor.CodeGen
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 @@ namespace Unity.Netcode.Editor.CodeGen
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 @@ namespace Unity.Netcode.Editor.CodeGen
// 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<System.Byte>";
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 @@ namespace Unity.Netcode.Editor.CodeGen
}
}
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;

Просмотреть файл

@ -37,12 +37,12 @@ namespace Unity.Netcode.Editor
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]);