I often find myself needing to grab a screenshot or two from my Unity applications, and often ending up using PrintScreen or the (admittedly very handy!) Snipping Tool.

To avoid this slightly awkward manual process I’ve put together a very simple Screenshot component.

You can define which key takes the screenshot (default F12) and a setting to allow super resolution shots to be taken (default 100%, up to 400%)

(The latest version can also be bound to the Unity Input manager via the inputButton field if required.)

I’ve tested 400% super resolution with my plucky old 1080p panel and it creates an 8k image without issue – 400% at 4k native will produce a 16k image which I guess might work – if you are brave enough to try this, please let me know!

Note: Unity MonoBehaviours should always be in a file where the name matches the class name – put this code in a file named Screenshot.cs

Without further ado, here’s the code!

Screenshot Component // Written by Chris Bellini and published as public domain to http://untitledgam.es // You may freely use, license or sell any portion of this code for any legal purpose // All code is presented as is and without warranty or liability and is used at your own risk using System; using System.IO; using UnityEngine; public class Screenshot : MonoBehaviour { private string folder; [SerializeField] private string inputButton = "Screenshot"; private bool inputButtonIsMapped; [SerializeField] private KeyCode key = KeyCode.F12; [SerializeField] [Range(1, 4)] private int superSize = 1; public KeyCode Key { get { return this.key; } set { this.key = value; } } public int SuperSize { get { return this.superSize; } set { this.superSize = Mathf.Clamp(value, 1, 4); } } private static string GetFilename() { return string.Format("{0}.png", DateTime.Now.ToBinary()); } public void Capture() { string filepath = Path.Combine(this.folder, Screenshot.GetFilename()); Application.CaptureScreenshot(filepath, this.SuperSize); Debug.Log(string.Format("[<color=blue>Screenshot</color>] Screenshot captured.

<color=grey>{0}</color>", filepath)); } private void Awake() { this.folder = Application.dataPath; this.folder = this.folder.Substring(0, this.folder.Length - 7); this.folder = Path.Combine(this.folder, "Screenshots"); if (!Directory.Exists(this.folder)) { Directory.CreateDirectory(this.folder); } this.inputButtonIsMapped = this.IsInputButtonMapped(); } private bool IsInputButtonMapped() { if (string.IsNullOrEmpty(this.inputButton)) { return false; } try { Input.GetButton(this.inputButton); return true; } catch { return false; } } private void Update() { if (Input.GetKeyDown(this.Key) || (this.inputButtonIsMapped && Input.GetButton(this.inputButton))) { this.Capture(); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 // Written by Chris Bellini and published as public domain to http://untitledgam.es // You may freely use, license or sell any portion of this code for any legal purpose // All code is presented as is and without warranty or liability and is used at your own risk using System ; using System . IO ; using UnityEngine ; public class Screenshot : MonoBehaviour { private string folder ; [ SerializeField ] private string inputButton = "Screenshot" ; private bool inputButtonIsMapped ; [ SerializeField ] private KeyCode key = KeyCode . F12 ; [ SerializeField ] [ Range ( 1 , 4 ) ] private int superSize = 1 ; public KeyCode Key { get { return this . key ; } set { this . key = value ; } } public int SuperSize { get { return this . superSize ; } set { this . superSize = Mathf . Clamp ( value , 1 , 4 ) ; } } private static string GetFilename ( ) { return string . Format ( "{0}.png" , DateTime . Now . ToBinary ( ) ) ; } public void Capture ( ) { string filepath = Path . Combine ( this . folder , Screenshot . GetFilename ( ) ) ; Application . CaptureScreenshot ( filepath , this . SuperSize ) ; Debug . Log ( string . Format ( "[<color=blue>Screenshot</color>] Screenshot captured.

<color=grey>{0}</color>" , filepath ) ) ; } private void Awake ( ) { this . folder = Application . dataPath ; this . folder = this . folder . Substring ( 0 , this . folder . Length - 7 ) ; this . folder = Path . Combine ( this . folder , "Screenshots" ) ; if ( ! Directory . Exists ( this . folder ) ) { Directory . CreateDirectory ( this . folder ) ; } this . inputButtonIsMapped = this . IsInputButtonMapped ( ) ; } private bool IsInputButtonMapped ( ) { if ( string . IsNullOrEmpty ( this . inputButton ) ) { return false ; } try { Input . GetButton ( this . inputButton ) ; return true ; } catch { return false ; } } private void Update ( ) { if ( Input . GetKeyDown ( this . Key ) || ( this . inputButtonIsMapped && Input . GetButton ( this . inputButton ) ) ) { this . Capture ( ) ; } } }

The shots are saved as PNG files into a Screenshots folder in the root of your project (up one from the Assets folder to prevent them being automatically included in your project!)

The names are a long number based on the current time – it’s not the prettiest naming system, but it means the shots are in the correct order by default when viewing the folder.

Just add the Screenshot component to any GameObject (I tend to make a new game object for it, just to keep it easy to find) and press the selected key when running the game, either in the editor or in the build.

It’s saved me a bit of time recently, so I thought I’d share it with you! 🙂