-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyComponent.cs
More file actions
34 lines (26 loc) · 1.01 KB
/
CopyComponent.cs
File metadata and controls
34 lines (26 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using UnityEngine;
using System;
using System.Reflection;
public static class CopyComponent
{
public static T GetCopyOf<T>(this Component component, T componentToCopy) where T : Component
{
Type type = component.GetType();
if (type != componentToCopy.GetType()) return null;
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default | BindingFlags.DeclaredOnly;
PropertyInfo[] propertyInformation = type.GetProperties(flags);
foreach (var information in propertyInformation)
{
if (information.CanWrite)
{
information.SetValue(component, information.GetValue(componentToCopy, null), null);
}
}
FieldInfo[] fieldInformation = type.GetFields(flags);
foreach (var information in fieldInformation)
{
information.SetValue(component, information.GetValue(componentToCopy));
}
return component as T;
}
}