Unity 截图功能

/*******************************************************************************
* 版本声明: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();
        }
    }
}

返回目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

末零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值