快过新年了,一直在加班赶项目,没时间写博客,今天上班最后一天,就休息过年了,将我强几天在做一个截图功能分享出来,网上查了很多,但是都是在Unity Editor下好使,能截图,并显示出来,但是,在Android下,截图成功,并能显示出来,但是就是存不到手机相册中,找了很多原因不知道怎么回事,查阅各种资料最终解决了。我总结了一下,我用过的方法,希望大家 能够用的上。
第一种方法:
使用Application类下的CaptureScreenshot方法。但是我觉得这并不好用,不随意。不能针对某一个相机(camera)的画面,进行截图。对局部画面截图,实现起来不方便,效率也低。
using System.Windows.Forms; //截图保存在电脑C盘中,安卓的我没想出用这中方法截图怎么保存到手机 if (Input .GetMouseButtonDown (0)) { SaveFileDialog save = new SaveFileDialog(); save.InitialDirectory = "c:\\"; save.Filter = "Image Files(*.JPG;*.BMP;*.PNG)|*.JPG;*.BMP;*.PNG|All files (*.*)|*.*"; DialogResult result = save.ShowDialog(); if (result == DialogResult.OK) { string path = save.FileName; //EditorUtility.SaveFilePanel也可以实现保存,但是只能在Unity Editor下运行。 Application.CaptureScreenshot(path); } }
Android下, Application.CaptureScreenshot(); 截图保存在沙河中,大家可以看一看 ,讲的很清楚: http://jingpin.jikexueyuan.com/article/30470.html
第二种方法:采用的是new Texture2D来截取图片分为截取一部分和全屏;
//截取一块区域,通过鼠标点击的点来获取要截取的区域; using UnityEngine; using System.Collections; using System.IO; public class ShotScreenPicture : MonoBehaviour { Texture2D p_w_picpath; Texture2D cutImage; WWW www; Rect rect; float time; Vector2 pos1; Vector2 pos2; // Update is called once per frame void Update() { //点击鼠标左键,记录第一个位置 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; } } void OnGUI() { //当下载完成 if (www.isDone) { GUI.DrawTexture(new Rect(0, 0, 600, 904), p_w_picpath); } GUI.Button(new Rect(0, 0, 100, 50), "W" + Screen.width + "H" + Screen.height); if (pos1 != null) { GUI.Button(new Rect(0, 50, 150, 50), pos1.ToString()); } if (pos2 != null) { GUI.Button(new Rect(0, 100, 150, 50), pos2.ToString()); } if (cutImage != null) { GUI.Button(new Rect(0, 150, 150, 50), "p_w_picpath W" + cutImage.width + "H" + cutImage.height); } if (rect != null) { GUI.Button(new Rect(0, 200, 250, 50), rect.ToString()); } } //截图 IEnumerator CutImage() { //图片大小 cutImage = new Texture2D((int)(pos2.x - pos1.x), (int)(pos1.y - pos2.y), TextureFormat.RGB24, true); //坐标左下角为0 rect = new Rect((int)pos1.x, Screen.height - (int)(Screen.height - pos2.y), (int)(pos2.x - pos1.x), (int)(pos1.y - pos2.y)); yield return new WaitForEndOfFrame(); cutImage.ReadPixels(rect, 0, 0, true); cutImage.Apply(); yield return cutImage; byte[] byt = cutImage.EncodeToPNG(); //保存截图 //如果是Andriod平台,可以把Application.streamingAssetsPath换成destination = "/sdcard/DCIM/Camera"; File.WriteAllBytes(Application.streamingAssetsPath + "/CutImage.png", byt); } }
//全屏截图 //存储路径 private string Path_save; //读取路径 private string Path_read; private string filepath; private string destination; void Start() { filepath = Application.persistentDataPath + "/test.txt"; } public void OnClickShot() { StartCoroutine(getTexture2d()); } IEnumerator getTexture2d() { //隐藏UI ................................. //截图操作 yield return new WaitForSeconds(0.1f); Texture2D t = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); //显示UI ....................... t.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true); byte[] bytes = t.EncodeToPNG(); t.Compress(true); t.Apply(); img.texture = t; //t就是截到的图片我们可以在这里上传到服务器 //下面是开始保存 //获取系统时间 System.DateTime now = new System.DateTime(); now = System.DateTime.Now; string filename = string.Format("p_w_picpath{0}{1}{2}{3}.png", now.Day, now.Hour, now.Minute, now.Second); //记录每一个截图名字 StreamWriter sw; FileInfo ft = new FileInfo(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) { string origin = 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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。