using UnityEngine; using System.Collections; using System.IO; #if UNITY_EDITOR using UnityEditor; using UnityEngine.UI; #endif namespace Gaia { /// /// A simple screen shot taker - with thanks to @jamiepollard on the Gaia forum /// Adapted from the original script here: /// http://answers.unity3d.com/questions/22954/how-to-save-a-picture-take-screenshot-from-a-camer.html /// [GaiaScriptOrder(500)] public class ScreenShotter : MonoBehaviour { /// /// Key the screenshotter is bound to /// public KeyCode m_screenShotKey = KeyCode.F12; /// /// The storage format, JPG is smaller, PNG is higher quality /// public Gaia.GaiaConstants.StorageFormat m_imageFormat = GaiaConstants.StorageFormat.JPG; /// /// The target directory for the screenshots i.e. /Assets/targetdir /// public string m_targetDirectory = "Gaia User Data/Screenshots"; /// /// Target resolution used to quickly change both width and height /// public GaiaConstants.ScreenshotResolution m_screenshotResolution = GaiaConstants.ScreenshotResolution.Resolution1920X1080; /// /// Target screenshot width /// public int m_targetWidth = 1920; /// /// Target screenshot height /// public int m_targetHeight = 1080; /// /// If set the actual screen dimensions will be used instead of target dimensions /// public bool m_useScreenSize = true; /// /// The screen shot camera /// public Camera m_mainCamera; /// /// A toggle to cause the next updatre to take a shot /// private bool m_takeShot = false; /// /// A toggle to cause an asset db refresh /// private bool m_refreshAssetDB = false; /// /// Texture used for the watermark /// public Texture2D m_watermark; /// /// Sets up the camera if not already done /// private void OnEnable() { if (m_mainCamera == null) { m_mainCamera = GaiaUtils.GetCamera(true); } //Create the target directory string path = Path.Combine(Application.dataPath, m_targetDirectory); if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); #if UNITY_EDITOR AssetDatabase.Refresh(); #endif } } /// /// Get unity to refresh the files list /// private void OnDisable() { //Refresh the asset database if (m_refreshAssetDB) { m_refreshAssetDB = false; #if UNITY_EDITOR AssetDatabase.Refresh(); #endif } } /// /// On Start find camera /// private void Start() { m_mainCamera = GaiaUtils.GetCamera(true); } /// /// Assigns a name to the screen shot - by default puts in in the assets directory /// /// Width of screenshot /// Height of screenshot /// Screen shot name and full path private string ScreenShotName(int width, int height) { string path = Path.Combine(Application.dataPath, m_targetDirectory); path = path.Replace('\\', '/'); if (path[path.Length-1] == '/') { path = path.Substring(0, path.Length - 1); } if (m_imageFormat == GaiaConstants.StorageFormat.JPG) { return string.Format("{0}/Grab {1:yyyyMMddHHmmss} w{2}h{3} x{4}y{5}z{6}r{7}.jpg", path, System.DateTime.Now, width, height, (int)m_mainCamera.transform.position.x, (int)m_mainCamera.transform.position.y, (int)m_mainCamera.transform.position.z, (int)m_mainCamera.transform.rotation.eulerAngles.y ); } else { return string.Format("{0}/Grab {1:yyyyMMdd HHmmss} w{2}h{3} x{4}y{5}z{6}r{7}.png", path, System.DateTime.Now, width, height, (int)m_mainCamera.transform.position.x, (int)m_mainCamera.transform.position.y, (int)m_mainCamera.transform.position.z, (int)m_mainCamera.transform.rotation.eulerAngles.y ); } } /// /// Call this to take a screen shot in next late update /// public void TakeHiResShot() { m_takeShot = true; } /// /// Takes the actual screen shot when the key is pressed or takeshot is true /// private void LateUpdate() { if (Input.GetKeyDown(m_screenShotKey) || m_takeShot) { if (m_mainCamera == null) { m_mainCamera = GaiaUtils.GetCamera(true); } //Pick up and use the actual screen dimensions if (m_useScreenSize) { m_targetWidth = Screen.width; m_targetHeight = Screen.height; } m_refreshAssetDB = true; RenderTexture rt = new RenderTexture(m_targetWidth, m_targetHeight, 24); m_mainCamera.targetTexture = rt; Texture2D screenShot = new Texture2D(m_targetWidth, m_targetHeight, TextureFormat.RGB24, false); m_mainCamera.Render(); RenderTexture.active = rt; screenShot.ReadPixels(new Rect(0, 0, m_targetWidth, m_targetHeight), 0, 0); m_mainCamera.targetTexture = null; RenderTexture.active = null; // JC: added to avoid errors Destroy(rt); if (m_watermark != null) { Gaia.GaiaUtils.MakeTextureReadable(m_watermark); screenShot = AddWatermark(screenShot, m_watermark); } byte[] bytes = null; if (m_imageFormat == GaiaConstants.StorageFormat.JPG) { bytes = screenShot.EncodeToJPG(); } else { bytes = screenShot.EncodeToPNG(); } string filename = ScreenShotName(m_targetWidth, m_targetHeight); PWCommon3.Utils.WriteAllBytes(filename, bytes); m_takeShot = false; Debug.Log(string.Format("Took screenshot to: {0}", filename)); } } /// /// Adds watermark the screenshot /// /// /// /// public Texture2D AddWatermark(Texture2D background, Texture2D watermark) { int startX = background.width - watermark.width - 10; int endX = startX + watermark.width; //int startY = background.height - watermark.height - 20; int startY = 8; int endY = startY + watermark.height; for (int x = startX; x < endX; x++) { for (int y = startY; y < endY; y++) { Color bgColor = background.GetPixel(x, y); Color wmColor = watermark.GetPixel(x - startX, y - startY); Color final_color = Color.Lerp(bgColor, wmColor, wmColor.a / 1.0f); background.SetPixel(x, y, final_color); } } background.Apply(); return background; } public void UpdateScreenshotResolution(GaiaConstants.ScreenshotResolution screenshotResolution) { switch (screenshotResolution) { case GaiaConstants.ScreenshotResolution.Resolution640X480: m_targetWidth = 640; m_targetHeight = 480; break; case GaiaConstants.ScreenshotResolution.Resolution800X600: m_targetWidth = 800; m_targetHeight = 600; break; case GaiaConstants.ScreenshotResolution.Resolution1280X720: m_targetWidth = 1280; m_targetHeight = 720; break; case GaiaConstants.ScreenshotResolution.Resolution1366X768: m_targetWidth = 1366; m_targetHeight = 768; break; case GaiaConstants.ScreenshotResolution.Resolution1600X900: m_targetWidth = 1600; m_targetHeight = 900; break; case GaiaConstants.ScreenshotResolution.Resolution1920X1080: m_targetWidth = 1920; m_targetHeight = 1080; break; case GaiaConstants.ScreenshotResolution.Resolution2560X1440: m_targetWidth = 2560; m_targetHeight = 1440; break; case GaiaConstants.ScreenshotResolution.Resolution3840X2160: m_targetWidth = 3840; m_targetHeight = 2160; break; case GaiaConstants.ScreenshotResolution.Resolution7680X4320: m_targetWidth = 7680; m_targetHeight = 4320; break; } } } }