Last active
October 14, 2022 10:02
-
-
Save winstxnhdw/d04efef21a8b94fc08ebfee97831c4ac to your computer and use it in GitHub Desktop.
A screenshot manager that evades image overwriting for Unity. Take a screenshot with the PrintScreen key and save it in Documents/<ApplicationName>/Screenshots/<Index>.png. Unfortunately, it only supports up to 18,446,744,073,709,551,615 screenshots.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.InputSystem; | |
// Only supports up to 18,446,744,073,709,551,615 screenshots | |
public class ScreenshotManager : MonoBehaviour { | |
const string DirectoryName = "Screenshots"; | |
DirectoryInfo Directory { get; set; } | |
void Awake() { | |
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); | |
string screenshotPath = Path.Combine(documentsPath, Application.productName, DirectoryName); | |
Directory.CreateDirectory(screenshotPath); | |
this.Directory = new DirectoryInfo(screenshotPath); | |
} | |
void Update() { | |
if (!Keyboard.current[Key.PrintScreen].wasPressedThisFrame) return; | |
this.Screenshot(); | |
} | |
void Screenshot() { | |
ParallelQuery<ulong> validScreenshotNames = | |
this.Directory.GetFiles("*.png") | |
.AsParallel() | |
.Where(screenshot => Path.GetFileNameWithoutExtension(screenshot.Name).All(char.IsDigit)) | |
.Select(screenshot => ulong.Parse(Path.GetFileNameWithoutExtension(screenshot.Name))); | |
string screenshotName = validScreenshotNames.Any() ? $"{validScreenshotNames.Max() + 1}.png" : "1.png"; | |
ScreenCapture.CaptureScreenshot(Path.Combine(this.Directory.FullName, screenshotName)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment