Unity3D随意截图并保存
来源:互联网 发布:as网络语言什么意思 编辑:程序博客网 时间:2024/05/21 10:01
效果
代码
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 initialization void Start() { StartCoroutine(LoadImage()); } // 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), 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); //坐标左下角为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(); //保存截图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 initialization void Start() { StartCoroutine(LoadImage()); } // 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), 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); //坐标左下角为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(); //保存截图 File.WriteAllBytes(Application.dataPath+ "/"+DateTime.Now.ToFileTime().ToString()+".png", byt); }}
- Unity3D随意截图并保存
- Unity3D随意截图并保存
- Unity3D截图并保存本地目录
- Unity3D截图并保存任意目录
- Unity3d 截图保存
- 屏幕截图并保存
- opencv 截图并保存
- 【iOS开发-截图】截图并保存相册
- Unity3D中截图并修改图片像素重新保存至本地
- opencv2视频截图并保存
- 截图并保存到相册
- Unity3D中Android截图保存相册
- Unity3D中Android截图保存相册
- 实现用例中随意截图并添加到报告中
- Learning opencv: 简单视频截图并保存
- android对View控件截图并保存
- android中截图并保存本地
- Java实现截图并保存到本地
- 单例模式
- android sqlite使用之模糊查询数据库数据的三种方式
- C# winform操作网页文件
- 二维数组参数传递方法
- QT实现图像处理-傅立叶变换、傅立叶反变换、平滑、锐化与模板匹配
- Unity3D随意截图并保存
- Linux操作系统学习_用户态与内核态之切换过程
- 2013-6-28-StreamWriter写入器
- xargs与cp结合使用
- 将Windows Server 2012改造成桌面操作系统(Windows 8)的方法
- oracle中如何根据一个字段名查找出所属的表名
- The Survey of Programming Language Classification
- 【windowSoftInputMode】android软键盘隐藏
- URL带有其他参数时,如何使用PHP的CI框架分页类?