2021-06-20 截取屏幕并转png格式保存到本地

截取屏幕并转png格式保存到本地

     /// <summary>
    /// 读取屏幕像素,可以截取到UI
    /// </summary>
    /// <returns></returns>
    public static IEnumerator ScreenShotOfReadPixels(Rect rect)
    {
        yield return new WaitForEndOfFrame();
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
        screenShot.ReadPixels(rect, 0, 0);
        screenShot.Apply();
        texture = screenShot;
        imgbytes = screenShot.EncodeToPNG(); //截取屏幕后转png格式
        if (Application.platform == RuntimePlatform.WindowsEditor)
        {
            File.WriteAllBytes(@"C:\Users\we\Desktop\截图\" + key + ".png", imgbytes);  //将截取后的图片保存到本地  key为图片名
        }
    }
     /// <summary>
    /// (全屏)相机截图,截取不到UI
    /// </summary>
    /// <param name="camera"></param>
    /// <param name="rect"></param>
    public static void ScreenShotOfCamera(Camera camera)
    {
        Rect rect = new Rect(0, 0, Screen.width, Screen.height);
        // 创建一个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();
        //ps: -------------------------------------------------------------------
        // 激活这个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();
        texture = screenShot;
        // 重置相关参数,以使用camera继续在屏幕上显示
        camera.targetTexture = null;
        //ps: camera2.targetTexture = null;
        RenderTexture.active = null; // JC: added to avoid errors
        Destroy(rt);
        // 最后将这些纹理数据,成一个png图片文件
        imgbytes = screenShot.EncodeToPNG();
    }
        DataJson = JsonMapper.ToJson(dataConns); //生成jSON    
        解析json:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值