ScreenCapture

只有两个静态方法

ScreenCapture.CaptureScreenshot

注意
1、文档中说的是在 Application.persistentDataPath,然而win10的unity编辑器中测试,是在和 Assets文件夹同一级的
2、文档中的示例不需要文件后缀自动保存为PNG文件,但测试结果是必须要有 后缀.png,否则没文件类型
3、测试发现也可以指定为其他文件类型,例如 jpg
4、文档中的示例用的 OnMouseDown(),没留意会忘了是要点击在碰撞器上才有效
5、free Aspect也是有效的,此时图片的分辨率比较随机
6、superSize 可以提高截图分辨率,文件大小也会改变
7、同名的截图文件会覆盖,所以搞一套规则(试过不用时间来命名)(大概可以用PlayerPrefs直接用数字命名)


Application.persistentDataPath :

C:/Users/zts/AppData/LocalLow/DefaultCompany/UnityEngine API

Application.dataPath :

E:/Unity 2017 zts/UnityEngine API/Assets

//filename 手机平台会自动包含 Application.persistentDataPath , 生成PNG文件,不用加后缀
//superSzie 提高分辨率的因数 (默认1,可以不用输入,如果2 ,1920*1080 就变成 3840*2160)
ScreenCapture.CaptureScreenshot( filename , superSize );

注意:文件名相同的情况下,会覆盖(可以用时间来命名,注意ToString())

if (Input.GetMouseButtonDown (0)) 
{
    Debug.Log ("--");
    ScreenCapture.CaptureScreenshot ("some.png");
}

ScreenCapture.CaptureScreenshotAsTexture

1、得到的是 Texture2D
2、需要用协程,WaitForEndOfFrame


在帧处理中,当调用它时,结果的屏幕截图会受到影响。
要捕获所有渲染堆栈,就需要在帧结束那一刻,调用它。
所以使用一个协程 yield return new WaitForEndOfFrame() 会是一个比较好的方式。
如果调用这个方法时,帧还没有结束,一些渲染工件(例如UI)可能不包含在生成的纹理中。

ScreenCapture.CaptureScreenshotAsTexture( superSize );
public class ScreenShotter : MonoBehaviour
{
    IEnumerator RecordFrame()
    {
        yield return new WaitForEndOfFrame();
        var texture = ScreenCapture.CaptureScreenshotAsTexture();
        // do something with texture

        // cleanup
        Object.Destroy(texture);
    }

    public void LateUpdate()
    {
        StartCoroutine(RecordFrame());
    }
}

保存在自定义路径

这个是别人的代码,还没试过

//首先需要在Unity里找到System.Windows.Forms.dll,并且放进工程里的Plugins文件夹里  
//using System.Windows.Forms; //需要引用  
//发布后playersetings--Resolution--Visible in background记得勾上   

public void Screenshots()  
 {  
        SaveFileDialog _SaveFileDialog = new SaveFileDialog();  
        _SaveFileDialog.InitialDirectory = "C:\\";  
        _SaveFileDialog.Filter = "Image Files(*.JPG;*.BMP;*.PNG)|*.JPG;*.BMP;*.PNG|All files (*.*)|*.*";  
        DialogResult result = _SaveFileDialog.ShowDialog();  

        UnityEngine.Screen.fullScreen = true;  
        if (result == DialogResult.OK)  
        {  
            string path = _SaveFileDialog.FileName;  
            UnityEngine.Application.CaptureScreenshot(path);  
  }  

在 Time.captureFramerate 中也有相关的,并且有一个示例代码
创建的文件夹,与 Assets文件夹 并列

using UnityEngine;
using System.Collections;

// Capture frames as a screenshot sequence. Images are
// stored as PNG files in a folder - these can be combined into
// a movie using image utility software (eg, QuickTime Pro).

public class CaptureTest : MonoBehaviour
{
    // The folder to contain our screenshots.
    // If the folder exists we will append numbers to create an empty folder.
    public string folder = "ScreenshotFolder";
    public int frameRate = 25;
    void Start()
    {
        // Set the playback framerate (real time will not relate to game time after this).
        Time.captureFramerate = frameRate;

        // Create the folder
        System.IO.Directory.CreateDirectory(folder);
    }

    void Update()
    {
        // Append filename to folder name (format is '0005 shot.png"')
        string name = string.Format("{0}/{1:D04} shot.png", folder, Time.frameCount);

        // Capture the screenshot to the specified file.
        ScreenCapture.CaptureScreenshot(name);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值