Okay, so I’m using this code from the wiki to capture a screenshot, write it to a texture, encode it to PNG format, and then save it as a .PNG file.
The problem with this is that all I keep getting are grey images when I am in a production build… While I’m testing in the editor, it works just fine… But in a production build, it just produces a grey image… Why could this be?
Here is the code:
IEnumerator ScreenshotEncode()
{
count++;
// wait for graphics to render
yield return new WaitForEndOfFrame();
// create a texture to pass to encoding
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
// put buffer into texture
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();
// split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
yield return 0;
byte[] bytes = texture.EncodeToPNG();
// save our test image (could also upload to WWW)
File.WriteAllBytes(Application.dataPath + "/Screenshot-" + System.DateTime.Now.Month + "-" + System.DateTime.Now.Day + "-" + System.DateTime.Now.Year + "(" + count + ")" + ".png", bytes);
//FileInfo fi = new FileInfo(Application.dataPath + "/Screenshot-" + System.DateTime.Now.Month + "-" + System.DateTime.Now.Day + "-" + System.DateTime.Now.Year + "(" + count + ")" + ".jpg");
//count++;
// Added by Karl. - Tell unity to delete the texture, by default it seems to keep hold of it and memory crashes will occur after too many screenshots.
DestroyObject( texture );
Debug.Log("Screenshot saved.");
AddToLog("Screenshot saved as: " + Application.dataPath + "/Screenshot-" + System.DateTime.Now.Month + "-" + System.DateTime.Now.Day + "-" + System.DateTime.Now.Year + "(" + count + ")" + ".png");
displayText = "";
displayText = GetDisplayText();
}