// Screenshot Tool // for use in Unity3D // suitable for taking screenshots for iOS/Android app store pages // made by Ryan Miller https://www.ryanmiller.ca using System.Collections; using System.Collections.Generic; using UnityEngine; using System; #if UNITY_EDITOR using System.IO; using UnityEditor; #endif public class ScreenshotTool : MonoBehaviour { [Tooltip("Edit as you please for captured image resolutions")] public Vector2[] captureResolutions = new Vector2[] { new Vector2(1920,1080), // standard display new Vector2(2732,2048), // iPad Pro 12.9" new Vector2(2688,1242), // iPhone 11 6.5" new Vector2(2280,1080) // Google Pixel 4 }; [Tooltip("Leave blank if you attach to camera, or want to use MainCamera tag")] public Camera screenshotCam; [Tooltip("Press this in-editor during runtime to take screenshots")] public KeyCode screenshotShortcut = KeyCode.P; #if UNITY_EDITOR private void Start() { if (screenshotCam == null) { screenshotCam = GetComponent(); } if (screenshotCam == null) { screenshotCam = Camera.main; } } void Update() { if (Input.GetKeyDown(screenshotShortcut)) { TakeScreenshots(); } } public void TakeScreenshots() { foreach (Vector2 vector2 in captureResolutions) { TakeScreenshot((int)vector2.x, (int)vector2.y); } } public void TakeScreenshot(int resWidth, int resHeight) { RenderTexture screenshotTextureTarget = new RenderTexture(resWidth, resHeight, 24); Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); screenshotCam.targetTexture = screenshotTextureTarget; screenshotCam.Render(); RenderTexture.active = screenshotTextureTarget; screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); screenshotCam.targetTexture = null; RenderTexture.active = null; Destroy(screenshotTextureTarget); byte[] bytes = screenShot.EncodeToPNG(); string filename = Time.time.ToString("0000") + "_" + resWidth + "x" + resHeight + ".png"; System.IO.File.WriteAllBytes(Application.persistentDataPath + "\\" + filename, bytes); Debug.Log("Screenshot saved - " + filename); } #endif } #if UNITY_EDITOR [CustomEditor(typeof(ScreenshotTool))] public class ScreenshotToolEditor : Editor { public override void OnInspectorGUI() { base.OnInspectorGUI(); if (GUILayout.Button("Open Folder")) { EditorUtility.RevealInFinder(Application.persistentDataPath); } if (Application.isPlaying == false) { GUI.enabled = false; } if (GUILayout.Button("Take Screenshot")) { FindObjectOfType().TakeScreenshots(); } GUI.enabled = true; } } #endif