Browse Source

Copy over camera effects in screenshot

Bepis 7 years ago
parent
commit
7ba7f0fba0
1 changed files with 30 additions and 0 deletions
  1. 30 0
      Plugins/Screencap/ScreenshotManager.cs

+ 30 - 0
Plugins/Screencap/ScreenshotManager.cs

@@ -62,6 +62,7 @@ namespace Screencap
             var go = new GameObject();
             Camera renderCam = go.AddComponent<Camera>();
             renderCam.CopyFrom(Camera.main);
+            CopyComponents(Camera.main.gameObject, renderCam.gameObject);
 
             renderCam.targetTexture = new RenderTexture(2048, 2048, 32); //((int)cam.pixelRect.width, (int)cam.pixelRect.height, 32);
             renderCam.aspect = renderCam.targetTexture.width / (float)renderCam.targetTexture.height;
@@ -77,5 +78,34 @@ namespace Screencap
             Destroy(renderCam);
             return image;
         }
+
+        void CopyComponents(GameObject original, GameObject target)
+        {
+            foreach (Component component in original.GetComponents<Component>())
+            {
+                var newComponent = CopyComponent(component, target);
+
+                if (component is MonoBehaviour)
+                {
+                    var behavior = (MonoBehaviour)component;
+
+                    (newComponent as MonoBehaviour).enabled = behavior.enabled;
+                }
+            }
+        }
+
+        //https://answers.unity.com/questions/458207/copy-a-component-at-runtime.html
+        Component CopyComponent(Component original, GameObject destination)
+        {
+            System.Type type = original.GetType();
+            Component copy = destination.AddComponent(type);
+            // Copied fields can be restricted with BindingFlags
+            System.Reflection.FieldInfo[] fields = type.GetFields();
+            foreach (System.Reflection.FieldInfo field in fields)
+            {
+                field.SetValue(copy, field.GetValue(original));
+            }
+            return copy;
+        }
     }
 }