Skip to content

Instantly share code, notes, and snippets.

@savage69kr
Forked from stevepdp/ScreenShotter.cs
Created April 17, 2023 06:16
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 savage69kr/31337b38ba9ab4ba736df7d2f6f4fe03 to your computer and use it in GitHub Desktop.
Save savage69kr/31337b38ba9ab4ba736df7d2f6f4fe03 to your computer and use it in GitHub Desktop.
A screenshot taking tool for Unity. Drop this onto a "ScreenShotter" or similarily named empty object in your scene and configure as desired. The defaults are however pretty good with screenshots being dumped to the top of your project folder every three seconds. Don't forget to exclude this directory in your .gitignore!
using UnityEngine;
using System.IO;
public class ScreenShotter : MonoBehaviour
{
[Header("Filename Settings")]
[SerializeField, Tooltip("Set the destination path. This folder will be placed at the top of your project directory.")]
string fileDestination = "Screenshots/";
[SerializeField, Tooltip("Set the start of the file name.")]
string filePrefix = "screenshot_";
[SerializeField, Tooltip("Set date/time format")]
string fileDateFormat = "yyyy-MM-dd_HH-mm-ss";
[SerializeField, Tooltip("Set the file extension. Note that it'll be a PNG regardless however.")]
string fileExt = ".png";
[Header("Quality Settings")]
[SerializeField, Range(1, 4), Tooltip("1x is standard. 4 is 4x the standard resolution.")]
int scaleFactor = 1;
[Header("Other Settings")]
[SerializeField, Tooltip("The number of seconds between each capture.")]
float captureInterval = 3f;
float timeSinceCapture;
void Start()
{
CreateScreenshotsDir();
}
void Update()
{
TakeScreenshot();
}
void CreateScreenshotsDir()
{
if (!Directory.Exists(fileDestination))
Directory.CreateDirectory(fileDestination);
}
void TakeScreenshot()
{
timeSinceCapture += Time.deltaTime;
if (timeSinceCapture >= captureInterval)
{
timeSinceCapture -= captureInterval;
string fileName = filePrefix + System.DateTime.Now.ToString(fileDateFormat) + fileExt;
string filePath = Path.Combine(fileDestination, fileName);
ScreenCapture.CaptureScreenshot(filePath, scaleFactor);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment