How to capture current desktop efficiently and cross-platform in Unity3D?

3.9k views Asked by At

I have a simple Unity3D-scene for Oculus Rift and want to add ability to show his own desktop to a user(from main monitor). I've found that I can render it to a Texture2D, but how to efficiently capture a desktop?

Actually, I need to provide something like http://www.vrdesktop.net/, which allows to the user see his monitor's content inside VR. But I need to implement this as a library for Unity3D. So what is the best way to do that?

P.S. It would be nice to have a cross-platform solution, at least Windows and Linux/Mac.

2

There are 2 answers

6
dk14 On BEST ANSWER

This one works (at least for Windows):

Texture2D tex = new Texture2D (200, 300, TextureFormat.RGB24, false);
Rectangle screenSize = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
Bitmap target = new Bitmap (screenSize.Width, screenSize.Height);
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(target)) {
  g.CopyFromScreen (0, 0, 0, 0, new Size (screenSize.Width, screenSize.Height));
}
MemoryStream ms = new MemoryStream ();
target.Save (ms, ImageFormat.Png);
ms.Seek (0, SeekOrigin.Begin);

tex.LoadImage (ms.ToArray ());

renderer.material.mainTexture = tex;

But efficiency is questionable here - every frame is converted to PNG, on the other hand capturing with CopyFromScreen works fine.

(Limitations) As it said in the Mono documentation: "Works on Win32 and on X11 (but not on Cocoa and Quartz)"

1
Andy On

I believe Unity3D does not have this sort of functionality. If you want to be able to interact with your desktop, you would use remote desktop software. If you want to grab your desktop view, you can use screen capturing/recording software. Else, you will have to use an incredibly complex or gimmicky method to integrate this into Unity. One of the comments here was quite informative.