cvr-props/Assets/editor_garbage/Screenshot.cs
2023-06-10 18:58:53 +02:00

36 lines
817 B
C#

using System.IO;
using UnityEngine;
public class Screenshot : MonoBehaviour
{
public bool takeScreenshot;
public string filename = "unity_screenshot.png";
public RenderTexture textureReference;
void Start()
{
if (takeScreenshot)
{
Camera cam = GetComponent<Camera>();
cam.targetTexture = textureReference;
RenderTexture currentRT = RenderTexture.active;
RenderTexture.active = cam.targetTexture;
cam.Render();
Texture2D Image = new Texture2D(cam.targetTexture.width, cam.targetTexture.height);
Image.ReadPixels(new Rect(0, 0, cam.targetTexture.width, cam.targetTexture.height), 0, 0);
Image.Apply();
RenderTexture.active = currentRT;
var Bytes = Image.EncodeToPNG();
Destroy(Image);
File.WriteAllBytes("/home/crispypin/pictures/" + filename, Bytes);
}
}
}