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代码:
- import System.IO;
- function Start() {
- UploadPNG();
- }
- function UploadPNG() {
- yield WaitForEndOfFrame();
- var width = Screen.width;
- var height = Screen.height;
- var tex = new Texture2D( width, height, TextureFormat.RGB24, false );
- tex.ReadPixels( Rect(0, 0, width, height), 0, 0 );
- tex.Apply();
- var bytes = tex.EncodeToPNG();
- Destroy( tex );
- File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
- var form = new WWWForm();
- form.AddField("frameCount", Time.frameCount.ToString());
- form.AddBinaryData("fileUpload",bytes);
- var w = WWW("http://localhost/cgi-bin/env.cgi?post", form);
- yield w;
- if (w.error != null)
- print(w.error);
- else
- print("Finished Uploading Screenshot");
- }
C#代码:
- using System.IO;
- using UnityEngine;
- using System.Collections;
- public class example : MonoBehaviour {
- void Start() {
- UploadPNG();
- }
- IEnumerator UploadPNG() {
- yield return new WaitForEndOfFrame();
- int width = Screen.width;
- int height = Screen.height;
- Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
- tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
- tex.Apply();
- byte[] bytes = tex.EncodeToPNG();
- Destroy(tex);
- WWWForm form = new WWWForm();
- form.AddField("frameCount", Time.frameCount.ToString());
- form.AddBinaryData("fileUpload", bytes);
- WWW w = new WWW("http://localhost/cgi-bin/env.cgi?post", form);
- yield return w;
- if (w.error != null)
- print(w.error);
- else
- print("Finished Uploading Screenshot");
- }
- }