Skip to content

Instantly share code, notes, and snippets.

@huihut
Created March 23, 2019 14:21
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 huihut/d6815f494298ae8de19fbc31fe137f54 to your computer and use it in GitHub Desktop.
Save huihut/d6815f494298ae8de19fbc31fe137f54 to your computer and use it in GitHub Desktop.
Unity TakeScreenshot
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Screenshot : MonoBehaviour
{
private int index = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void OnScreenshotButton()
{
StartCoroutine(CaptureScreen());
}
private IEnumerator CaptureScreen()
{
// Wait till the last possible moment before screen rendering to hide the UI
yield return null;
GameObject.Find("Canvas").GetComponent<Canvas>().enabled = false;
// Wait for screen rendering to complete
yield return new WaitForEndOfFrame();
index++;
// Take screenshot
ScreenCapture.CaptureScreenshot(@"screenshot" + index.ToString("D3") + ".png");
// Show UI after we're done
GameObject.Find("Canvas").GetComponent<Canvas>().enabled = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment