Created
March 25, 2026 23:30
-
-
Save Maxime66410/10e68febd09d19f77839fc07074f73b2 to your computer and use it in GitHub Desktop.
Unity ScreenShot Script
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
| #if UNITY_EDITOR | |
| using UnityEngine; | |
| using System.Collections; | |
| using UnityEditor; | |
| public class Screenshot : MonoBehaviour | |
| { | |
| public int resWidth = 2550; | |
| public int resHeight = 3300; | |
| public Camera camera; | |
| private bool takeHiResShot = false; | |
| public static string ScreenShotName(int width, int height) | |
| { | |
| return string.Format("{0}/screenshots/screen_{1}x{2}_{3}.png", | |
| Application.dataPath, | |
| width, height, | |
| System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")); | |
| } | |
| [MenuItem("ScreenShot/Setup ScreenShot")] | |
| static void CreateFolder() | |
| { | |
| string guid = AssetDatabase.CreateFolder("Assets", "screenshots"); | |
| string newFolderPath = AssetDatabase.GUIDToAssetPath(guid); | |
| } | |
| public void TakeHiResShot() | |
| { | |
| takeHiResShot = true; | |
| } | |
| void LateUpdate() | |
| { | |
| takeHiResShot |= Input.GetKeyDown("k"); | |
| if (takeHiResShot) | |
| { | |
| RenderTexture rt = new RenderTexture(resWidth, resHeight, 24); | |
| camera.targetTexture = rt; | |
| Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); | |
| camera.Render(); | |
| RenderTexture.active = rt; | |
| screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); | |
| camera.targetTexture = null; | |
| RenderTexture.active = null; // JC: added to avoid errors | |
| Destroy(rt); | |
| byte[] bytes = screenShot.EncodeToPNG(); | |
| string filename = ScreenShotName(resWidth, resHeight); | |
| System.IO.File.WriteAllBytes(filename, bytes); | |
| Debug.Log(string.Format("Took screenshot to: {0}", filename)); | |
| takeHiResShot = false; | |
| } | |
| } | |
| [MenuItem("ScreenShot/HD")] | |
| static void HD() | |
| { | |
| Camera Cam = GameObject.Find("Main Camera").GetComponent<Camera>(); | |
| if (Cam != null) | |
| { | |
| RenderTexture rt = new RenderTexture(1280, 720, 24); | |
| Cam.targetTexture = rt; | |
| Texture2D screenShot = new Texture2D(1280, 720, TextureFormat.RGB24, false); | |
| Cam.Render(); | |
| RenderTexture.active = rt; | |
| screenShot.ReadPixels(new Rect(0, 0, 1280, 720), 0, 0); | |
| Cam.targetTexture = null; | |
| RenderTexture.active = null; // JC: added to avoid errors | |
| //Destroy(rt); | |
| byte[] bytes = screenShot.EncodeToPNG(); | |
| string filename = ScreenShotName(1280, 720); | |
| System.IO.File.WriteAllBytes(filename, bytes); | |
| Debug.Log(string.Format("Took screenshot to: {0}", filename)); | |
| } | |
| else | |
| { | |
| Debug.LogWarning("Camera Not Found, Make sure you have a camera called 'Main Camera' in your scene"); | |
| } | |
| } | |
| [MenuItem("ScreenShot/FHD")] | |
| static void ScreenShotFHD() | |
| { | |
| Camera Cam = GameObject.Find("Main Camera").GetComponent<Camera>(); | |
| if (Cam != null) | |
| { | |
| RenderTexture rt = new RenderTexture(1920, 1080, 24); | |
| Cam.targetTexture = rt; | |
| Texture2D screenShot = new Texture2D(1920, 1080, TextureFormat.RGB24, false); | |
| Cam.Render(); | |
| RenderTexture.active = rt; | |
| screenShot.ReadPixels(new Rect(0, 0, 1920, 1080), 0, 0); | |
| Cam.targetTexture = null; | |
| RenderTexture.active = null; // JC: added to avoid errors | |
| //Destroy(rt); | |
| byte[] bytes = screenShot.EncodeToPNG(); | |
| string filename = ScreenShotName(1920, 1080); | |
| System.IO.File.WriteAllBytes(filename, bytes); | |
| Debug.Log(string.Format("Took screenshot to: {0}", filename)); | |
| } | |
| else | |
| { | |
| Debug.LogWarning("Camera Not Found, Make sure you have a camera called 'Main Camera' in your scene"); | |
| } | |
| } | |
| [MenuItem("ScreenShot/4K")] | |
| static void HD4K() | |
| { | |
| Camera Cam = GameObject.Find("Main Camera").GetComponent<Camera>(); | |
| if (Cam != null) | |
| { | |
| RenderTexture rt = new RenderTexture(3840, 2160, 24); | |
| Cam.targetTexture = rt; | |
| Texture2D screenShot = new Texture2D(3840, 2160, TextureFormat.RGB24, false); | |
| Cam.Render(); | |
| RenderTexture.active = rt; | |
| screenShot.ReadPixels(new Rect(0, 0, 3840, 2160), 0, 0); | |
| Cam.targetTexture = null; | |
| RenderTexture.active = null; // JC: added to avoid errors | |
| //Destroy(rt); | |
| byte[] bytes = screenShot.EncodeToPNG(); | |
| string filename = ScreenShotName(3840, 2160); | |
| System.IO.File.WriteAllBytes(filename, bytes); | |
| Debug.Log(string.Format("Took screenshot to: {0}", filename)); | |
| } | |
| else | |
| { | |
| Debug.LogWarning("Camera Not Found, Make sure you have a camera called 'Main Camera' in your scene"); | |
| } | |
| } | |
| [MenuItem("ScreenShot/8K")] | |
| static void UHD8K() | |
| { | |
| Camera Cam = GameObject.Find("Main Camera").GetComponent<Camera>(); | |
| if (Cam != null) | |
| { | |
| RenderTexture rt = new RenderTexture(7680, 4320, 24); | |
| Cam.targetTexture = rt; | |
| Texture2D screenShot = new Texture2D(7680, 4320, TextureFormat.RGB24, false); | |
| Cam.Render(); | |
| RenderTexture.active = rt; | |
| screenShot.ReadPixels(new Rect(0, 0, 7680, 4320), 0, 0); | |
| Cam.targetTexture = null; | |
| RenderTexture.active = null; // JC: added to avoid errors | |
| //Destroy(rt); | |
| byte[] bytes = screenShot.EncodeToPNG(); | |
| string filename = ScreenShotName(7680, 4320); | |
| System.IO.File.WriteAllBytes(filename, bytes); | |
| Debug.Log(string.Format("Took screenshot to: {0}", filename)); | |
| } | |
| else | |
| { | |
| Debug.LogWarning("Camera Not Found, Make sure you have a camera called 'Main Camera' in your scene"); | |
| } | |
| } | |
| [MenuItem("ScreenShot/Furrany Studio")] | |
| static void Links() | |
| { | |
| Application.OpenURL("https://furranystudio.fr/"); | |
| } | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment