Unity -- 普通截图和相机截图以及图片保存

  1. 在项目中需要用到Unity的截图,于是查阅了很多资料,也看了很多博客,发现都写的不够详细,所以自己对各种方法都尝试了一下,并加以总结。

    UnityEngine自带API方法
    ScreenCapture.CaptureScreenshot(string filename);
    这个方法是UnityEngine自带的api,截取的是在某一帧时整个游戏的全部画面,即全屏截图。

     èªå¸¦apiæªå¾

     

    这个api只能截取全屏,对局部画面的截图很难去操作;

    参数内传入文件名,包括文件的路径(注意:必须带上图片的后缀,如.png等,不然保存的文件没有类型);

    具体的效率问题,因为觉得不太好用,所以并没有做专门的测试;

    ScreenCapture.CaptureScreenshotAsTexture();
    这个方法也是UnityEngine自带的api,截取的是在某一帧时整个游戏的全部画面,即全屏截图。最后得到的是一个Texture2D对象,可以用在游戏内展示,也可以处理Texture2D对象转化为图片文件储存起来。

  2.  

    èªå¸¦apiæªå¾


    这个api只能截取全屏;
    返回Texture2D对象,可对Texture2D进行操作,写入图片文件;
    注意:需要写在协程里面,等待帧结束后才能调用,不然会报错:failed to generate texture! Was method called before the ‘end of frame’ state was reached?
    使用Texture2D读取屏幕像素
    使用Texture2D.ReadPixels()方法,读取屏幕上的像素信息,然后进行Texture2D读åå±å¹åç´ 保存。


    可截取全屏或者指定区域内的图片;
    Rect指截取的区域,可用Rect rect = new Rect(x,y,width,height)去指定需要截图的区域;
    加入了一个委托,用于处理截图完成之后的回调。比如需要截取无UI的图片,则可以在截图之前,先把UI关闭显示,然后截完图之后,再打开UI即可;
    注意:需要写在协程里面,等待帧结束后才能调用,不然会报错:ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame.
    对相机拍摄区域进行截图
    在场景内专门放一个截图用的相机,也可以使用多个相机,最后画面是叠加显示,对给定区域进行截图。

  3.  

    ç»å®ç¸æºæªå¾


    将截图相机放置在需要的位置,可对游戏内任意位置进行截图,并且去除UI层;
    Rect指截取的区域,可用Rect rect = new Rect(x,y,width,height)去指定需要截图的区域;
    如果需要多个相机共同作用,可自行加入其它的相机,最后的结果是叠加显示的。

    最后附上代码:

    另附上GitHub链接,里面有demo示例:截图工具类GitHub链接

    using UnityEngine;
    using System.Collections;

    public delegate void CallBack();//利用委托回调可以先关闭UI,截取到没有UI的画面
    /// <summary>
    /// 截图工具类
    /// </summary>
    public class ScreenTool
    {
        private static ScreenTool _instance;
        public static ScreenTool Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new ScreenTool();
                return _instance;
            }
        }

        /// <summary>
        /// UnityEngine自带截屏Api,只能截全屏
        /// </summary>
        /// <param name="fileName">文件名</param>
        public void ScreenShotFile(string fileName)
        {
            UnityEngine.ScreenCapture.CaptureScreenshot(fileName);//截图并保存截图文件
            Debug.Log(string.Format("截取了一张图片: {0}", fileName));

    #if UNITY_EDITOR
            UnityEditor.AssetDatabase.Refresh();//刷新Unity的资产目录
    #endif
        }
        /// <summary>
        /// UnityEngine自带截屏Api,只能截全屏
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="callBack">截图完成回调</param>
        /// <returns>协程</returns>
        public IEnumerator ScreenShotTex(string fileName, CallBack callBack = null)
        {
            yield return new WaitForEndOfFrame();//等到帧结束,不然会报错
            Texture2D tex = UnityEngine.ScreenCapture.CaptureScreenshotAsTexture();//截图返回Texture2D对象
            byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片
            System.IO.File.WriteAllBytes(fileName, bytes);//写入数据
            Debug.Log(string.Format("截取了一张图片: {0}", fileName));

            callBack?.Invoke();
    #if UNITY_EDITOR
            UnityEditor.AssetDatabase.Refresh();//刷新Unity的资产目录
    #endif
        }
        /// <summary>
        /// 截取游戏屏幕内的像素
        /// </summary>
        /// <param name="rect">截取区域:屏幕左下角为0点</param>
        /// <param name="fileName">文件名</param>
        /// <param name="callBack">截图完成回调</param>
        /// <returns></returns>
        public IEnumerator ScreenCapture(Rect rect, string fileName, CallBack callBack = null)
        {
            yield return new WaitForEndOfFrame();//等到帧结束,不然会报错
            Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象
            tex.ReadPixels(rect, 0, 0);//读取像素,屏幕左下角为0点
            tex.Apply();//保存像素信息

            byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片
            System.IO.File.WriteAllBytes(fileName, bytes);//写入数据
            Debug.Log(string.Format("截取了一张图片: {0}", fileName));

            callBack?.Invoke();
    #if UNITY_EDITOR
            UnityEditor.AssetDatabase.Refresh();//刷新Unity的资产目录
    #endif
        }
        /// <summary>
        /// 对相机拍摄区域进行截图,如果需要多个相机,可类比添加,可截取多个相机的叠加画面
        /// </summary>
        /// <param name="camera">待截图的相机</param>
        /// <param name="width">截取的图片宽度</param>
        /// <param name="height">截取的图片高度</param>
        /// <param name="fileName">文件名</param>
        /// <returns>返回Texture2D对象</returns>
        public Texture2D CameraCapture(Camera camera, Rect rect, string fileName)
        {
            RenderTexture render = new RenderTexture((int)rect.width, (int)rect.height, -1);//创建一个RenderTexture对象 

            camera.gameObject.SetActive(true);//启用截图相机
            camera.targetTexture = render;//设置截图相机的targetTexture为render
            camera.Render();//手动开启截图相机的渲染

            RenderTexture.active = render;//激活RenderTexture
            Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象
            tex.ReadPixels(rect, 0, 0);//读取像素
            tex.Apply();//保存像素信息

            camera.targetTexture = null;//重置截图相机的targetTexture
            RenderTexture.active = null;//关闭RenderTexture的激活状态
            Object.Destroy(render);//删除RenderTexture对象

            byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片
            System.IO.File.WriteAllBytes(fileName, bytes);//写入数据
            Debug.Log(string.Format("截取了一张图片: {0}", fileName));

    #if UNITY_EDITOR
            UnityEditor.AssetDatabase.Refresh();//刷新Unity的资产目录
    #endif

            return tex;//返回Texture2D对象,方便游戏内展示和使用
        }
    }
     

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值