Unity3D随意截图并保存
生活随笔
收集整理的這篇文章主要介紹了
Unity3D随意截图并保存
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
效果
代碼
using UnityEngine; using System.Collections; using System.IO;public class CropPicture : MonoBehaviour {string localPath = "http://192.168.1.100:8080/picture/15.jpg";Texture2D image;Texture2D cutImage;WWW www;Rect rect;float time;Vector2 pos1;Vector2 pos2;// Use this for initializationvoid Start(){StartCoroutine(LoadImage());}// Update is called once per framevoid 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), image);}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), "image W" + cutImage.width + "H" + cutImage.height);}if (rect != null){GUI.Button(new Rect(0, 200, 250, 50), rect.ToString());}}//下載圖片IEnumerator LoadImage(){www = new WWW(localPath);yield return www;image = www.texture;if (www.error != null){Debug.Log(www.error);}}//截圖IEnumerator CutImage(){//圖片大小cutImage = new Texture2D((int)(pos2.x - pos1.x), (int)(pos1.y - pos2.y), TextureFormat.RGB24, true);//坐標左下角為0rect = 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();//保存截圖Project面板下要創(chuàng)建StreamingAssets文件夾,保存文件后要刷新Project面板圖片才會顯示出來File.WriteAllBytes(Application.streamingAssetsPath + "/CutImage.png", byt);} }2016.5.7.17.34修改后的代碼
using UnityEngine; using System.Collections; using System.IO; using System;public class CropPicture : MonoBehaviour {string localPath = "http://192.168.1.100:8080/picture/15.jpg";Texture2D image;Texture2D cutImage;WWW www;Rect rect;float time;Vector2 pos1;Vector2 pos2;// Use this for initializationvoid Start(){StartCoroutine(LoadImage());}// Update is called once per framevoid 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), image);}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), "image W" + cutImage.width + "H" + cutImage.height);}if (rect != null){GUI.Button(new Rect(0, 200, 250, 50), rect.ToString());}}//下載圖片IEnumerator LoadImage(){www = new WWW(localPath);yield return www;image = www.texture;if (www.error != null){Debug.Log(www.error);}}//截圖IEnumerator CutImage(){//圖片大小cutImage = new Texture2D((int)(pos2.x - pos1.x), (int)(pos1.y - pos2.y), TextureFormat.RGB24, true);//坐標左下角為0rect = 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();//保存截圖File.WriteAllBytes(Application.dataPath+ "/"+DateTime.Now.ToFileTime().ToString()+".png", byt);} }總結
以上是生活随笔為你收集整理的Unity3D随意截图并保存的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: squeeze()和unsqueeze(
- 下一篇: 搜索引擎优化SEO工具集合[转]