网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Unity3D截图功能总结,并保存在指定的文件夹-创新互联

快过新年了,一直在加班赶项目,没时间写博客,今天上班最后一天,就休息过年了,将我强几天在做一个截图功能分享出来,网上查了很多,但是都是在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下的手机相册中,都要在发布的时候改成安卓默认路径

Unity3D截图功能总结,并保存在指定的文件夹

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

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

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

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻标题:Unity3D截图功能总结,并保存在指定的文件夹-创新互联
文章URL:http://bjjierui.cn/article/dhihes.html

其他资讯