Skip to content

Commit b22008c

Browse files
author
Malte
committed
Will also copy base class properties by default
1 parent ddbe869 commit b22008c

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

Assets/Scripts/RuntimePresets/ComponentExtensions.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,30 @@ public static class ComponentExtensions
1515
{"mesh", "sharedMesh"}
1616
};
1717

18-
public static T TransferValuesFrom<T>(this Component comp, T other, bool? considerBaseClasses = null) where T : Component
18+
public static T TransferValuesFrom<T>(this Component comp, T other, bool considerBaseClasses = true) where T : Component
1919
{
20-
var conditionalFlags = (considerBaseClasses ?? false) ? BindingFlags.FlattenHierarchy : BindingFlags.DeclaredOnly;
21-
Type type = comp.GetType(); //type of the copy
22-
if (type != other.GetType()) return null; // type mis-match
20+
var conditionalFlags = considerBaseClasses ? BindingFlags.FlattenHierarchy : BindingFlags.DeclaredOnly;
21+
22+
// Type of the copy
23+
Type type = comp.GetType();
24+
25+
// Type mismatch
26+
if (type != other.GetType())
27+
{
28+
return null;
29+
}
30+
2331
BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Default | conditionalFlags;
24-
PropertyInfo[] pinfos = type.GetProperties(flags);
32+
PropertyInfo[] propertyInfos = type.GetProperties(flags);
2533

2634
//Handle variables
27-
foreach (var pinfo in pinfos)
35+
foreach (var pinfo in propertyInfos)
2836
{
2937
if (pinfo.CanWrite)
3038
{
3139
try
3240
{
33-
//Ignore obsolete variables to avoid editor warnings
41+
// Ignore obsolete variables to avoid editor warnings
3442
if (HasAnnotation<ObsoleteAttribute>(pinfo) || HasAnnotation<NotSupportedException>(pinfo) || HasAnnotation<System.ComponentModel.EditorBrowsableAttribute>(pinfo))
3543
{
3644
continue;
@@ -49,7 +57,7 @@ public static T TransferValuesFrom<T>(this Component comp, T other, bool? consid
4957
}
5058
}
5159

52-
//Handle properties
60+
// Handle properties
5361
FieldInfo[] finfos = type.GetFields(flags);
5462

5563
foreach (var finfo in finfos)

0 commit comments

Comments
 (0)