webplayer中截屏

来源:互联网 发布:奶酪 知乎 编辑:程序博客网 时间:2024/06/16 18:51
因为webplayer的限制,在webplayer上运行unity程序的时候,截屏并不像本地运行程序那样。基本思路是:
1、通过相应的接口把屏幕像素读取到内存,并且encode成png格式;
2、通过wwwform类把png格式的流由服务器接收,并且存在服务器上,做成链接,然用户下载;


unity官方相关的代码例子:

  1. // Grab a screen shot and upload it to a CGI script.
  2. // The CGI script must be able to hande form uploads.
  3. var screenShotURL= "http://www.my-server.com/cgi-bin/screenshot.pl";
  4. // Take a screen shot immediately
  5. function Start() {
  6.     UploadPNG();
  7. }
  8. function UploadPNG() {
  9.     // We should only read the screen after all rendering is complete
  10.     yield WaitForEndOfFrame();
  11.     // Create a texture the size of the screen, RGB24 format
  12.     var width = Screen.width;
  13.     var height = Screen.height;
  14.     var tex = new Texture2D( width, height, TextureFormat.RGB24, false );
  15.     // Read screen contents into the texture
  16.     tex.ReadPixels( Rect(0, 0, width, height), 0, 0 );
  17.     tex.Apply();
  18.     // Encode texture into PNG
  19.     var bytes = tex.EncodeToPNG();
  20.     Destroy( tex );
  21.     // Create a Web Form
  22.     var form = new WWWForm();
  23.     form.AddField("frameCount", Time.frameCount.ToString());
  24.     form.AddBinaryData("fileUpload", bytes, "screenShot.png", "image/png");
  25.     // Upload to a cgi script
  26.     var w = WWW(screenShotURL, form);
  27.     yield w;
  28.     if (!String.IsNullOrEmpty(w.error))
  29.         print(w.error);
  30.     else
  31.         print("Finished Uploading Screenshot");
  32. }
复制代码



上面的代码段是用在unity程序中的:截取屏幕并生成png格式的流;访问服务器脚本,让改代码接收png格式的流。screenShotURL是wwwform访问的服务器脚本。
0 0
原创粉丝点击