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代码:
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");
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值