Skip to content

Instantly share code, notes, and snippets.

@mattatz

mattatz/ComponentUtil.cs

Last active Jun 10, 2020
Embed
What would you like to do?
CopyComponent by Script for Unity
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
namespace Utils
{
// http://answers.unity3d.com/questions/458207/copy-a-component-at-runtime.html
// http://stackoverflow.com/questions/10261824/how-can-i-get-all-constants-of-a-type-by-reflection
public class ComponentUtil
{
public static T CopyComponent<T>(T original, GameObject destination) where T : Component
{
System.Type type = original.GetType();
var dst = destination.GetComponent(type) as T;
if (!dst) dst = destination.AddComponent(type) as T;
var fields = GetAllFields(type);
foreach (var field in fields)
{
if (field.IsStatic) continue;
field.SetValue(dst, field.GetValue(original));
}
var props = type.GetProperties();
foreach (var prop in props)
{
if (!prop.CanWrite || !prop.CanWrite || prop.Name == "name") continue;
prop.SetValue(dst, prop.GetValue(original, null), null);
}
return dst as T;
}
public static IEnumerable<FieldInfo> GetAllFields(System.Type t)
{
if (t == null)
{
return Enumerable.Empty<FieldInfo>();
}
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Static | BindingFlags.Instance |
BindingFlags.DeclaredOnly;
return t.GetFields(flags).Concat(GetAllFields(t.BaseType));
}
}
}
@C0DEF52

This comment has been minimized.

Copy link

@C0DEF52 C0DEF52 commented Jan 11, 2020

I guess !prop.CanWrite || !prop.CanWrite is typo and second check should be !prop.CanRead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.