- Home /
EncodeToPng that works more smoothly on mobile
On iOS Android - Is it possible for a texture2D to be encoded to png that doesn't freeze the frame for a perceptible second or two?
Here's a snippet example of the freeze delay from a screenshot encoded to png
void screenshot(){ int width = Screen.width; int height = Screen.height; tex = new Texture2D( width, height, TextureFormat.RGB24, false );
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0 );
tex.Apply();
// this produces a noticable pause on mobile
byte[] bytes = tex.EncodeToPNG();
}
Answer by Dreamora · May 16, 2013 at 07:08 AM
That can not be avoided completely. ReadPixels locks the rendering and reads all pixels from the GPU to RAM which is a quite a visual hickup especially on iPad Retina.
The only part that can be made less impacting would be to replace the EndcodeToPNG with an own threaded implementation. That would likely shorten the length of the pause.
EncodeToPNG() can only be called from the main thread. Try EncodeToJPEG() which seems faster.
You can not use EncodeToPNG for this as you have no control over Unity Engine functions. You will need to use a 3rd party image library for this or in this case: use what iOS offers you for example through a few native calls (work on any Unity iOS license as plugins are not a Pro thing on mobile)
Your answer
Follow this Question
Related Questions
Mobile Game Different Screen Size Issue 2 Answers
Restart App on iOS and Android 0 Answers
How to detect screen touching more frequently than fps. 1 Answer
Mobile device screen sizes 4 Answers
size of GUI pics too big 1 Answer