Created
August 10, 2016 04:42
-
-
Save teraphlaxis/11c0bc5d8fdb2d0c04a993f9b716a5f3 to your computer and use it in GitHub Desktop.
Screenshot Helper Class for Unity Allowing Editor and Runtime 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 UnityEngine; | |
using System; | |
using System.Collections; | |
// >>================================================== | |
// Screenshot Editor Class | |
// >>================================================== | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
public static class ScreenshotHelperFunctions | |
{ | |
static DateTime m_LastTime = new DateTime(0); | |
static int m_LastNumber = 0; | |
// Create Our | |
// >>================================================== | |
[RuntimeInitializeOnLoadMethod] | |
static void RuntimeInitialise() | |
{ | |
if (ScreenshotHelper.Instance == null) | |
{ | |
GameObject t_Obj = new GameObject("Screenshot Helper"); | |
t_Obj.AddComponent<ScreenshotHelper>(); | |
} | |
} | |
// >>================================================== | |
public static string GetNextFilePath() | |
{ | |
if (m_LastTime.Approximately(DateTime.Now)) { m_LastNumber++; } else { m_LastTime = DateTime.Now; m_LastNumber = 0; } | |
string t_Filename = Application.productName + " - " + m_LastTime.ToString("yyyy-MM-dd HH-mm-ss"); | |
if (m_LastNumber != 0) { t_Filename = t_Filename + " (" + m_LastNumber + ")"; } | |
return Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + t_Filename + ".png"; | |
} | |
// DateTime Extenstion for Comparison down to Seconds | |
// >>================================================== | |
static bool Approximately(this DateTime a_Self, DateTime a_CompareTo) | |
{ | |
if (a_Self.Year != a_CompareTo.Year) { return false; } | |
if (a_Self.Month != a_CompareTo.Month) { return false; } | |
if (a_Self.Day != a_CompareTo.Day) { return false; } | |
if (a_Self.Hour != a_CompareTo.Hour) { return false; } | |
if (a_Self.Minute != a_CompareTo.Minute) { return false; } | |
if (a_Self.Second != a_CompareTo.Second) { return false; } | |
return true; | |
} | |
#if UNITY_EDITOR | |
// Menu Item and Shortcut for In Editor Screenshots, outside of Play Mode | |
// >>================================================== | |
[MenuItem("Tools/Save Screenshot &P")] | |
public static void TakeScreenshot() | |
{ | |
string t_FilePath = GetNextFilePath(); | |
Debug.Log("Screenshot Saved to \"" + t_FilePath + "\""); | |
Application.CaptureScreenshot(t_FilePath); | |
} | |
#endif | |
} | |
// >>================================================== | |
// Screenshot Runtime Class | |
// >>================================================== | |
public class ScreenshotHelper : MonoBehaviour | |
{ | |
private static ScreenshotHelper _Instance = null; | |
public static ScreenshotHelper Instance { get { return _Instance; } } | |
// >>================================================== | |
void Awake() | |
{ | |
if (_Instance == null) { _Instance = this; DontDestroyOnLoad(gameObject); } else { DestroyImmediate(this); } | |
} | |
// >>================================================== | |
void OnDestroy() | |
{ | |
if (_Instance == this) { _Instance = null; } | |
} | |
// >>================================================== | |
void OnGUI() | |
{ | |
if (Event.current.type == EventType.keyUp && Event.current.keyCode == KeyCode.SysReq) { SaveScreenshot(); } | |
} | |
// >>================================================== | |
public void SaveScreenshot(bool a_CompressImage = false) { StartCoroutine(SaveScreenshotToFile(a_CompressImage)); } | |
// >>================================================== | |
private static IEnumerator SaveScreenshotToFile(bool a_CompressImage) | |
{ | |
yield return new WaitForEndOfFrame(); | |
// Prepare Screenshot Image | |
Texture2D screenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); | |
screenShot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); | |
screenShot.Apply(); | |
if (a_CompressImage) { screenShot.Compress(true); } | |
byte[] bytes = screenShot.EncodeToPNG(); | |
// Save Image | |
string t_FilePath = ScreenshotHelperFunctions.GetNextFilePath(); | |
System.IO.File.WriteAllBytes(t_FilePath, bytes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment