Skip to content

Instantly share code, notes, and snippets.

@stevepdp
Last active May 8, 2023 17:41
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stevepdp/f6ce0f158310c7fbb059f8c00b5b3cd6 to your computer and use it in GitHub Desktop.
Save stevepdp/f6ce0f158310c7fbb059f8c00b5b3cd6 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!
#if UNITY_EDITOR
using System.IO;
using UnityEngine;
public class ScreenShotter : MonoBehaviour
{
static ScreenShotter instance;
public static ScreenShotter Instance
{
get
{
if (instance == null)
instance = GameObject.FindObjectOfType<ScreenShotter>();
if (instance == null)
instance = Instantiate(new GameObject("ScreenShotter")).AddComponent<ScreenShotter>();
return instance;
}
}
[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;
[SerializeField] bool isEnabled;
void Awake()
{
EnforceSingleInstance();
}
void Update()
{
CreateScreenshotsDir();
TakeScreenshot();
}
void CreateScreenshotsDir()
{
if (!Directory.Exists(fileDestination) && isEnabled)
Directory.CreateDirectory(fileDestination);
}
void EnforceSingleInstance()
{
if (instance == null)
instance = this;
else if (instance != this)
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
}
void TakeScreenshot()
{
if (isEnabled)
{
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);
}
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment