I have a screenshot preview at the end of the game's level. When I am loading the screenshot, it makes the game freeze for a moment. What may I do so there would be no freeze when I am loading that screenshot? Here is myc ode:

private IEnumerator ShowScreeshotPreview()
        {
    screenshotTexture = new Texture2D(Screen.height, Screen.height, TextureFormat.RGB24, false);
            for (int i = 0; i < 9; i++)
            {
                yield return null;
            }
            RenderTexture.active = Singletons.Get<SpecialEffectsHelper>().MainCharacterScript.GetComponent<GeneratorScript>().screenshotTexture;
            screenshotTexture.ReadPixels(new Rect(0, 0, Screen.height, Screen.height), 0, 0);
    screenshotTexture.Apply();
            RenderTexture.active = null; //Added to avoid errors

            screenshotPrview.mainTexture = screenshotTexture;
    ScreenshotPreviewAnimator.SetBool("visible", true);
            screenshotLoaded = true;

            yield return null;
        }
share|improve this question

I think you might find the following function useful:

public Texture2D getTexture2DFromRenderTexture(RenderTexture rTex) {
    Texture2D texture2D = new Texture2D(rTex.width, rTex.height);
    RenderTexture.active = rTex;
    texture2D.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
    texture2D.Apply();
    return texture2D;
}

This would be called in the following way:

Texture2D myTex2D = getTexture2DFromRenderTexture(myRenderTexture);
share|improve this answer
    
Nice function, Grant Mooney! Although I guess this unloads loads the texture data from GPU to CPU back again, doesnt it? Because this normaly results in stalls. This function could be usefull for my planetary terrain engine, but if it stalls it wont help unfortunately. Have you tried, or does anyone know? – user3347999 Aug 20 '16 at 7:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.