- Home /
Take Screen Shot onGUI
Hello Everyone! I have attached the following code below to my camera and am able to take a screenshot by hitting the letter K. But how would I rework it to take a screenshot when the player clicks on a GUI button? Thank you for your input!
K.
var resWidth : int = 4096; var resHeight : int = 2232;
function Update() { if (Input.GetKeyDown ("k")) { var rt = new RenderTexture(resWidth, resHeight, 24);
camera.targetTexture = rt; var screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); camera.Render(); RenderTexture.active = rt; screenShot.ReadPixels(Rect(0, 0, resWidth, resHeight), 0, 0); RenderTexture.active = null; // JC: added to avoid errors camera.targetTexture = null; Destroy(rt); var bytes = screenShot.EncodeToPNG(); System.IO.File.WriteAllBytes(Application.dataPath + "/screenshots/screen" + System.DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss") + ".png", bytes); }
}
Answer by yoyo · Feb 06, 2011 at 06:46 AM
Instead of Update, use OnGUI, and instead of Input.GetKeyDown, use GUI.Button.
For more info, see docs.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103018im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to take a high-resolution screenshot including OnGUI()? 1 Answer
upload a screenshot directly to flickr (or any other photosharing website) 1 Answer
Save to a separate system folder 1 Answer
Screenshot PNG to GUITexture (NONE Texture) 0 Answers
SOLVED - Force camera to fixed aspect ratio and screenshot 1 Answer