Unity3D随意截图并保存
http://blog.csdn.net/awnuxcvbn/article/details/9199245
效果
代码
- <pre name="code" class="csharp">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();
- //保存截图
- File.WriteAllBytes(Application.streamingAssetsPath + "/CutImage.png", byt);
- }
- }
Unity3D随意截图并保存的更多相关文章
- Unity3d之截图方法
http://blog.csdn.net/highning0007/article/details/37991787 Unity3d之截图方法 分类: Unity3D2013-11-28 17:13 ...
- [Android] 拍照、截图、保存并显示在ImageView控件中
近期在做Android的项目,当中部分涉及到图像处理的内容.这里先讲述怎样调用Camera应用程序进行拍照,并截图和保存显示在ImageView控件中以及遇到的困难和解决方法. PS:作者购买 ...
- opencv 截图并保存
opencv 截图并保存(转载) 代码功能:选择图像中矩形区,按S键截图并保存,Q键退出. #include<opencv2/opencv.hpp> #include<iostrea ...
- html2canvas插件对整个网页或者网页某一部分截图并保存为图片
html2canvas能够实现在用户浏览器端直接对整个或部分页面进行截屏.这个脚本将当前页面渲染成一个canvas图片,通过读取DOM并将不同的样式应用到这些元素上实现.它不需要来自服务器任何渲染,整 ...
- 对html进行截图并保存为本地图片
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Python+Selenium练习篇之21-如何截图并保存
本文介绍如何利用Selenium的方法进行截图,在测试过程中,是有必要截图,特别是遇到错误的时候进行截图.在selenium for python中主要有三个截图方法,我们挑选其中最常用的一种. ge ...
- Python3.X Selenium 自动化测试中如何截图并保存成功
在selenium for python中主要有三个截图方法,我们挑选其中最常用的一种. 挑最常用的:get_screenshot_as_file() 相关代码如下:(下面的代码可直接复制) # co ...
- selenium 页面截图并保存
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org. ...
- 利用HTML5的Video进行视频截图并保存到本地
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
随机推荐
- 【BZOJ 3997】 3997: [TJOI2015]组合数学 (DP| 最小链覆盖=最大点独立集)
3997: [TJOI2015]组合数学 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 919 Solved: 664 Description 给出 ...
- 矩阵&行列式
# 代数 排列 对换,对于一个排列操作,对于一个偶排列一次对换之后变为奇排列 反之变为偶排列 行列式 N阶行列式室友N^2个数aij(i,j = 1,2,3,...n) 行列式的数=\(\sum_ { ...
- 「UOJ218」火车管理
「UOJ218」火车管理 解题思路:观察发现,在弹出 \(x\) 之前,它前面这个元素都是保持不变的,所以可以用一棵可持久化线段树维护每一个栈顶元素的插入时间,每次找到当前时间\(-1\) 的版本就可 ...
- (转载)打破某些大牛比较呵呵的MySQL无file权限读root hash的谣言
如题.比如乌云社区发帖的这位大牛http://zone.wooyun.org/content/12432 看那帖子标题就很喜感有木有,大概意思就是创建了一个没有file权限的账户test,然后不能lo ...
- 用ExifInterface读取经纬度的时候遇到的一个问题
如果读取图片经纬度,使用 String latValue = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE); String ln ...
- python输入输出入门 A+B
描述 求两个整数之和. 输入 输入数据只包括两个整数A和B. 输出 两个整数的和. 样例输入 1 2 样例输出 3 a=input().split() print(int(a[0])+int(a[1 ...
- Codeforces Round #346 (Div. 2) C. Tanya and Toys 贪心
C. Tanya and Toys 题目连接: http://www.codeforces.com/contest/659/problem/C Description In Berland recen ...
- CROC 2016 - Qualification C. Hostname Aliases map
C. Hostname Aliases 题目连接: http://www.codeforces.com/contest/644/problem/C Description There are some ...
- 利用DC/DC开关调节器延长DSP系统的电池寿命 - 动态电压调节
http://www.analog.com/zh/content/dc-to-dc_switching_regulator_insights/fca.html 作者:Sridhar Gurram,Ol ...
- C# 通过HttpWebRequest在后台对WebService进行调用
通过HttpWebRequest在后台对WebService进行调用 http://www.cnblogs.com/macroxu-1982/archive/2009/12/23/1630415.ht ...