[SOLVED] Texture getting wrong when displayed with GUI.drawtexture or Graphics.drawtexture

Hey guys,

The idea, is to render the camera to a RenderTexture, then ReadPixels to a Texture2D, then pause the game, and display that Texture2D for the background of my menu.

I tried to put that texture to a png for debugging purpose, the image I get in the png is exactly what I want, but when it comes to drawtexture it, I have a grey/white image…

Any Ideas?

Here is some code:

    private bool IsPaused;
    public Texture2D t2d;
    void OnGUI()
    {
        if(IsPaused == true) GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), t2d);
    }
    Texture2D SaveBackground()
    {
        int resWidth = 1920;
        int resHeight = 1080;
        Camera camera0 = GameObject.Find("Main Camera").GetComponent<Camera>();
        RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
        camera0.targetTexture = rt;
        Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
        camera0.Render();
        RenderTexture.active = rt;
        screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
        camera0.targetTexture = null;
        RenderTexture.active = null;
        Destroy(rt);
      
        return screenShot;
    }
    void PauseGame()
    {
        t2d = SaveBackground();
    ////////////texture debugger/////////////
        /*byte[] bytes = t2d.EncodeToPNG();
        string filename = "testpause.png";
        System.IO.File.WriteAllBytes(filename, bytes);
        Debug.Log(string.Format("Took screenshot to: {0}", filename));*/
    ////////////////////////////////////////////
        IsPaused = true;
    }

PS: the code is cut just to have the interesting part, other parts of the code is about disabling gameobject etc…

So, I found a work around but it’s really slow

it looks like this:

    private bool IsPaused;
    public Texture2D t2d;
    void OnGUI()
    {
        if(IsPaused == true) GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), t2d);
    }
    Texture2D SaveBackground()
    {
        int resWidth = 1920;
        int resHeight = 1080;
        Camera camera0 = GameObject.Find("Main Camera").GetComponent<Camera>();
        RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
        camera0.targetTexture = rt;
        Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
        camera0.Render();
        RenderTexture.active = rt;
        screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
        camera0.targetTexture = null;
        RenderTexture.active = null;
        Destroy(rt);
   
        return screenShot;
    }
    void PauseGame()
    {
        t2d = SaveBackground();
 
        byte[] bytes = t2d.EncodeToPNG();
        t2d.LoadImage(bytes);
        ///////////////////////////////////////////////////////////////////////////////////
        /*string filename = "testpause.png";
        System.IO.File.WriteAllBytes(filename, bytes);
        Debug.Log(string.Format("Took screenshot to: {0}", filename));*/
    ////////////////////////////////////////////
        IsPaused = true;
    }

So, I checked the format of t2d after LoadImage, it’s ARGB32. So, I tried that:

    private bool IsPaused;
    public Texture2D t2d;
    void OnGUI()
    {
        if(IsPaused == true) GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), t2d);
    }
    Texture2D SaveBackground()
    {
        int resWidth = 1920;
        int resHeight = 1080;
        Camera camera0 = GameObject.Find("Main Camera").GetComponent<Camera>();
        RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
        camera0.targetTexture = rt;
        Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.ARGB32, false);
        camera0.Render();
        RenderTexture.active = rt;
        screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
        camera0.targetTexture = null;
        RenderTexture.active = null;
        Destroy(rt);
   
        return screenShot;
    }
    void PauseGame()
    {
        t2d = SaveBackground();
        ////////////////////////////////////////////////////////////////// 
        /*byte[] bytes = t2d.EncodeToPNG();
        t2d.LoadImage(bytes);
        string filename = "testpause.png";
        System.IO.File.WriteAllBytes(filename, bytes);
        Debug.Log(string.Format("Took screenshot to: {0}", filename));*/
    ////////////////////////////////////////////
        IsPaused = true;
    }

It still doesnt work, Any Idea? Am I missing something?

Okay, found the what was wrong, the right code is:

    private bool IsPaused;
    public Texture2D t2d;
    void OnGUI()
    {
        if(IsPaused == true) GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), t2d);
    }
    Texture2D SaveBackground()
    {
        int resWidth = 1920;
        int resHeight = 1080;
        Camera camera0 = GameObject.Find("Main Camera").GetComponent<Camera>();
        RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
        camera0.targetTexture = rt;
        Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.ARGB32, false);
        camera0.Render();
        RenderTexture.active = rt;
        screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
        camera0.targetTexture = null;
        RenderTexture.active = null;
        Destroy(rt);
  
        return screenShot;
    }
    void PauseGame()
    {
        t2d = SaveBackground();
        t2d.Apply(); //<<<<<<<<<<<<<<THIS
        //////////////////////////////////////////////////////////////////
        /*byte[] bytes = t2d.EncodeToPNG();
        t2d.LoadImage(bytes);
        string filename = "testpause.png";
        System.IO.File.WriteAllBytes(filename, bytes);
        Debug.Log(string.Format("Took screenshot to: {0}", filename));*/
    ////////////////////////////////////////////
        IsPaused = true;
    }

Can be useful to someone one day.
Bye =)