Search Instagram Twitter Facebook Spotify Grid Tag Date Folder Chat Pencil

Saving Image

Worked on optimizing saving today.

At the start of the day, it was 1000 ms as it was running in the main thread.

Had it run in a separate thread, and also split up texture2D.ReadPixels to happen over multiple frames.

I’m saving a small screenshot of the game to use in the load game screen.

Intead of doing ReadPixels all at once, I read it in chunks over the course of several frames. Much better performance.

Code here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Saving by one slice each frame
        screenshot.ReadPixels(new Rect(0, 0, width / 4, height), 0, 0);
        yield return null;

        for (int i = 1; i < 4; i++)
        {
            current_tex = RenderTexture.active;
            RenderTexture.active = renderTex;
            screenshot.ReadPixels(new Rect(width * ((float)i / 4), 0, width * ((float)i / 4), height), (int) (width * ((float)i / 4)), 0);
            RenderTexture.active = current_tex;
            screenshotCamera.targetTexture = null;

            yield return null;
        }

screenshotCamera.targetTexture = null clears the player camera.

Took a while to figure this one out. Without it, the screen kept going black momentarily.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.