CameraCopyer.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityStandardAssets.ImageEffects;
  5. /* 复制器-对相机上挂在的组件进行批量复制和粘贴 */
  6. public class CameraCopyer : MonoBehaviour
  7. {
  8. [SerializeField] GameObject src;
  9. [SerializeField] GameObject dest1;
  10. [SerializeField] GameObject dest2;
  11. void Awake() {
  12. Component[] components = {
  13. src.GetComponent<FlareLayer>(),
  14. src.GetComponent<VignetteAndChromaticAberration>(),
  15. src.GetComponent<Bloom>(),
  16. src.GetComponent<ScreenSpaceAmbientObscurance>(),
  17. src.GetComponent<GlobalFog>()
  18. };
  19. foreach (Component item in components)
  20. {
  21. // UnityEditorInternal.ComponentUtility.CopyComponent(item);
  22. // UnityEditorInternal.ComponentUtility.PasteComponentAsNew(dest1);
  23. // UnityEditorInternal.ComponentUtility.PasteComponentAsNew(dest2);
  24. CopyComponent(item, dest1);
  25. CopyComponent(item, dest2);
  26. }
  27. Color skyBoxBgColor = src.GetComponent<Camera>().backgroundColor;
  28. dest1.GetComponent<Camera>().backgroundColor = skyBoxBgColor;
  29. dest2.GetComponent<Camera>().backgroundColor = skyBoxBgColor;
  30. }
  31. void CopyComponent(Component comp, GameObject dest)
  32. {
  33. System.Type type = comp.GetType();
  34. Component copy = dest.AddComponent(type);
  35. System.Reflection.FieldInfo[] fields = type.GetFields();
  36. foreach (System.Reflection.FieldInfo field in fields)
  37. {
  38. field.SetValue(copy, field.GetValue(comp));
  39. }
  40. }
  41. }