Search Unity

Trouble with RenderTexture to Texture2D

Discussion in 'Scripting' started by Kali101, Apr 4, 2008.

  1. Kali101

    Kali101

    Joined:
    Jan 9, 2008
    Posts:
    5
    I set up a RenderTexture and rendered via ReadPixels to a Texture2D called assetThumbnail:

    Code (csharp):
    1. void OnGUI(){
    2.         if(GUILayout.Button("Take Snapshot")) {
    3.          currentCamera.Render();
    4.                    
    5.             float width = 0.36f;
    6.             float height = 0.2f;
    7.             assetThumbnail.ReadPixels(new Rect( Screen.width * width,
    8.                                                   Screen.height * height,
    9.                                                   Screen.width * (1.0f - width),
    10.                                                   Screen.height * (1.0f - height) ),
    11.                                                   0, 0, false);
    12.             assetThumbnail.Apply(false);
    13.       }
    14.       GUILayout.Box(assetThumbnail);
    15. }
    The weird thing is I set the Texture2D as public variable and it shows up no problem in the inspector GUI. But it won't show up in the in-game GUI, and when I call EncodeToPNG() on the texture the image file is blank, though it still has the right size. Any help is appreciated.
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You need to make sure the RenderTexture you are rendering into with the camera is setup to be the active, when calling ReadPixels
    RenderTexture.active = myRTex;