Skip to content

Instantly share code, notes, and snippets.

@adrianseeley
Created June 27, 2020 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianseeley/c1cc3f9b185d6a9dad6fdb53fcdcaf78 to your computer and use it in GitHub Desktop.
Save adrianseeley/c1cc3f9b185d6a9dad6fdb53fcdcaf78 to your computer and use it in GitHub Desktop.
UnityScreenshot.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class UnityRoboTester : MonoBehaviour
{
public Camera Camera;
private RenderTexture RenderTexture;
private Texture2D Image;
private Rect Rect;
public void Start()
{
}
bool test = false;
bool test2 = false;
public void Update()
{
if (Time.time > 3 && !test)
{
test = true;
StartCoroutine(Snapshot("DEMO"));
}
if (Time.time > 5 && !test2)
{
test2 = true;
StartCoroutine(Snapshot("DEMO2"));
}
}
public IEnumerator Snapshot(string Label)
{
yield return new WaitForEndOfFrame();
Debug.Log("Snapshot");
if (RenderTexture == null)
{
RenderTexture = new RenderTexture(Camera.scaledPixelWidth, Camera.scaledPixelHeight, 24);
Image = new Texture2D(RenderTexture.width, RenderTexture.height, TextureFormat.RGB24, false);
Rect = new Rect(0, 0, RenderTexture.width, RenderTexture.height);
}
Camera.targetTexture = RenderTexture;
Camera.Render();
Camera.targetTexture = null;
Image.ReadPixels(Rect, 0, 0);
Image.Apply();
byte[] pngBytes = Image.EncodeToPNG();
System.IO.File.WriteAllBytes("./" + Label + ".png", pngBytes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment