ReadPixels was called to read pixels from system frame buffer, while not inside drawi

Please bear with me as I’m new to Unity. I’m using Vuforia and the Prime31 Etcetera plugin and am trying to email a screenshot of the application on an Android device. Vuforia works fine, sending an email works fine, but as soon as I try to create the screenshot LogCat shows me:

ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame.

This is the code I’m using:

void SendAndroidEmail() {
        int width = Screen.width;
        int height = Screen.height;
        Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
        tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        tex.Apply();
        byte[] bytes = tex.EncodeToPNG();
        Destroy(tex);
		
		var path = System.IO.Path.Combine( Application.persistentDataPath, "tmp_" + System.DateTime.Now.ToString() + ".png" );
		System.IO.File.WriteAllBytes( path, bytes );

		EtceteraAndroid.showEmailComposer( "noone@nothing.com", "Message subject", "click <a href='http://somelink.com'>here</a> for a present", true, path );
}

I have tried experimenting with yield but to no avail as I’m not sure what I’m doing…

Thanks!

1 Like

I was able to get the screenshot by moving the code above from ‘Update’ to ‘LateUpdate’. But unfortunately then in the the screenshot I can only see the camera image with the marker (or target), but not the model rendered on top of that…

Is there some other place I need to execute this code form to get both the model and the camera image?

1 Like
1 Like

Thank your very much! OnPostRender did the trick.

3 Likes