Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

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

Discussion in 'UGUI & TextMesh Pro' started by Adrenesis, Mar 6, 2015.

  1. Adrenesis

    Adrenesis

    Joined:
    Feb 22, 2015
    Posts:
    7
    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:
    Code (CSharp):
    1.     private bool IsPaused;
    2.     public Texture2D t2d;
    3.     void OnGUI()
    4.     {
    5.         if(IsPaused == true) GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), t2d);
    6.     }
    7.     Texture2D SaveBackground()
    8.     {
    9.         int resWidth = 1920;
    10.         int resHeight = 1080;
    11.         Camera camera0 = GameObject.Find("Main Camera").GetComponent<Camera>();
    12.         RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
    13.         camera0.targetTexture = rt;
    14.         Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
    15.         camera0.Render();
    16.         RenderTexture.active = rt;
    17.         screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
    18.         camera0.targetTexture = null;
    19.         RenderTexture.active = null;
    20.         Destroy(rt);
    21.      
    22.         return screenShot;
    23.     }
    24.     void PauseGame()
    25.     {
    26.         t2d = SaveBackground();
    27.     ////////////texture debugger/////////////
    28.         /*byte[] bytes = t2d.EncodeToPNG();
    29.         string filename = "testpause.png";
    30.         System.IO.File.WriteAllBytes(filename, bytes);
    31.         Debug.Log(string.Format("Took screenshot to: {0}", filename));*/
    32.     ////////////////////////////////////////////
    33.         IsPaused = true;
    34.     }
    PS: the code is cut just to have the interesting part, other parts of the code is about disabling gameobject etc...
     
    Last edited: Mar 6, 2015
  2. Adrenesis

    Adrenesis

    Joined:
    Feb 22, 2015
    Posts:
    7
    So, I found a work around but it's really slow

    it looks like this:


    Code (CSharp):
    1.     private bool IsPaused;
    2.     public Texture2D t2d;
    3.     void OnGUI()
    4.     {
    5.         if(IsPaused == true) GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), t2d);
    6.     }
    7.     Texture2D SaveBackground()
    8.     {
    9.         int resWidth = 1920;
    10.         int resHeight = 1080;
    11.         Camera camera0 = GameObject.Find("Main Camera").GetComponent<Camera>();
    12.         RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
    13.         camera0.targetTexture = rt;
    14.         Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
    15.         camera0.Render();
    16.         RenderTexture.active = rt;
    17.         screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
    18.         camera0.targetTexture = null;
    19.         RenderTexture.active = null;
    20.         Destroy(rt);
    21.    
    22.         return screenShot;
    23.     }
    24.     void PauseGame()
    25.     {
    26.         t2d = SaveBackground();
    27.  
    28.         byte[] bytes = t2d.EncodeToPNG();
    29.         t2d.LoadImage(bytes);
    30.         ///////////////////////////////////////////////////////////////////////////////////
    31.         /*string filename = "testpause.png";
    32.         System.IO.File.WriteAllBytes(filename, bytes);
    33.         Debug.Log(string.Format("Took screenshot to: {0}", filename));*/
    34.     ////////////////////////////////////////////
    35.         IsPaused = true;
    36.     }
    So, I checked the format of t2d after LoadImage, it's ARGB32. So, I tried that:

    Code (CSharp):
    1.     private bool IsPaused;
    2.     public Texture2D t2d;
    3.     void OnGUI()
    4.     {
    5.         if(IsPaused == true) GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), t2d);
    6.     }
    7.     Texture2D SaveBackground()
    8.     {
    9.         int resWidth = 1920;
    10.         int resHeight = 1080;
    11.         Camera camera0 = GameObject.Find("Main Camera").GetComponent<Camera>();
    12.         RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
    13.         camera0.targetTexture = rt;
    14.         Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.ARGB32, false);
    15.         camera0.Render();
    16.         RenderTexture.active = rt;
    17.         screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
    18.         camera0.targetTexture = null;
    19.         RenderTexture.active = null;
    20.         Destroy(rt);
    21.    
    22.         return screenShot;
    23.     }
    24.     void PauseGame()
    25.     {
    26.         t2d = SaveBackground();
    27.         //////////////////////////////////////////////////////////////////
    28.         /*byte[] bytes = t2d.EncodeToPNG();
    29.         t2d.LoadImage(bytes);
    30.         string filename = "testpause.png";
    31.         System.IO.File.WriteAllBytes(filename, bytes);
    32.         Debug.Log(string.Format("Took screenshot to: {0}", filename));*/
    33.     ////////////////////////////////////////////
    34.         IsPaused = true;
    35.     }
    It still doesnt work, Any Idea? Am I missing something?
     
  3. Adrenesis

    Adrenesis

    Joined:
    Feb 22, 2015
    Posts:
    7
    Okay, found the what was wrong, the right code is:
    Code (CSharp):
    1.     private bool IsPaused;
    2.     public Texture2D t2d;
    3.     void OnGUI()
    4.     {
    5.         if(IsPaused == true) GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), t2d);
    6.     }
    7.     Texture2D SaveBackground()
    8.     {
    9.         int resWidth = 1920;
    10.         int resHeight = 1080;
    11.         Camera camera0 = GameObject.Find("Main Camera").GetComponent<Camera>();
    12.         RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
    13.         camera0.targetTexture = rt;
    14.         Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.ARGB32, false);
    15.         camera0.Render();
    16.         RenderTexture.active = rt;
    17.         screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
    18.         camera0.targetTexture = null;
    19.         RenderTexture.active = null;
    20.         Destroy(rt);
    21.  
    22.         return screenShot;
    23.     }
    24.     void PauseGame()
    25.     {
    26.         t2d = SaveBackground();
    27.         t2d.Apply(); //<<<<<<<<<<<<<<THIS
    28.         //////////////////////////////////////////////////////////////////
    29.         /*byte[] bytes = t2d.EncodeToPNG();
    30.         t2d.LoadImage(bytes);
    31.         string filename = "testpause.png";
    32.         System.IO.File.WriteAllBytes(filename, bytes);
    33.         Debug.Log(string.Format("Took screenshot to: {0}", filename));*/
    34.     ////////////////////////////////////////////
    35.         IsPaused = true;
    36.     }
    Can be useful to someone one day.
    Bye =)