Skip to content

Instantly share code, notes, and snippets.

@mattatz
Last active April 18, 2024 03:04
Show Gist options
  • Save mattatz/1c039dce5ef251dcef7b628c878bea32 to your computer and use it in GitHub Desktop.
Save mattatz/1c039dce5ef251dcef7b628c878bea32 to your computer and use it in GitHub Desktop.
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));
}
}
}
@wp498930275
Copy link

when copy MeshRenderer, original and destination material will instante; this changed original`s property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment