1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-25 13:38:49 +00:00

fix: only copy Unity-serialized members

This commit is contained in:
Oliver Booth 2023-04-07 13:09:07 +01:00
parent ad2d33aa88
commit a4a1d3b13a
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025

View File

@ -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);
}
/// <summary>
@ -96,7 +96,6 @@ public static class ComponentExtensions
var typeInfo = componentType.GetTypeInfo();
CopyFields(typeInfo, component, targetComponent);
CopyProperties(typeInfo, component, targetComponent);
}
/// <summary>
@ -184,7 +183,12 @@ public static class ComponentExtensions
{
foreach (FieldInfo field in typeInfo.DeclaredFields)
{
if (field.IsStatic)
if (field.IsStatic || !field.IsPublic && !field.HasCustomAttribute<SerializeField>())
{
continue;
}
if (field.HasCustomAttribute<NonSerializedAttribute>())
{
continue;
}
@ -194,28 +198,6 @@ public static class ComponentExtensions
}
}
private static void CopyProperties<T>(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>(T component, T targetComponent, object value)
where T : Component
{