SFX_ComponentUtil.cs 942 B

123456789101112131415161718192021222324252627282930
  1. using UnityEngine;
  2. // ReSharper disable once CheckNamespace
  3. namespace QFX.SFX
  4. {
  5. public static class SFX_ComponentUtil
  6. {
  7. public static T CopyComponent<T>(T original, GameObject destination) where T : Component
  8. {
  9. var type = original.GetType();
  10. var dst = destination.GetComponent(type) as T;
  11. if (!dst) dst = destination.AddComponent(type) as T;
  12. var fields = type.GetFields();
  13. foreach (var field in fields)
  14. {
  15. if (field.IsStatic) continue;
  16. field.SetValue(dst, field.GetValue(original));
  17. }
  18. var props = type.GetProperties();
  19. foreach (var prop in props)
  20. {
  21. if (!prop.CanWrite || !prop.CanWrite || prop.Name == "name") continue;
  22. prop.SetValue(dst, prop.GetValue(original, null), null);
  23. }
  24. return dst;
  25. }
  26. }
  27. }