/*******************************************************************************
* 版本声明:v1.0.0
* 类 名 称:LookController
* 创建日期:2019-11-14 13:21:14
* 作者名称:末零
* 功能描述:截图
******************************************************************************/
using System;
using UnityEngine;
using UnityEngine.UI;
namespace LastZero.Utility
{
public class ScreenshotController
{
/// <summary>
/// 截屏
/// </summary>
/// <param name="path">路径(含文件名及后缀)</param>
public static void CaptureScreenshot(string path)
{
ScreenCapture.CaptureScreenshot(path, 0);
}
/// <summary>
/// 对相机截图
/// </summary>
/// <returns>Texture2D截屏</returns>
/// <param name="camera">Camera.要被截屏的相机</param>
/// <param name="rect">Rect.截屏的区域</param>
public static void CaptureCamera(Camera camera, Rect rect, Image img, Action action)
{
//创建一个RenderTexture对象
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
//临时设置相关相机的targetTexture为rt, 并手动渲染相关相机
camera.targetTexture = rt;
camera.Render();
//ps: --- 如果这样加上第二个相机,可以实现只截图某几个指定的相机一起看到的图像。
//ps: camera2.targetTexture = rt;
//ps: camera2.Render();
//激活这个rt, 并从中中读取像素。
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素
screenShot.Apply();
img.sprite = AssectsController.Texture2DToSprite(screenShot);
//重置相关参数,以使用camera继续在屏幕上显示
camera.targetTexture = null;
//ps: camera2.targetTexture = null;
RenderTexture.active = null;
GameObject.Destroy(rt);
action();
}
}
}
Unity 截图功能
最新推荐文章于 2024-08-10 15:55:30 发布