- Home /
CaptureScreenshot
Hello, I have a little performance problem with Application.CaptureScreenshot function. It's 2D scrolling game for IOS. When the player dies, I capture screen by CaptureScreenshot. But it drops half of the FPS and lags. (from 60 to (30~45)).
Without capturing screenshot, FPS doesn't drop and the game hasn't got any lag.
I have tried to reduce hitches by lerping timescale to 0.5. It improves little bit but not quite enough.
Does anyone know best way to take a screenshot? Thanks in advance.
Sadly Application.CaptureScreenshot will peak on your performance. The only other alternative I know is to use EncodeToPNG as described here:
http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.EncodeToPNG.html
Or if you have pro to use a render texture.
Ty for your reply. I will do test both of them on $$anonymous$$onday.
I have tested http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.EncodeToPNG.html But it's even worse, decreasing FPS to 20 ~ 25.
private IEnumerator TakeScreenshot() {
yield return new WaitForEndOfFrame();
var width = Screen.width;
var height = Screen.height;
var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
byte[] screenshot = tex.EncodeToPNG();
Destroy(tex);
File.WriteAllBytes( Application.persistentDataPath + "/" + screenshotFilename + ".png", screenshot);
}
Also I have tried with threading with File.WriteAllBytes but no luck.
Is there any method that saves the current camera render to memory. After that I can save to file.(Some moment later.)
Sorry for the late reply. As far as I know the only way to save a camera render apart Application.CaptureScreenshot is with a RenderTexture, sadly I can't help you here as I haven't the pro version of unity, which is required for RenderTexture.
But with some research on the forums I think you can find info on this.
Answer by saruul34 · Mar 19, 2014 at 08:03 AM
Thank you @Neurological for keep helping me. I have solved this question by RenderTexture as you adviced.
Assets -> Create -> Render Texture
public RenderTexture currentRT; // Drag here created texture.
Called when the player dies:
private IEnumerator TakeScreenshot() { yield return new WaitForEndOfFrame(); Camera.main.targetTexture = currentRT; yield return new WaitForSeconds(0.2f); Camera.main.targetTexture = null; }
When screenshot needed, save to Document
void save () {
int width = 512;
int height = 256;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
RenderTexture.active = ScreenShot.Instance.currentRT;
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
byte[] screenshot = tex.EncodeToPNG();
RenderTexture.active = null;
File.WriteAllBytes(pathToImage, screenshot);
}
Glad you solved it, and thanks for posting your solution, if one day I'll be able to get a Pro license I'll be sure to follow your solution.