Unity3D的截屏函数

原文地址: http://docs.unity3d.com/Documentation/ScriptReference/Application.CaptureScreenshot.html


Application.CaptureScreenshot

静态函数 void CaptureScreenshot (String filename, int superSize = 0)

描述
抓取一张屏幕截图并以png格式保存为给定的文件名。
(译者:文件会被保存在/data/data/your.package.name/files/目录下)

如果文件已经存在,会覆盖。该函数不能用于web player中。

如果supersize这个参数大于1,会截出更高分辨率的图片,例如supersize设为4,那么截出的图会是普通情况下的4X4倍大,这个参数在需要打印截图的场合会很有用。

示例:
function OnMouseDown() 
{
 Application.CaptureScreenshot("Screenshot.png");
}


Unity中另外一种截屏的方法:
主要用到了Texture2D.ReadPixels()方法和Texture2D.EncodeToPng()方法


JS代码:
[javascript] view plain copy
  1. import System.IO;  
  2. function Start() {  
  3.     UploadPNG();  
  4. }  
  5.   
  6. function UploadPNG() {  
  7. yield WaitForEndOfFrame();  
  8.   
  9.  var width = Screen.width;  
  10.  var height = Screen.height;  
  11. var tex = new Texture2D( width, height, TextureFormat.RGB24, false );  
  12.   
  13.  tex.ReadPixels( Rect(0, 0, width, height), 0, 0 );  
  14.  tex.Apply();  
  15.   
  16. var bytes = tex.EncodeToPNG();  
  17. Destroy( tex );  
  18.   
  19. File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);  
  20.   
  21. var form = new WWWForm();  
  22. form.AddField("frameCount", Time.frameCount.ToString());  
  23. form.AddBinaryData("fileUpload",bytes);  
  24.   
  25. var w = WWW("http://localhost/cgi-bin/env.cgi?post", form);  
  26. yield w;  
  27.  if (w.error != null)  
  28.         print(w.error);      
  29. else  
  30.         print("Finished Uploading Screenshot");      
  31. }  

C#代码:
[csharp] view plain copy
  1. using System.IO;  
  2. using UnityEngine;  
  3. using System.Collections;  
  4.   
  5. public class example : MonoBehaviour {  
  6.     void Start() {  
  7.         UploadPNG();  
  8.     }  
  9.   
  10.     IEnumerator UploadPNG() {  
  11.         yield return new WaitForEndOfFrame();  
  12.         int width = Screen.width;  
  13.         int height = Screen.height;  
  14.         Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);  
  15.         tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);  
  16.         tex.Apply();  
  17.         byte[] bytes = tex.EncodeToPNG();  
  18.         Destroy(tex);  
  19.         WWWForm form = new WWWForm();  
  20.         form.AddField("frameCount", Time.frameCount.ToString());  
  21.         form.AddBinaryData("fileUpload", bytes);  
  22.         WWW w = new WWW("http://localhost/cgi-bin/env.cgi?post", form);  
  23.         yield return w;  
  24.         if (w.error != null)  
  25.             print(w.error);  
  26.         else  
  27.             print("Finished Uploading Screenshot");  
  28.     }  
  29. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值