123456789101112131415161718192021222324252627282930 |
- using UnityEngine;
- // ReSharper disable once CheckNamespace
- namespace QFX.SFX
- {
- public static class SFX_ComponentUtil
- {
- public static T CopyComponent<T>(T original, GameObject destination) where T : Component
- {
- var type = original.GetType();
- var dst = destination.GetComponent(type) as T;
- if (!dst) dst = destination.AddComponent(type) as T;
- var fields = type.GetFields();
- 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;
- }
- }
- }
|