using System.IO; using UnityEngine; namespace Luneraria.View { /// Renders a camera into a PNG (screenshots of tests and tools, also in batch mode). public static class LuneCapture { public static void Render(Camera camera, string file, int width = 1920, int height = 1080) { var target = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32) { antiAliasing = 4 }; RenderTexture previous = camera.targetTexture; float aspect = camera.aspect; camera.aspect = (float)width / height; camera.targetTexture = target; camera.Render(); RenderTexture.active = target; var image = new Texture2D(width, height, TextureFormat.RGB24, false); image.ReadPixels(new Rect(0, 0, width, height), 0, 0); image.Apply(); Directory.CreateDirectory(Path.GetDirectoryName(file)); File.WriteAllBytes(file, image.EncodeToPNG()); RenderTexture.active = null; camera.targetTexture = previous; camera.aspect = aspect; Object.Destroy(target); Object.Destroy(image); } } }