当前位置: 移动技术网 > IT编程>开发语言>c# > Unity实现全屏截图以及QQ截图

Unity实现全屏截图以及QQ截图

2020年06月23日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了unity实现全屏截图、unity实现qq截图,供大家参考,具体内容如下

全屏截图:要实现的是点击鼠标左键,就实现截图,并且将所截图片保存到本地assets目录下的streamingassets文件夹下面。

代码如下:

using unityengine;
using system.collections;

public class takescreenshot : monobehaviour {
void update () {
//点击鼠标左键
if (input.getmousebuttondown (0)) {
//开启协程方法
startcoroutine (mycapturescreen ());

}
}
//我的截屏方法
ienumerator mycapturescreen(){
//等待所有的摄像机和gui被渲染完成。
yield return new waitforendofframe ();
//创建一个空纹理(图片大小为屏幕的宽高)
texture2d tex = new texture2d (screen.width,screen.height);
//只能在帧渲染完毕之后调用(从屏幕左下角开始绘制,绘制大小为屏幕的宽高,宽高的偏移量都为0)
tex.readpixels (new rect (0,0,screen.width,screen.height),0,0);
//图片应用(此时图片已经绘制完成)
tex.apply ();
//将图片装换成jpg的二进制格式,保存在byte数组中(计算机是以二进制的方式存储数据)
byte[] result = tex.encodetojpg ();
//文件保存,创建一个新文件,在其中写入指定的字节数组(要写入的文件的路径,要写入文件的字节。)
system.io.file.writeallbytes (application.streamingassetspath+"/1.jpg",result);

}
}

之后思考一下,如何像qq截图那样,绘制一个截屏的矩形框只截取自己想要的部分呢?

using unityengine;
using system.collections;
using system.io;//引入io流
public class newbehaviourscript : monobehaviour {
//定义一个存储截屏图片的路径
string filepath;
void start () {
//图片存储在streamingassets文件夹内。
filepath = application.streamingassetspath + "/1.png";
}
rect rect;
//截屏开始的位置
vector3 s_pos;
//截屏结束的位置
vector3 e_pos;
//是否绘制
bool isdraw;
void update()
{
//按下鼠标左键时,记录当前鼠标的位置为开始截屏时的位置
if(input.getmousebuttondown(0))
{
s_pos = input.mouseposition;
}
//鼠标处于按下状态时
if(input.getmousebutton(0))
{
e_pos = input.mouseposition;
//可以开始绘制
isdraw = true;
}
//抬起鼠标左键时,记录当前鼠标的位置为结束截屏时的位置
if(input.getmousebuttonup(0))
{
//结束绘制
isdraw = false;
e_pos = input.mouseposition;
//获取到截屏框起始点的位置,和宽高。
rect = new rect(mathf.min(s_pos.x,e_pos.x),mathf.min(s_pos.y,e_pos.y),mathf.abs(s_pos.x-e_pos.x),mathf.abs(s_pos.y - e_pos.y));
//开启绘制的协程方法
startcoroutine(capsture(filepath, rect));
}
}


ienumerator capsture(string filepath, rect rect)
{
yield return new waitforendofframe();
//创建纹理(纹理贴图的大小和截屏的大小相同)
texture2d tex = new texture2d((int)rect.width, (int)rect.height);
//读取像素点
tex.readpixels(rect, 0, 0) ;
//将像素点应用到纹理上,绘制图片
tex.apply();
//将图片装换成jpg的二进制格式,保存在byte数组中(计算机是以二进制的方式存储数据)
byte[] result =tex.encodetopng();
//文件夹(如果streamassets文件夹不存在,在assets文件下创建该文件夹)
if(!directory.exists(application.streamingassetspath))
directory.createdirectory(application.streamingassetspath);
//将截屏图片存储到本地
file.writeallbytes(filepath, result);
}
//在这里要用gl实现绘制截屏的矩形框
//1.gl的回调函数
//2.定义一个材质material
public material linematerial;

void onpostrender() {
if(!isdraw) return;
print (s_pos);

vector3 spos = camera.main.screentoworldpoint(s_pos + new vector3(0, 0, 10));
vector3 epos = camera.main.screentoworldpoint(e_pos + new vector3(0, 0, 10));

print(string.format("gl.....{0}, {1}", spos, epos));
// set your materials done
gl.pushmatrix();
// yourmaterial.setpass( );
linematerial.setpass(0);//告诉gl使用该材质绘制
// draw your stuff
//始终在最前面绘制
gl.invertculling = true;
gl.begin(gl.lines);//开始绘制

//gl.vertex(spos);
//gl.vertex(epos);
//如果想要绘制,矩形,将下面代码启动
gl.vertex(spos);
gl.vertex(new vector3(epos.x, spos.y, 0));


gl.vertex(new vector3(epos.x, spos.y, 0));
gl.vertex(epos);

gl.vertex(epos);
gl.vertex(new vector3(spos.x, epos.y, 0));

gl.vertex(new vector3(spos.x, epos.y, 0));
gl.vertex(spos);
gl.end();//结束绘制

gl.popmatrix();
}

}

在脚本组件中拖入材质球

此图片黑色的矩形框就是通过gl绘制出来的(通过记录点的位置来依次绘制出线)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网