Unity三种截屏方法(非自带API)
者利用了三种表现形式:
1,选择截图路径的方法
2,直接截取截屏的方法
3,截取鼠标圈选区域。
上代码,:
第一种是调用.net的类库,需要引用System.Windows.Forms.dll,在Assents文件夹里新建Plugins文件夹将System.Windows.Forms.dll放入其中。
在头部添加
using System.Windows.Forms;
public enum Method { ChooseFileDialog,GetAllScreen,MouseChoose};
public Method GetMethod ;
Vector2 pos1;
Vector2 pos2;
float time;
bool isShowRect = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (GetMethod == Method.ChooseFileDialog)
{
Camera.main.GetComponent<DrawRect>().enabled = false;
if (Input.GetKeyDown(KeyCode.X))
{
SaveFileDialog saveLog = new SaveFileDialog();
saveLog.InitialDirectory = UnityEngine.Application.dataPath;
saveLog.Filter = "Image Files(*.JPG;*.BMP;*.PNG)|*.JPG;*.BMP;*.PNG|All files (*.*)|*.*";
DialogResult result = saveLog.ShowDialog();
if (result == DialogResult.OK)
{
string path = saveLog.FileName;
StartCoroutine(fnGetScreen(path));
//UnityEngine.Application.CaptureScreenshot(path);
}
}
}
else if (GetMethod == Method.GetAllScreen)
{
Camera.main.GetComponent<DrawRect>().enabled = false;
if (Input.GetKeyDown(KeyCode.X))
{
StartCoroutine(fnGetScreen());
}
}
else if(GetMethod == Method.MouseChoose)
{
Camera.main.GetComponent<DrawRect>().enabled = true;
if (Input.GetMouseButtonDown(0))
{
pos1 = Input.mousePosition;
print(pos1);
}
if (Input.GetMouseButtonUp(0))
{
pos2 = Input.mousePosition;
print(pos2);
if (pos1 != pos2)
{
if (Vector3.Distance(pos1, pos2) > 2f)
{
StartCoroutine(GetCapture());
}
}
}
}
}
IEnumerator GetCapture()
{
yield return new WaitForEndOfFrame();
Texture2D tex = new Texture2D((int)Mathf.Abs(pos2.x-pos1.x), (int)Mathf.Abs(pos1.y-pos2.y), TextureFormat.RGB24, false);
Rect rect = new Rect(Mathf.Min((int)pos1.x,(int)pos2.x),Mathf.Min((int)pos1.y, (int)pos2.y), (int)Mathf.Abs(pos2.x - pos1.x), (int)Mathf.Abs(pos1.y - pos2.y));
tex.ReadPixels(rect, 0, 0, true);
tex.Apply();
byte[] imagebytes = tex.EncodeToPNG();//转化为png图
tex.Compress(false);//对屏幕缓存进行压缩
//image.mainTexture = tex;//对屏幕缓存进行显示(缩略图)
File.WriteAllBytes(UnityEngine.Application.dataPath + "/ScreenShot/screencapture.png", imagebytes);//存储png图
}
IEnumerator fnGetScreen()
{
yield return new WaitForEndOfFrame();
Texture2D tex2 = new Texture2D(UnityEngine.Screen.width, UnityEngine.Screen.height, TextureFormat.RGB24, false);
Rect rect2 = new Rect(0, 0, UnityEngine.Screen.width, UnityEngine.Screen.height);
tex2.ReadPixels(rect2, 0, 0, true);
tex2.Apply();
byte[] imagebytes2 = tex2.EncodeToPNG();
tex2.Compress(false);
File.WriteAllBytes(UnityEngine.Application.dataPath + "/ScreenShot/screen.png", imagebytes2);//存储png图
}
IEnumerator fnGetScreen(string s)
{
yield return new WaitForEndOfFrame();
Texture2D tex2 = new Texture2D(UnityEngine.Screen.width, UnityEngine.Screen.height, TextureFormat.RGB24, false);
Rect rect2 = new Rect(0, 0, UnityEngine.Screen.width, UnityEngine.Screen.height);
tex2.ReadPixels(rect2, 0, 0, true);
tex2.Apply();
byte[] imagebytes2 = tex2.EncodeToPNG();
tex2.Compress(false);
File.WriteAllBytes(s, imagebytes2);//存储png图
}
脚本贴到Maincamera上,然后新建一个DrawRect脚本,将脚本也贴到MainCamera上。
public class DrawRect : MonoBehaviour {
private Vector2 mMouseStart, mMouseEnd;
private bool mBDrawMouseRect;
private Material rectMat = null;//画线的材质 不设定系统会用当前材质画线 结果不可控
void Start()
{
mBDrawMouseRect = false;
rectMat = new Material("Shader \"Lines/Colored Blended\" {" +
"SubShader { Pass { " +
" Blend SrcAlpha OneMinusSrcAlpha " +
" ZWrite Off Cull Off Fog { Mode Off } " +
" BindChannels {" +
" Bind \"vertex\", vertex Bind \"color\", color }" +
"} } }");//生成画线的材质
rectMat.hideFlags = HideFlags.HideAndDontSave;
rectMat.shader.hideFlags = HideFlags.HideAndDontSave;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
//按下鼠标左键
{
Vector3 mousePosition = Input.mousePosition;
mMouseStart = new Vector2(mousePosition.x, mousePosition.y);
}
if (Input.GetMouseButton(0))
//持续按下鼠标左键
{
mBDrawMouseRect = true;
Vector3 mousePosition = Input.mousePosition;
mMouseEnd = new Vector2(mousePosition.x, mousePosition.y);
}
if (Input.GetMouseButtonUp(0))
{
mBDrawMouseRect = false;
}
}
void OnGUI()
{
if (mBDrawMouseRect)
Draw(mMouseStart, mMouseEnd);
}
//渲染2D框
void Draw(Vector2 start, Vector2 end)
{
rectMat.SetPass(0);
GL.PushMatrix();//保存摄像机变换矩阵
Color clr = Color.green;
clr.a = 0.1f;
GL.LoadPixelMatrix();//设置用屏幕坐标绘图
//透明框
GL.Begin(GL.QUADS);
GL.Color(clr);
GL.Vertex3(start.x, start.y, 0);
GL.Vertex3(end.x, start.y, 0);
GL.Vertex3(end.x, end.y, 0);
GL.Vertex3(start.x, end.y, 0);
GL.End();
//线
//上
GL.Begin(GL.LINES);
GL.Color(Color.green);
GL.Vertex3(start.x, start.y, 0);
GL.Vertex3(end.x, start.y, 0);
GL.End();
//下
GL.Begin(GL.LINES);
GL.Color(Color.green);
GL.Vertex3(start.x, end.y, 0);
GL.Vertex3(end.x, end.y, 0);
GL.End();
//左
GL.Begin(GL.LINES);
GL.Color(Color.green);
GL.Vertex3(start.x, start.y, 0);
GL.Vertex3(start.x, end.y, 0);
GL.End();
//右
GL.Begin(GL.LINES);
GL.Color(Color.green);
GL.Vertex3(end.x, start.y, 0);
GL.Vertex3(end.x, end.y, 0);
GL.End();
GL.PopMatrix();//还原
}
}
Unity三种截屏方法(非自带API)的更多相关文章
- Windows的三种截屏方法
「发表于知乎」戳
- IOS开发-几种截屏方法
IOS开发-几种截屏方法 1. UIGraphicsBeginImageContextWithOptions(pageView.page.bounds.size, YES, zoomSc ...
- Android开发笔记:安卓程序截屏方法
1,基于Android SDK的截屏方法 (1)主要就是利用SDK提供的View.getDrawingCache()方法.网上已经有很多的实例了.首先创建一个android project,然后进行L ...
- .Net MVC 导入导出Excel总结(三种导出Excel方法,一种导入Excel方法) 通过MVC控制器导出导入Excel文件(可用于java SSH架构)
.Net MVC 导入导出Excel总结(三种导出Excel方法,一种导入Excel方法) [原文地址] 通过MVC控制器导出导入Excel文件(可用于java SSH架构) public cl ...
- Android系统的三种分屏显示模式
Google在Android 7.0中引入了一个新特性——多窗口支持,允许用户一次在屏幕上打开两个应用.在手持设备上,两个应用可以在"分屏"模式中左右并排或上下并排显示.在电视设备 ...
- JSON三种数据解析方法(转)
原 JSON三种数据解析方法 2018年01月15日 13:05:01 zhoujiang2012 阅读数:7896 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...
- iOS截屏方法
//获取屏幕截屏方法 - (UIImage *)capture { // 创建一个context UIGraphicsBeginImageContextWithOptions(self.view.bo ...
- 同源策略(same-origin policy)及三种跨域方法
同源策略(same-origin policy)及三种跨域方法 1.同源策略 含义: 同源是指文档的来源相同,主要包括三个方面 协议 主机 载入文档的URL端口 所以同源策略就是指脚本只能读取和所属文 ...
- Linux下截屏方法
参考百度经验 https://jingyan.baidu.com/article/48a42057c8e8dfa92525047c.html 第一种: 截屏部分画面并保存 快捷键Shift+PrtSc
随机推荐
- Bootstrap_Javascript_选项卡
选项卡Tabs是Web中一种非常常用的功能.用户点击或悬浮对应的菜单项,能切换出对应的内容. 一 . 结构分析 Bootstrap框架中的选项卡主要有两部分内容组成: 选项卡组件(也就是菜单组件),对 ...
- QQ空间API接口
(以下内容可能会随着时间改变而改变!) 查看对方QQ空间的背景音乐 http://qzone-music.qq.com/fcg-bin/cgi_playlist_xml.fcg?json=0& ...
- Device.js——检测设备平台、操作系统的Javascript 库
http://segmentfault.com/a/1190000000373735 Device.js 是一个可以让你检测设备的平台,操作系统和方向 JavaScript 库,它会自动在 <h ...
- python自动开发之第十二天
一.数据库的介绍 (1)由多张表组成(2)存取有规则,数据有关联(3)数据量大,被优化 好处:更有效的存取数据 二.关系型数据库管理系统(RDBMS) Oracle,Mysql,Sqlserver,D ...
- G - A+B for Input-Output Practice (VI)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description You ...
- block 和delegate的用法
//block 和delegate的用法 设置代理 #import <UIKit/UIKit.h> typedef void (^ASIHeadersBlock)(NSString *my ...
- java-web-j2e学习建议路线
JAVA学习之路(2) 首先要明白Java体系设计到得三个方面:J2SE,J2EE,J2ME(KJAVA).J2SE,Java 2 Platform Standard Edition,我们经常说 ...
- 最新iOS 6 in Xcode4.5新特性——Storyboard和属性自动绑定
最新iOS 6 in Xcode4.5新特性编程之二(上)——Storyboard和属性自动绑定 从Xcode 4.3开始,Storyboard 就是iOS 5和iOS 6中令人兴奋的一个新特性,他将 ...
- VS自带的功能:性能和诊断
先看一眼代码: using System; using System.Collections.Generic; using XCode; using XCode.Configuration; usin ...
- GNU PGM
Linux程序设计入门 - gpm gpm是Linux console下的滑鼠驱动程序,它主要提供文字模式下的滑鼠事件处 理.Linux下文字界面的滑鼠几乎都是用gpm来处理. gpm的文件在gpm原 ...