Programmers learn & share
0 votes
108 views

Problem :

I am trying to create the test app using unity but facing following issue:

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

by (6.9k points)   | 108 views

1 Answer

0 votes

Solution :

Uou should wait until the end of the current frame encounters before taking the screenshot or before calling the function Texture2D.ReadPixels . This should be done with the WaitForEndOfFrame class. Please note  how I cached it to avoid the creation of new Object every time the function TakeSnapshot  is called.

e.g.

WaitForEndOfFrame frameEnd = new WaitForEndOfFrame();

public IEnumerator TakeSnapshot(int width, int height)
{
yield return frameEnd;
Texture2D texture = new Texture2D(800, 800, TextureFormat.RGB24, true);
texture.ReadPixels(new Rect(0, 0, 800, 800), 0, 0);
    texture.LoadRawTextureData(texture.GetRawTextureData());
texture.Apply();
sendTexture(texture, messageToSend);
}
This should fix your issue.
by (36.1k points)  
2,218 questions
2,683 answers
59 comments
241 users