- Home /
How to get a screenshot in game and re-use in game?
For my game, when you die, I want to know how to save the image that the player viewed last and then save it so then you can view it on a newspaper in the game. Basically, I want to know how to keep an image that the player saw and make it so then it can be viewed later on. Almost like those games that let you take pictures with a camera and then view them later.
I would start looking into Application.CaptureScreenshot and the WWW class. This Answer might give you ideas.
Thanks but don't think that that's what I want. What I meant is that I want everything to be in the game. I would want to somehow be able to make a texture from an image in the game and then put it into a material or something. I don't really want to save any images to the players computer from the game.
Answer by robertbu · Feb 24, 2014 at 11:02 PM
You can use Texture2D.ReadPixels() to grab the screen. Here is a bit of demonstration code. To use:
Create a Quad
Scale the Quad so that the aspect ratio is approximately the same as the screen (optional)
Create a material that uses the Unlit/Texture shader with no texture
Attach the material to the Quad
Attach this script to the Quad
Run and use the space bar to get a screen capture:
private var tex2D : Texture2D = null; function Update () { if (Input.GetKeyDown(KeyCode.Space)) CaptureScreen(); } function CaptureScreen() { yield WaitForEndOfFrame(); if (tex2D == null); tex2D = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); tex2D.ReadPixels(Rect(0, 0, Screen.width, Screen.height), 0, 0, false); tex2D.Apply(); renderer.material.mainTexture = tex2D; }
Note there was a question on UA in the last month about ReadPixels not working on iOS. I don't know if this is a real issue, or some problem with the OPs code, but if you are targeting iOS, you might want to check it out. If it turns out to be a problem, Jamora's suggestion of writing out the screen shot using Application.CaptureScreenshot and reading the results back in is an alternative.
Thanks your code worked for me too!! i was looking for like this also.