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

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

一、全屏截图并保存到StreamingAssets路径下
   Texture2D screenShot;//保存截取的纹理public Image image;  //显示截屏的Imagepublic 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;//保存到streamingAssetsbyte[] bytes = screenShot.EncodeToJPG();string filename = Application.streamingAssetsPath + "/Images/Screenshot" + DateTime.UtcNow.Ticks + ".png";File.WriteAllBytes(filename, bytes);}
二、区域截图并保存到StreamingAssets路径下
  Texture2D screenShot;//保存截取的纹理public Image image;  //显示截屏的Imagepublic 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;//保存到streamingAssetsbyte[] 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_EDITORbyte[] bytes = merge_image.EncodeToJPG();string filename = Application.streamingAssetsPath + "/Screenshot" + UnityEngine.Random.Range(0, 1000) + ".png";File.WriteAllBytes(filename, bytes);
#endifDownLoad(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_EDITORDebug.Log("<color=blue>编辑器无法下载</color>");
#elseif (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; //只有一张图}}//初始Texture2DTexture2D 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
}
四、调用js方法下载图片

在Plugins文件夹下新建

ImageDownloader.jslib

放入下面代码

var ImageDownloaderPlugin = {ImageDownloader: function (str, fn) {console.log("start jslib download");var msg = UTF8ToString(str);var fname = UTF8ToString(fn);var contentType = 'image/jpeg';function fixBinary(bin) {var length = bin.length;var buf = new ArrayBuffer(length);var arr = new Uint8Array(buf);for (var i = 0; i < length; i++) {arr[i] = bin.charCodeAt(i);}return buf;}var binary = fixBinary(atob(msg));var data = new Blob([binary], { type: contentType });var link = document.createElement('a');link.download = fname;link.innerHTML = 'DownloadFile';link.setAttribute('id', 'ImageDownloaderLink');link.href = window.URL.createObjectURL(data);link.onclick = function () {var child = document.getElementById('ImageDownloaderLink');child.parentNode.removeChild(child);};link.style.display = 'none';document.body.appendChild(link);link.click();window.URL.revokeObjectURL(link.href);}
};
mergeInto(LibraryManager.library, ImageDownloaderPlugin)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/63970.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用 ADB (Android Debug Bridge) 工具来截取 Android 设备的屏幕截图

可以使用 ADB (Android Debug Bridge) 工具来截取 Android 设备的屏幕截图。以下是具体的操作步骤&#xff1a; 1. 连接设备 确保 Android 设备通过 USB 或网络连接到电脑&#xff0c;并运行以下命令检查连接状态&#xff1a; adb devices2. 截取屏幕截图 运行以下命令将设…

TypeScript 的崛起:全面解析与深度洞察

一、背景与起源 &#xff08;一&#xff09;JavaScript 的局限性 类型系统缺失 难以在编码阶段发现类型相关错误&#xff0c;导致运行时错误频发。例如&#xff0c;将字符串误当作数字进行数学运算&#xff0c;可能在运行时才暴露问题。函数参数类型不明确&#xff0c;容易传入…

Moretl无人值守日志采集工具

永久免费: 至Gitee下载 使用教程: Moretl使用说明 用途 定时全量或增量采集工控机,电脑文件或日志. 优势 开箱即用: 解压直接运行.不需额外下载.管理设备: 后台统一管理客户端.无人值守: 客户端自启动,自更新.稳定安全: 架构简单,兼容性好,通过授权控制访问. 架构 技术架…

The Rise and Potential of Large Language ModelBased Agents:A Survey---摘要、背景、引言

题目 基于大语言模型的Agent的兴起与发展前景 论文地址&#xff1a;https://arxiv.org/pdf/2309.07864.pdf 项目地址&#xff1a;https:/github.com/WooooDyy./LLM-Agent–Paper-List 摘要 长期以来&#xff0c;人类一直在追求等同于或超越人类水平的人工智能(A)&#xff0c;…

lc46全排列——回溯

46. 全排列 - 力扣&#xff08;LeetCode&#xff09; 法1&#xff1a;暴力枚举 总共n!种全排列&#xff0c;一一列举出来放入list就行&#xff0c;关键是怎么去枚举呢&#xff1f;那就每次随机取一个&#xff0c;然后删去这个&#xff0c;再从剩下的数组中继续去随机选一个&a…

题目 1761: 学习ASCII码

题目 1761: 学习ASCII码 时间限制: 2s 内存限制: 192MB 提交: 4331 解决: 2415 题目描述 刚开始学C语言&#xff0c;ASCII码可是必须要会的哦&#xff01;那么问题来了&#xff0c;要求你用熟悉的printf输出字符常量 ’ t ’ 的ASCII以及ASCII码值63对应的字符&#xff01; 注…

使用Flink CDC实现 Oracle数据库数据同步的oracle配置操作

使用Flink CDC实现 Oracle数据库数据同步的oracle配置操作&#xff0c;包括开启日志归档和用户授权。 flink官方参考资料&#xff1a; https://nightlies.apache.org/flink/flink-cdc-docs-master/zh/docs/connectors/flink-sources/oracle-cdc/ 操作步骤&#xff1a; 1.启用…

字体子集化实践探索

最近项目rust生成PDF组件printpdf需要内嵌完整字体导致生成的PDF很大&#xff0c;需要做压缩&#xff0c;但是rust的类库allsorts::subset::subset不支持windows&#xff0c;所以做了一些windows下字体子集化的尝试 方案一&#xff1a;node.js做子集化 fontmin 缺点是也需要集…

Spring Boot教程之二十五: 使用 Tomcat 部署项目

Spring Boot – 使用 Tomcat 部署项目 Spring Boot 是一个基于微服务的框架&#xff0c;在其中创建可用于生产的应用程序只需很少的时间。Spring Boot 建立在 Spring 之上&#xff0c;包含 Spring 的所有功能。如今&#xff0c;它正成为开发人员的最爱&#xff0c;因为它是一个…

Vue自定义快捷键做粘贴

静态&#xff1a; export default {data() {return {customContent: 这里是你想要粘贴的自定义内容 // 自定义内容};},mounted() {window.addEventListener(keydown, this.handleKeyDown);},beforeDestroy() {window.removeEventListener(keydown, this.handleKeyDown);},meth…

【C语言篇】C 语言总复习(下):点亮编程思维,穿越代码的浩瀚星河

我的个人主页 我的专栏&#xff1a;C语言&#xff0c;希望能帮助到大家&#xff01;&#xff01;&#xff01;点赞❤ 收藏❤ 在C语言的世界里&#xff0c;结构体和联合体以及文件操作都是非常重要且实用的知识板块&#xff0c;掌握它们能帮助我们更高效地组织数据以及与外部文…

CNCF云原生生态版图-项目和产品综合分析

CNCF云原生生态版图-项目和产品综合分析 CNCF云原生生态版图-项目和产品综合分析整体统计分析中国研发人员贡献项目和产品其中&#xff0c;纳入 CNCF 管理的开源项目 链接 CNCF云原生生态版图-项目和产品综合分析 整体统计分析 在对云原生技术选型时&#xff0c;优先选择经过 …

【vue2】文本自动省略组件,支持单行和多行省略,超出显示tooltip

代码见文末 vue3实现 最开始就用的vue3实现&#xff0c;如下 Vue3实现方式 vue2开发和使用文档 组件功能 TooltipText 是一个文字展示组件&#xff0c;具有以下功能&#xff1a; 文本显示&#xff1a;支持单行和多行文本显示。自动判断溢出&#xff1a;判断文本是否溢出…

MetaGPT源码 (ContextMixin 类)

目录 理解 ContextMixin什么是 ContextMixin&#xff1f;主要组件实现细节 测试 ContextMixin示例&#xff1a;ModelX1. 配置优先级2. 多继承3. 多继承重写4. 配置优先级 在本文中&#xff0c;我们将探索 ContextMixin 类&#xff0c;它在多重继承场景中的集成及其在 Python 配…

VScode、Windsurf、Cursor 中 R 语言相关快捷键设置

前言 在生物信息学数据分析中&#xff0c;R语言是一个不可或缺的工具。为了提高R语言编程效率&#xff0c;合理设置快捷键显得尤为重要。本文介绍在VSCode Windsurf Cursor 中一些实用的R语言快捷键设置&#xff0c;让非 Rstudio 的 IDE 用起来得心应手&#x1f611; 操作种…

分布式任务调度平台xxl-job源码学习

XXL-JOB是一个分布式任务调度平台&#xff0c;其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码并接入多家公司线上产品线&#xff0c;开箱即用。 官网&#xff1a;https://www.xuxueli.com/xxl-job/ github&#xff1a;https://github.com/xuxueli/xxl-…

Macbookpro M1 IDEA中安装mysql

一&#xff1a;安装与连接数据库 1. 首先在mysql中创建一个初始数据库&#xff1a;idea_db&#xff0c;如示&#xff1a; 2.打开IDEA,如果最右侧没有database窗口&#xff0c;则在插件那里下载“Database navigator”,稍后重启一下即可&#xff1b; 点击最右侧Database---->…

leetcode 3264. K 次乘运算后的最终数组 I 简单

给你一个整数数组 nums &#xff0c;一个整数 k 和一个整数 multiplier 。 你需要对 nums 执行 k 次操作&#xff0c;每次操作中&#xff1a; 找到 nums 中的 最小 值 x &#xff0c;如果存在多个最小值&#xff0c;选择最 前面 的一个。将 x 替换为 x * multiplier 。 请你…

根据契约进行分析--录像店案例研究01

Richard Mitchell 著&#xff0c;zhen_lei 译 本文包括录像店案例研究的一些片段&#xff0c;用来说明根据契约进行分析的原理。本文假定读者已经从其它渠道学习了一些关于根据契约进行分析的方法。 完整的一套模型可以写成一本书。这些选择的片段用来说明开发的某些方面&…

Linux内核结构及源码概述

参考&#xff1a;深入分析LINUX内核源码 深入分析Linux内核源码 (kerneltravel.net) Linux 是一个庞大、高效而复杂的操作系统&#xff0c;虽然它的开发起始于 Linus Torvalds 一个人&#xff0c;但随着时间的推移&#xff0c;越来越多的人加入了 Linux 的开发和对它的不断完善…