Skip to content

Instantly share code, notes, and snippets.

@SteveSwink
Created August 14, 2013 20:15
Show Gist options
  • Save SteveSwink/6235136 to your computer and use it in GitHub Desktop.
Save SteveSwink/6235136 to your computer and use it in GitHub Desktop.
Screenshot grabber for unity
using UnityEngine;
using System.Collections;
using System.IO;
public class CaptureScreenshot : MonoBehaviour {
public int sizeMultiplier = 2;
public string prefix = "Scale_Screen";
int incrementValue = 0;
string dateAndTime = "DEFAULT";
string screenshotName;
string screenshotPath;
public KeyCode hotkey = KeyCode.P;
void Start(){
GeneratePathAndFileName();
}
void GeneratePathAndFileName(){
// dateAndTime = System.DateTime.Now.ToString();
// dateAndTime = System.DateTime.Today.ToShortTimeString();
dateAndTime = System.DateTime.Today.ToString("d");
dateAndTime = dateAndTime.Replace("/", "-");
screenshotName = "/" + prefix + "_" + dateAndTime + "_" + incrementValue;
// screenshotPath = Application.persistentDataPath + "/Screens" + screenshotName + ".png";
screenshotPath = Application.persistentDataPath + screenshotName + ".png";
CheckForExisting();
}
IEnumerator CheckForExisting(){
do
{
Debug.Log ("SCREENSHOT ALREADY EXISTS");
incrementValue++;
GeneratePathAndFileName();
yield return 0;
}while(File.Exists(screenshotPath));
incrementValue++;
GeneratePathAndFileName();
}
IEnumerator PrepScreenshot(){
GeneratePathAndFileName();
while(File.Exists(screenshotPath))
{
Debug.Log ("SCREENSHOT ALREADY EXISTS");
incrementValue++;
GeneratePathAndFileName();
yield return 0;
}
TakeScreenshot();
}
void TakeScreenshot(){
Debug.Log(" ------------------ Taking screenshot!" + screenshotPath + " at " + Time.timeSinceLevelLoad);
Application.CaptureScreenshot(screenshotPath, sizeMultiplier);
}
void Update(){
if(Input.GetKeyDown(hotkey)){
StartCoroutine(PrepScreenshot());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment