From a4a1d3b13a28de5fcd15452b210c01e0a4d96a64 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Fri, 7 Apr 2023 13:09:07 +0100 Subject: [PATCH] fix: only copy Unity-serialized members --- X10D.Unity/src/ComponentExtensions.cs | 32 ++++++--------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/X10D.Unity/src/ComponentExtensions.cs b/X10D.Unity/src/ComponentExtensions.cs index 93afe4d..9a7dafc 100644 --- a/X10D.Unity/src/ComponentExtensions.cs +++ b/X10D.Unity/src/ComponentExtensions.cs @@ -1,6 +1,7 @@ using System.Globalization; using System.Reflection; using UnityEngine; +using X10D.Reflection; using Object = UnityEngine.Object; namespace X10D.Unity; @@ -52,7 +53,6 @@ public static class ComponentExtensions var typeInfo = typeof(T).GetTypeInfo(); CopyFields(typeInfo, component, targetComponent); - CopyProperties(typeInfo, component, targetComponent); } /// @@ -96,7 +96,6 @@ public static class ComponentExtensions var typeInfo = componentType.GetTypeInfo(); CopyFields(typeInfo, component, targetComponent); - CopyProperties(typeInfo, component, targetComponent); } /// @@ -184,7 +183,12 @@ public static class ComponentExtensions { foreach (FieldInfo field in typeInfo.DeclaredFields) { - if (field.IsStatic) + if (field.IsStatic || !field.IsPublic && !field.HasCustomAttribute()) + { + continue; + } + + if (field.HasCustomAttribute()) { continue; } @@ -194,28 +198,6 @@ public static class ComponentExtensions } } - private static void CopyProperties(TypeInfo typeInfo, T component, T targetComponent) - where T : Component - { - foreach (PropertyInfo property in typeInfo.DeclaredProperties) - { - if (!property.CanRead || !property.CanWrite) - { - continue; - } - - MethodInfo getMethod = property.GetMethod; - MethodInfo setMethod = property.SetMethod; - if (getMethod.IsStatic || setMethod.IsStatic) - { - continue; - } - - object propertyValue = GetNewReferences(component, targetComponent, property.GetValue(component)); - property.SetValue(targetComponent, propertyValue); - } - } - private static object GetNewReferences(T component, T targetComponent, object value) where T : Component {