encodeToPNG - gray image problem

Hi,

I’m trying to get screenshots via IO except of Application.screenCapture. I’m using example from documentation:

function takeSnapShot (){
	print("Taking snap...");
	yield new WaitForEndOfFrame ();
	// Create a texture the size of the screen, RGB24 format
	var tex = new Texture2D (128, 128, TextureFormat.RGB24, false);
	// Read screen contents into the texture
	tex.ReadPixels (Rect(0, 0, Screen.width, Screen.height), 0, 0);
	tex.Apply ();
	
	// Encode texture into PNG
	var bytes = tex.EncodeToPNG();
	Destroy (tex);
	
	// Write to a file in the project folder
	File.WriteAllBytes(Application.dataPath+"/externalData/smini"+currDataBlock+".png", bytes);
	print("Snap taken!");
}

And there is a problem - with yield function screenshot is never taken - console stops with “Taking snap…”, without yield screenshot is taken, but it’s grey, blank texture (size and format are correct). I’m not familiar with I/O so any help and advice is welcome.

Kind regards,
Artur

Where is this function called? Can you post the calling function?

I just cut pasted your code and called it from Update() when a key was pressed. It only grabs a small 128x128 corner of the screen (as determined by your texture format), but otherwise showed up fine - with or without the yield. If that section of the screen isn’t grey, then it’s likely a problem in the calling function.