- Home /
Screenshot to variable
When I call the 'TakeScreenshot' function i want it to take a screenshot and place it into a variable. I was looking at Application.CaptureScreenshot ()
but could not get it to work.
Any help would be much appreciated
var ScreenShot : Texture; //Screenshot here
function TakeScreenshot ()
{
//Screenshot Code here
}
Answer by tanoshimi · May 21, 2016 at 07:05 AM
CaptureScreenshot() is for saving to a file. For writing pixels from the screen to a texture, use ReadPixels:
void TakeScreenshot() {
ScreenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
ScreenShot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
ScreenShot.Apply();
}
(This is for C# - you may need to modify it if you're using Unityscript).
I've edited my code - it's a Texture2D, as you had in your original code.
It has seemed to work, thanks. I was wondering where the image is saved? I need it to be able to get access the file when the game has been quit and reloaded. Would i use PlayerPrefs for something like this?
The image isn't saved anywhere - it's assigned to the Screenshot texture variable held in memory (which is what you said you wanted in your question.....). To save a screenshot to a file, use Application.CaptureScreenshot, or you could serialize the Screenshot variable in the code above to a binary file. I wouldn't use PlayerPrefs for image data - it's designed more for recording simple settings, text strings etc.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081232im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
scale textures using scrollbars that show/enable when texture is clicked on 0 Answers
Application.CaptureScreenshot on a Texture2D? 1 Answer
GUI Texture fixed on Object 1 Answer
Creating new variables through incrementing 0 Answers
How do I change the text of a GUIText object through another GameObject using a variable? 2 Answers