Capture a screenshot from a camera component and use it as a texture in your scene, or save it as a PNG or JPEG!
By SamFadi
Example of use :
// Display the rendered texture to UI
<root>
<Image Texture=@RenderTexture style="width: 128; height: 128" />
</root>
@code
{
public CameraComponent Camera { get; set; }
Texture RenderTexture { get; set; }
public override void Tick()
{
if ( Camera is null ) return;
// Take a screenshot of size 2048x2048 (static method)
RenderTexture = BasicScreenshot.TakeScreenshot(cameraComponent, 2048, 2048);
}
}
Methods for Texture format :
static public Texture TakeScreenshot(CameraComponent cameraComponent, Vector2 size, ImageFormat format = ImageFormat.RGBA8888, bool log = false)
static public Texture TakeScreenshot(CameraComponent cameraComponent, int width, int height, ImageFormat format = ImageFormat.RGBA8888, bool log = false)
static public Texture TakeScreenshot(GameObject camera, int width, int height, ImageFormat format = ImageFormat.RGBA8888, bool log = false)
static public Texture TakeScreenshot(GameObject camera, Vector2 size, ImageFormat format = ImageFormat.RGBA8888, bool log = false)
Methods for PNG & JPEG format
static public void TakeScreenshotAndSaveAsPng(CameraComponent cameraComponent, string path, Vector2 size, ImageFormat format = ImageFormat.RGBA8888, int mip = 0, bool log = true)
static public void TakeScreenshotAndSaveAsJpeg(CameraComponent cameraComponent, string path, Vector2 size, ImageFormat format = ImageFormat.RGBA8888, int mip = 0, bool log = true)
Texture to image
static public void TextureToJpeg(Texture texture, string path, int mip = 0, bool log = true)
static public void TextureToPng(Texture texture, string path, int mip = 0, bool log = true)