2023-02-12 22:21:03 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class Screenshot : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public bool takeScreenshot;
|
|
|
|
|
public string filename = "unity_screenshot.png";
|
|
|
|
|
|
2023-02-14 20:04:51 +01:00
|
|
|
|
public RenderTexture textureReference;
|
|
|
|
|
|
2023-02-12 22:21:03 +01:00
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
if (takeScreenshot)
|
|
|
|
|
{
|
|
|
|
|
Camera cam = GetComponent<Camera>();
|
|
|
|
|
|
2023-02-14 20:04:51 +01:00
|
|
|
|
cam.targetTexture = textureReference;
|
|
|
|
|
|
2023-02-12 22:21:03 +01:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|