using UnityEngine; namespace Crosstales.Common.Util { /// Take screen shots inside an application. [DisallowMultipleComponent] public class TakeScreenshot : Singleton { #region Variables ///Prefix for the generate file names. [Tooltip("Prefix for the generate file names.")] public string Prefix = "CT_Screenshot"; ///Factor by which to increase resolution (default: 1). [Tooltip("Factor by which to increase resolution (default: 1).")] public int Scale = 1; ///Key-press to capture the screen (default: F8). [Tooltip("Key-press to capture the screen (default: F8).")] public KeyCode KeyCode = KeyCode.F8; ///Show file location (default: true). [Tooltip("Show file location (default: true).")] public bool ShowFileLocation = true; private Texture2D texture; private bool locationShown; #endregion #if (!UNITY_WSA && !UNITY_WEBGL) || UNITY_EDITOR #region MonoBehaviour methods private void Update() { if (Input.GetKeyDown(KeyCode)) Capture(); } #endregion #region Public methods ///Capture the screen. public void Capture() { string file = $"{Application.persistentDataPath}/{Prefix}{System.DateTime.Now:_dd-MM-yyyy-HH-mm-ss-f}.png"; ScreenCapture.CaptureScreenshot(file, Scale); Debug.Log($"Screenshot saved: {file}"); if (!locationShown && ShowFileLocation) { BaseHelper.ShowFileLocation(file); locationShown = true; } } #endregion #else public void Start() { Debug.LogWarning("'TakeScreenshot' doesn't work with the current platform!"); } #endif } } // © 2014-2020 crosstales LLC (https://www.crosstales.com)