在开发过程中,我们可能需要用到场景截图的功能,我在开发一个VR项目的时候,用到了手持相机截图的功能,自己写了个脚本,觉得还挺好用的,分享一下:
/****************************************************
文件:CameraShot.cs
作者:Paul 邮箱: 794451358@qq.com
日期:2020/7/23 16:6:42
功能:截图
*****************************************************/
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using System.IO;
namespace PFarmeWork
{
public class CameraShot
{
public static void SaveShot(Camera ShotCamera,string imgPath)
{
RenderTexture renderTexture = ShotCamera.targetTexture;
Rect rect = new Rect(0, 0, renderTexture.width, renderTexture.height);
RenderTexture.active = renderTexture;
Texture2D texture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.RGB24, false);
texture.ReadPixels(rect, 0, 0);
texture.Apply();
byte[] bytes = texture.EncodeToPNG();
File.WriteAllBytes(imgPath, bytes);
}
}
}
要用的直接拷贝代码就能使用了