Unity屏幕截图、区域截图、读取图片、WebGL长截屏并下载到本地jpg
一、全屏截图并保存到StreamingAssets路径下
Texture2D screenShot;//保存截取的纹理publicImage image;//显示截屏的ImagepublicvoidJietu(){StartCoroutine(ScrrenCapture(newRect(0,0, Screen.width, Screen.height),0));}IEnumeratorScrrenCapture(Rect rect,int a){
screenShot =newTexture2D((int)rect.width,(int)rect.height, TextureFormat.RGB24,false);yieldreturnnewWaitForEndOfFrame();
screenShot.ReadPixels(rect,0,0);
screenShot.Apply();yieldreturnnewWaitForSeconds(0.1f);Sprite sp = Sprite.Create(screenShot,newRect(0,0, screenShot.width, screenShot.height),newVector2(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;//保存截取的纹理publicImage image;//显示截屏的ImagepublicImage im;Texture2D texture2ds;//存储的截图publicvoidJietu(){StartCoroutine(getScreenTexture(im.rectTransform));}publicIEnumeratorgetScreenTexture(RectTransform rectT){yieldreturnnewWaitForEndOfFrame();
texture2ds =newTexture2D((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 =newRect(x, y, rectT.rect.width, rectT.rect.height);
texture2ds.ReadPixels(position,0,0,true);//按照设定区域读取像素;注意是以左下角为原点读取
texture2ds.Apply();Sprite sp = Sprite.Create(texture2ds,newRect(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文件
usingSystem.Collections;usingSystem.IO;usingSystem.Runtime.InteropServices;usingUnityEngine;usingUnityEngine.UI;/// <summary>/// unity发布WebGL屏幕长截屏并通过浏览器下载到本地jpg文件/// </summary>publicclassScreenshotArea:MonoBehaviour{[Header("截图区域")]publicRectTransform screenshot_area;[Header("滚动条")]publicScrollbar scrollbar;[Header("截图数量")]publicint number_max;[Header("每次截图滑动条vaule的位置,从下往上记")]publicfloat[] number;[Header("是否横向合并")]publicbool isHorizontal;Texture2D[] texture2ds;//存储的截图Texture2D merge_image;//合并后的图片string image_name="测试";//下载后图片的名字publicRectTransform screenshot_area1;publicRectTransform screenshot_area2;privatevoidStart(){
texture2ds =newTexture2D[number_max];}publicvoid OnClick_调用(){
Screen.fullScreen =true;StartCoroutine(getScreenTexture(screenshot_area));}#region 屏幕多次截图publicIEnumeratorgetScreenTexture(RectTransform rectT){
scrollbar.value= number[0];yieldreturnnewWaitForEndOfFrame();for(int i =0; i < number_max; i++){
texture2ds[i]=newTexture2D((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 =newRect(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;}yieldreturnnewWaitForEndOfFrame();}
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;privatevoidDownLoad(Texture2D screenShot){
sprite = Sprite.Create(screenShot,newRect(0,0, screenShot.width, screenShot.height),newVector2(0.5f,0.5f));byte[] photoByte =getImageSprite();//获取jpeg图像的字节流if(photoByte !=null){DownloadImage(photoByte, image_name+".jpg");}else{
Debug.LogError("<color=red>下载失败</color>");}}privatebyte[]getImageSprite(){if(sprite){return sprite.texture.EncodeToJPG();}returnnull;}#endregion#region 调用js方法下载[DllImport("__Internal")]privatestaticexternvoidImageDownloader(string str,string fn);publicvoidDownloadImage(byte[] imageData,string imageFileName ="newpic"){#ifUNITY_EDITOR
Debug.Log("<color=blue>编辑器无法下载</color>");#elseif(imageData !=null){
Debug.Log("Downloading..."+ imageFileName);ImageDownloader(System.Convert.ToBase64String(imageData), imageFileName);}#endif}#endregion#region 合并多张图片publicTexture2DMergeImage(Texture2D[] tex){if(tex.Length ==0)returnnull;//定义新图的宽高, 合并分为两种情况水平方向合并、垂直方向合并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 =newTexture2D(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}
本文转载自: https://blog.csdn.net/w091253/article/details/139000487
版权归原作者 Ke-Di 所有, 如有侵权,请联系我们删除。
版权归原作者 Ke-Di 所有, 如有侵权,请联系我们删除。