我有一个用于Oculus Rift的简单Unity3D场景,想要增加向用户展示自己的桌面的能力(从主监视器)。我发现可以将其渲染到Texture2D,但是如何有效捕获桌面呢?
实际上,我需要提供类似http://www.vrdesktop.net/的内容,该内容允许用户在VR中查看其监视器的内容。但是我需要将其实现为Unity3D的库。那么最好的方法是什么?
PS最好有一个跨平台的解决方案,至少是Windows和Linux / Mac。
此功能有效(至少适用于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;
但是这里的效率值得怀疑-每个帧都转换为PNG,另一方面,使用CopyFromScreen捕获效果很好。
(局限性)如Mono文档中所述:“在Win32和X11上可用(但在Cocoa和Quartz上不可用)”
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句