快过新年了,一直在加班赶项目,没时间写博客,今天上班最后一天,就休息过年了,将我强几天在做一个截图功能分享出来,网上查了很多,但是都是在Unity Editor下好使,能截图,并显示出来,但是,在Android下,截图成功,并能显示出来,但是就是存不到手机相册中,找了很多原因不知道怎么回事,查阅各种资料最终解决了。我总结了一下,我用过的方法,希望大家 能够用的上。


第一种方法:

使用Application类下的CaptureScreenshot方法。但是我觉得这并不好用,不随意。不能针对某一个相机(camera)的画面,进行截图。对局部画面截图,实现起来不方便,效率也低。

usingSystem.Windows.Forms;//截图保存在电脑C盘中,安卓的我没想出用这中方法截图怎么保存到手机if(Input.GetMouseButtonDown(0)){SaveFileDialogsave=newSaveFileDialog();save.InitialDirectory="c:\\";save.Filter="ImageFiles(*.JPG;*.BMP;*.PNG)|*.JPG;*.BMP;*.PNG|Allfiles(*.*)|*.*";DialogResultresult=save.ShowDialog();if(result==DialogResult.OK){stringpath=save.FileName;//EditorUtility.SaveFilePanel也可以实现保存,但是只能在UnityEditor下运行。Application.CaptureScreenshot(path);}}

Android下,Application.CaptureScreenshot();截图保存在沙河中,大家可以看一看 ,讲的很清楚: http://jingpin.jikexueyuan.com/article/30470.html


第二种方法:采用的是new Texture2D来截取图片分为截取一部分和全屏;

//截取一块区域,通过鼠标点击的点来获取要截取的区域;usingUnityEngine;usingSystem.Collections;usingSystem.IO;publicclassShotScreenPicture:MonoBehaviour{Texture2Dp_w_picpath;Texture2DcutImage;WWWwww;Rectrect;floattime;Vector2pos1;Vector2pos2;//UpdateiscalledonceperframevoidUpdate(){//点击鼠标左键,记录第一个位置if(Input.GetMouseButtonDown(0)){pos1=Input.mousePosition;time=Time.time;if(time>1f){Debug.Log(pos1);}}//放开左键记录第二个位置if(Input.GetMouseButtonUp(0)){pos2=Input.mousePosition;Debug.Log(pos2);StartCoroutine(CutImage());time=0;}}voidOnGUI(){//当下载完成if(www.isDone){GUI.DrawTexture(newRect(0,0,600,904),p_w_picpath);}GUI.Button(newRect(0,0,100,50),"W"+Screen.width+"H"+Screen.height);if(pos1!=null){GUI.Button(newRect(0,50,150,50),pos1.ToString());}if(pos2!=null){GUI.Button(newRect(0,100,150,50),pos2.ToString());}if(cutImage!=null){GUI.Button(newRect(0,150,150,50),"p_w_picpathW"+cutImage.width+"H"+cutImage.height);}if(rect!=null){GUI.Button(newRect(0,200,250,50),rect.ToString());}}//截图IEnumeratorCutImage(){//图片大小cutImage=newTexture2D((int)(pos2.x-pos1.x),(int)(pos1.y-pos2.y),TextureFormat.RGB24,true);//坐标左下角为0rect=newRect((int)pos1.x,Screen.height-(int)(Screen.height-pos2.y),(int)(pos2.x-pos1.x),(int)(pos1.y-pos2.y));yieldreturnnewWaitForEndOfFrame();cutImage.ReadPixels(rect,0,0,true);cutImage.Apply();yieldreturncutImage;byte[]byt=cutImage.EncodeToPNG();//保存截图//如果是Andriod平台,可以把Application.streamingAssetsPath换成destination="/sdcard/DCIM/Camera";File.WriteAllBytes(Application.streamingAssetsPath+"/CutImage.png",byt);}}

//全屏截图//存储路径privatestringPath_save;//读取路径privatestringPath_read;privatestringfilepath;privatestringdestination;voidStart(){filepath=Application.persistentDataPath+"/test.txt";}publicvoidOnClickShot(){StartCoroutine(getTexture2d());}IEnumeratorgetTexture2d(){//隐藏UI.................................//截图操作yieldreturnnewWaitForSeconds(0.1f);Texture2Dt=newTexture2D(Screen.width,Screen.height,TextureFormat.RGB24,false);//显示UI.......................t.ReadPixels(newRect(0,0,Screen.width,Screen.height),0,0,true);byte[]bytes=t.EncodeToPNG();t.Compress(true);t.Apply();img.texture=t;//t就是截到的图片我们可以在这里上传到服务器//下面是开始保存//获取系统时间System.DateTimenow=newSystem.DateTime();now=System.DateTime.Now;stringfilename=string.Format("p_w_picpath{0}{1}{2}{3}.png",now.Day,now.Hour,now.Minute,now.Second);//记录每一个截图名字StreamWritersw;FileInfoft=newFileInfo(filepath);la[1].text=filename;if(!ft.Exists){sw=ft.CreateText();}else{sw=ft.AppendText();}sw.WriteLine(filename);sw.Close();sw.Dispose();//应用平台判断,路径选择if(Application.platform==RuntimePlatform.Android){stringorigin=Path_save;//保存在Android相册中,如果是PC就改成Application.dataPath的路径destination="/sdcard/DCIM/Camera";if(!Directory.Exists(destination)){Directory.CreateDirectory(destination);}destination=destination+"/"+filename;Path_save=destination;}//保存文件File.WriteAllBytes(Path_save,bytes);}


第三种方法:有一个截图插件,在附件即可下载;

snapShot.CaptureAndSaveToAlbum();

即可截图成功,包含了选区域和全屏截图。


【注意】第二种和第三种保存在Android下的手机相册中,都要在发布的时候改成安卓默认路径


最后再给一个调安卓截图的功能(在搜索的时候看到的,觉得挺好的,分享给大家)



http://www.360doc.com/content/16/0128/23/21062130_531360104.shtml

http://www.ceeger.com/forum/read.php?tid=15252&page=1