日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity3D随意截图并保存

發布時間:2024/1/18 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity3D随意截图并保存 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

效果


代碼

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面板下要創建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随意截图并保存的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。