Created
August 14, 2013 20:15
-
-
Save SteveSwink/6235136 to your computer and use it in GitHub Desktop.
Screenshot grabber for unity
This file contains hidden or 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 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