Unity 中截屏的三种方法

第一种: ScreenCapture.CaptureScreenshot

优点:简单、快速的截取某一帧的画面,全屏截图。

缺点:不能针对某个摄像机截图,不能定制大小截图。

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScreenCaptureExample : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKeyDown(KeyCode.F1))
        {
            ScreenCapture1();
        }
	}
    private void ScreenCapture1()
    {
        ScreenCapture.CaptureScreenshot(Application.persistentDataPath + "/CaptureScreenshot"+Time.time.ToString()+".png");
    }
}

运行结果如下:

截图存储在计算机的位置:C:\Users\EDZ\AppData\LocalLow\DefaultCompany\ScreenCapture

 

第二种方法:Texture2D.ReadPixels

优点:可以自定义截图的大小

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class ScreenCaptureExample : MonoBehaviour {

    private Texture2D screenShot;
	void Start () {
        //实例化一张带透明通道大小为256*256的贴图
        screenShot = new Texture2D(256, 256, TextureFormat.RGB24, false);
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKeyDown(KeyCode.F1))
        {
            ScreenCapture1();
        }
        if(Input.GetKeyDown(KeyCode.F2))
        {
            StartCoroutine(ScreenCapture2(Application.persistentDataPath + "/CaptureScreenshot1" + Time.time.ToString() + ".png"));
        }
	}
    private void ScreenCapture1()
    {
        ScreenCapture.CaptureScreenshot(Application.persistentDataPath + "/CaptureScreenshot"+Time.time.ToString()+".png");
    }
    IEnumerator ScreenCapture2(string filename)
    {
        //在一帧渲染之后读取屏幕信息
        yield return new WaitForEndOfFrame();
        //读取屏幕像素信息并存储为纹理数据
        screenShot.ReadPixels(new Rect(Screen.width / 4, Screen.height / 4, Screen.width / 4 * 3, Screen.height / 4 * 3),0,0);
        screenShot.Apply();
        //将纹理数据转换成png图片文件
        byte[] bytes = screenShot.EncodeToPNG();
        //写入文件,并且指定路径
        File.WriteAllBytes(filename, bytes);
    }
}

运行结果如下:

 

第三种方法:RenderTextures

和Texture2D.ReadPixels相比,不同点在于RenderTextures可以读取某个摄像机渲染的像素

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class RenderTextures : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKeyDown(KeyCode.F12))
        {
            StartCoroutine(RenderTexturesScreenCapture(Application.persistentDataPath + "/CaptureScreenshot1" + Time.time.ToString() + ".png"));
        }
	}
    IEnumerator RenderTexturesScreenCapture(string filename)
    {
        yield return new WaitForEndOfFrame();
        RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 24);
        Texture2D screenShot = new Texture2D(256, 256, TextureFormat.RGB24, false);
        //遍历所有的摄像机
        //foreach (Camera camera in Camera.allCameras)
        //{
        //    camera.targetTexture = rt;
        //    camera.Render();
        //    camera.targetTexture = null;
        //}
        //Camera.main.targetTexture = rt;
        //Camera.main.Render();
        //Camera.main.targetTexture = null;
        Camera.allCameras[1].targetTexture = rt;
        Camera.allCameras[1].Render();
        Camera.allCameras[1].targetTexture = null;

        RenderTexture.active = rt;
        screenShot.ReadPixels(new Rect(Screen.width / 4, Screen.height / 4, Screen.width / 4 * 3, Screen.height / 4 * 3), 0, 0);
        //Camera.main.targetTexture = null;
        RenderTexture.active = null;
        Destroy(rt);
        yield return 0;
        byte[] bytes = screenShot.EncodeToPNG();
        File.WriteAllBytes(filename, bytes);
    }
}

运行效果如下图:

 

总结

1. ScreenCapture.CaptureScreenshot 在全屏截图的时候很方便。

2. Texture2D.ReadPixels 适合用在自定义图片大小的时候。

3. RenderTextures 截屏的时候可以指定设摄像机,适合截屏的时候屏蔽UI。

 

  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值