Unity屏幕截图、区域截图、读取图片、WebGL长截屏并下载到本地jpg

Unity屏幕截图、区域截图、读取图片、WebGL长截屏并下载到本地jpg

一、全屏截图并保存到StreamingAssets路径下

   Texture2D screenShot;//保存截取的纹理
    public Image image;  //显示截屏的Image
 public void Jietu()
    {
         StartCoroutine(ScrrenCapture(new Rect(0, 0, Screen.width, Screen.height), 0));    
    }
    IEnumerator ScrrenCapture(Rect rect, int a)
    {

        screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
        yield return new WaitForEndOfFrame();
        screenShot.ReadPixels(rect, 0, 0);
        screenShot.Apply();

        yield return new WaitForSeconds(0.1f);

        Sprite sp = Sprite.Create(screenShot, new Rect(0, 0, screenShot.width, screenShot.height), new Vector2(0.5f, 0.5f), 100.0f);
        image.sprite = sp;
        
             //保存到streamingAssets
        byte[] bytes = screenShot.EncodeToJPG();
        string filename = Application.streamingAssetsPath + "/Images/Screenshot" + DateTime.UtcNow.Ticks + ".png";
        File.WriteAllBytes(filename, bytes);
            }

二、区域截图并保存到StreamingAssets路径下

  Texture2D screenShot;//保存截取的纹理
    public Image image;  //显示截屏的Image
    public Image im;
    Texture2D texture2ds;//存储的截图
     public void Jietu()
    {
        StartCoroutine(getScreenTexture(im.rectTransform));
    }
    public IEnumerator getScreenTexture(RectTransform rectT)
    {
        yield return new WaitForEndOfFrame();

        texture2ds = new Texture2D((int)rectT.rect.width, (int)rectT.rect.height, TextureFormat.RGB24, true);
        float x = rectT.localPosition.x + (Screen.width - rectT.rect.width) / 2;
        float y = rectT.localPosition.y + (Screen.height - rectT.rect.height) / 2;
        Rect position = new Rect(x, y, rectT.rect.width, rectT.rect.height);
        texture2ds.ReadPixels(position, 0, 0, true);//按照设定区域读取像素;注意是以左下角为原点读取
        texture2ds.Apply();
   Sprite sp = Sprite.Create(texture2ds, new Rect(0, 0, texture2ds.width, texture2ds.height), Vector2.zero);
        image.sprite = sp;
         //保存到streamingAssets
        byte[] bytes = texture2ds.EncodeToJPG();
        string filename = Application.streamingAssetsPath + "/Images/Screenshot" + DateTime.UtcNow.Ticks + ".png";
        File.WriteAllBytes(filename, bytes);
    }

三、unity发布WebGL屏幕长截屏并通过浏览器下载到本地jpg文件

在这里插入图片描述

using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// unity发布WebGL屏幕长截屏并通过浏览器下载到本地jpg文件
/// </summary>
public class ScreenshotArea : MonoBehaviour
{
    [Header("截图区域")]
    public RectTransform screenshot_area;
    [Header("滚动条")]
    public Scrollbar scrollbar;
    [Header("截图数量")]
    public int number_max;
    [Header("每次截图滑动条vaule的位置,从下往上记")]
    public float[] number;
    [Header("是否横向合并")]
    public bool isHorizontal;

    Texture2D[] texture2ds;//存储的截图
    Texture2D merge_image;//合并后的图片
    string image_name="测试";//下载后图片的名字


    public RectTransform screenshot_area1;
    public RectTransform screenshot_area2;
    private void Start()
    {
        texture2ds = new Texture2D[number_max];
    }
    public void OnClick_调用()
    {
        Screen.fullScreen = true;
        StartCoroutine(getScreenTexture(screenshot_area));
    }

    #region 屏幕多次截图
    public IEnumerator getScreenTexture(RectTransform rectT)
    {
        scrollbar.value = number[0];
        yield return new WaitForEndOfFrame();

        for (int i = 0; i < number_max; i++)
        {
            texture2ds[i] = new Texture2D((int)rectT.rect.width, (int)rectT.rect.height, TextureFormat.RGB24, true);
            float x = rectT.localPosition.x + (Screen.width - rectT.rect.width) / 2;
            float y = rectT.localPosition.y + (Screen.height - rectT.rect.height) / 2;
            Rect position = new Rect(x, y, rectT.rect.width, rectT.rect.height);
            texture2ds[i].ReadPixels(position, 0, 0, true);//按照设定区域读取像素;注意是以左下角为原点读取
            texture2ds[i].Apply();

            if (i < number_max - 1)
                scrollbar.value = number[i + 1];
            if (i == 0)
            {
                rectT = screenshot_area;
            }
            if (i == number_max - 2)
            {
                rectT = screenshot_area1;
            }

            yield return new WaitForEndOfFrame();
        }
       
        merge_image = MergeImage(texture2ds); //图片合并
#if UNITY_EDITOR
        byte[] bytes = merge_image.EncodeToJPG();
        string filename = Application.streamingAssetsPath + "/Screenshot" + UnityEngine.Random.Range(0, 1000) + ".png";
        File.WriteAllBytes(filename, bytes);
#endif
        
        DownLoad(merge_image);//下载图片
    }
    #endregion
    #region 下载图片
    Sprite sprite;
    private void DownLoad(Texture2D screenShot)
    {
        sprite = Sprite.Create(screenShot, new Rect(0, 0, screenShot.width, screenShot.height), new Vector2(0.5f, 0.5f));

        byte[] photoByte = getImageSprite();//获取jpeg图像的字节流
        if (photoByte != null)
        {
            DownloadImage(photoByte, image_name+".jpg");
        }
        else
        {
            Debug.LogError("<color=red>下载失败</color>");
        }
    }
    private byte[] getImageSprite()
    {
        if (sprite)
        {
            return sprite.texture.EncodeToJPG();
        }
        return null;
    }
    #endregion
    #region 调用js方法下载
    [DllImport("__Internal")]
    private static extern void ImageDownloader(string str, string fn);
    public void DownloadImage(byte[] imageData, string imageFileName = "newpic")
    {
#if UNITY_EDITOR
        Debug.Log("<color=blue>编辑器无法下载</color>");
#else
        if (imageData != null)
        {
            Debug.Log("Downloading..." + imageFileName);
            ImageDownloader(System.Convert.ToBase64String(imageData), imageFileName);
        }
#endif
    }
    #endregion
    #region 合并多张图片
    public Texture2D MergeImage(Texture2D[] tex)
    {
        if (tex.Length == 0)
            return null;

        //定义新图的宽高, 合并分为两种情况水平方向合并、垂直方向合并
        int width = 0, height = 0;
        for (int i = 0; i < tex.Length; i++)
        {
            if (isHorizontal == false)
            {
                //新图的高度
                height += tex[i].height;
                if (i > 0)
                {
                    //新图的宽度,这里筛选为最宽
                    if (tex[i].width > tex[i - 1].width)
                    {
                        width = tex[i].width;
                    }
                }
                else width = tex[i].width; //只有一张图
            }
            else
            {
                //新图的宽度
                width += tex[i].width;
                if (i > 0)
                {
                    //新图的高度,这里筛选为最高
                    if (tex[i].height > tex[i - 1].height)
                    {
                        height = tex[i].height;
                    }
                }
                else height = tex[i].height; //只有一张图
            }
        }
        //初始Texture2D
        Texture2D texture2D = new Texture2D(width, height);
        int x = 0, y = 0;
        for (int i = 0; i < tex.Length; i++)
        {
            //取图
            Color32[] color = tex[i].GetPixels32(0);

            //赋给新图
            if (i > 0)
            {
                if (isHorizontal == false)
                {
                    texture2D.SetPixels32(x, y += tex[i - 1].height, tex[i].width, tex[i].height, color); //高度
                }
                else
                {
                    texture2D.SetPixels32(x += tex[i - 1].width, y, tex[i].width, tex[i].height, color); //宽度
                }
            }
            else
            {
                texture2D.SetPixels32(x, y, tex[i].width, tex[i].height, color);
            }
        }
        //应用
        texture2D.Apply();
        return texture2D;
    }
    #endregion
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ke-Di

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

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

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

打赏作者

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

抵扣说明:

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

余额充值