者利用了三种表现形式:

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)的更多相关文章

  1. Windows的三种截屏方法

    「发表于知乎」戳

  2. IOS开发-几种截屏方法

    IOS开发-几种截屏方法 1.        UIGraphicsBeginImageContextWithOptions(pageView.page.bounds.size, YES, zoomSc ...

  3. Android开发笔记:安卓程序截屏方法

    1,基于Android SDK的截屏方法 (1)主要就是利用SDK提供的View.getDrawingCache()方法.网上已经有很多的实例了.首先创建一个android project,然后进行L ...

  4. .Net MVC 导入导出Excel总结(三种导出Excel方法,一种导入Excel方法) 通过MVC控制器导出导入Excel文件(可用于java SSH架构)

    .Net MVC  导入导出Excel总结(三种导出Excel方法,一种导入Excel方法) [原文地址] 通过MVC控制器导出导入Excel文件(可用于java SSH架构)   public cl ...

  5. Android系统的三种分屏显示模式

    Google在Android 7.0中引入了一个新特性——多窗口支持,允许用户一次在屏幕上打开两个应用.在手持设备上,两个应用可以在"分屏"模式中左右并排或上下并排显示.在电视设备 ...

  6. JSON三种数据解析方法(转)

    原 JSON三种数据解析方法 2018年01月15日 13:05:01 zhoujiang2012 阅读数:7896    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...

  7. iOS截屏方法

    //获取屏幕截屏方法 - (UIImage *)capture { // 创建一个context UIGraphicsBeginImageContextWithOptions(self.view.bo ...

  8. 同源策略(same-origin policy)及三种跨域方法

    同源策略(same-origin policy)及三种跨域方法 1.同源策略 含义: 同源是指文档的来源相同,主要包括三个方面 协议 主机 载入文档的URL端口 所以同源策略就是指脚本只能读取和所属文 ...

  9. Linux下截屏方法

    参考百度经验 https://jingyan.baidu.com/article/48a42057c8e8dfa92525047c.html 第一种: 截屏部分画面并保存 快捷键Shift+PrtSc

随机推荐

  1. 【转】jQuery教程

    “jQuery风暴” 推荐及配套代码下载 ziqiu.zhang 2011-03-24 00:28 阅读:15339 评论:100   从零开始学习jQuery(剧场版) 你必须知道的javascri ...

  2. C++函数二义性问题,我怎么感觉编译器有偷懒嫌疑!!!

    瞎扯一段,讲得不一定对.纯属学习! struct BB{ void a(){ cout << "bb's a()\n"; }}; struct B1 : public ...

  3. 使用Physics_Body_Editor获得json文件的类

    [转自]:http://www.cocoachina.com/bbs/read.php?tid=209290 工具介绍,json文件获得方法,请参考原帖 MyBodyParser.h // // My ...

  4. hibernate映射关系之多对多

    多对多: * 关系在第三张表中,和两张表本身没有关系 * 多对多谁维护关系:谁都能维护关系(效率是一样的),一般情况下可以通过页面 来体现 * 关系体现: 第三张表的维护:增加.删除 course类对 ...

  5. 在vs code中使用ftp-sync插件实现客户端与服务器端代码的同步

    在vs code中使用ftp-sync插件实现客户端与服务器端代码的同步 下载安装 vscode-ftp-sync 插件. 安装方法1. Ctrl+Shift+P 输入 ext install [插件 ...

  6. ng-class css样式

    <style> .error{background-color: red;} .warning{background-color: yellow;} </style> < ...

  7. android 开源项目学习

    1.Android团队提供的示例项目 如果不是从学习Android SDK中提供的那些样例代码开始,可能没有更好的方法来掌握在Android这个框架上开发.由Android的核心开发团队提供了15个优 ...

  8. 修改app名字

    一张图说明问题 如果没有成功clean一下,或者卸载掉原有的重新生成一下 如果要修改路径名和工程名有个复杂的方法 http://blog.sina.com.cn/s/blog_a42013280101 ...

  9. kafka笔记-Kafka在zookeeper中的存储结构【转】

    参考链接:apache kafka系列之在zookeeper中存储结构  http://blog.csdn.net/lizhitao/article/details/23744675 1.topic注 ...

  10. JavaScript的组成—ECMAScript、BOM和DOM

      JavaScript 是一种基于 ECMAScript 规范的脚本语言,并在此基础上进行了自己的封装.ECMAScript 不是一种编程语言,仅仅是一种脚本语言规范,由欧洲计算机协会制定和发布,任 ...