Search Unity

Quick screenshot question

Discussion in 'Scripting' started by geetoo, Jun 8, 2011.

  1. geetoo

    geetoo

    Joined:
    Apr 21, 2009
    Posts:
    42
    Hi. Sorry for posting yet another screenshot question, but nothing I found in other posts satisfied me.

    I'm trying to make a Screenshot function. Here is my code :
    Code (csharp):
    1. // Copy pixels to a 2D texture
    2. RenderTexture.active = thisCamera.targetTexture;
    3. exportTexture.ReadPixels(new Rect(0, 0, thisCamera.targetTexture.width, thisCamera.targetTexture.height), 0, 0, false);
    4. exportTexture.Apply(false);
    5.  
    6. // Resize to correct ratio
    7. exportTexture.Resize(screenshotWidth, screenshotHeight);
    8. exportTexture.Apply(false);
    9. Debug.Log(exportTexture.width + " x " + exportTexture.height);
    10.  
    11. // Convert pixels as a byte array and write it to a PNG file
    12. exportBytes = exportTexture.EncodeToPNG();
    13. File.WriteAllBytes(exportPath, exportBytes);
    14.  
    15. // Deactivate render texture
    16. RenderTexture.active = null;
    Reading pixels from RenderTexture is OK. And writing them to file (without resizing) is OK.
    But if I uncomment the two resizing lines, it makes the resulting PNG file entirely black. The resolution though, is correctly set and the Debug.Log line shows the correct one (i.e. 1920 x 1080).

    Does someone see what's wrong ?

    Thanks a lot.
     
  2. heohni

    heohni

    Joined:
    Feb 26, 2010
    Posts:
    14