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);
		}
	}

}
