I want to capture the screen the user is seen and send that data as a byte array to a remote machine, running another Unity3D application.
I am using Texture2D.GetRawTextureData() at present, but after timing it, I see that it takes up to 12 milliseconds:
private byte[] FrameData;
public IEnumerator SetFrame()
{
// Can take up to 12 ms!
yield return new WaitForEndOfFrame();
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
Debug.Log(sw.ElapsedMilliseconds.ToString());
FrameData = tex.GetRawTextureData();
Destroy(tex);
}
On this other machine, I do the following to display the image to the user:
void DisplayFrame(byte[] frame)
{
//3ms at most
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
texture.LoadRawTextureData(frame);
texture.Apply();
GetComponent<Renderer>().material.mainTexture = texture;
}
Is there something faster than ReadPixels?
(PS how the hell do I insert a new line between sentences? This editor just ignores them!)