欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Unity实现截屏以及根据相机画面截图

程序员文章站 2022-06-17 22:17:42
在游戏开发和软件开发中,经常需要截图的功能,分带ui的截图和不带ui的截图功能。代码如下:using system.collections;using system.collections.gener...

在游戏开发和软件开发中,经常需要截图的功能,分带ui的截图和不带ui的截图功能。代码如下:

using system.collections;
using system.collections.generic;
using unityengine;
 
public static class screenshotforcamera{
 
 public static void capturescreen(string _path = null)
 {
 if (_path == null)
 _path = "screenshot.png";
 
 application.capturescreenshot(_path, 0);
 }
 
 public static texture2d capturescreen(rect rect, bool _iscreatephoto = false, string _path = null)
 {
 // 先创建一个的空纹理,大小可根据实现需要来设置 
 texture2d screenshot = new texture2d((int)rect.width, (int)rect.height, textureformat.rgb24, false);
 
 // 读取屏幕像素信息并存储为纹理数据, 
 screenshot.readpixels(rect, 0, 0);
 screenshot.apply();
 
 // 然后将这些纹理数据,成一个png图片文件 
 if (_iscreatephoto)
 {
 if(_path == null)
 _path = application.datapath + "/screenshot.png";
 
 byte[] bytes = screenshot.encodetopng();
 string filename = _path;
 system.io.file.writeallbytes(filename, bytes);
 debug.log(string.format("截屏了一张图片: {0}", filename));
 }
 
 // 最后,我返回这个texture2d对象,这样我们直接,所这个截图图示在游戏中,当然这个根据自己的需求的。 
 return screenshot;
 }
 
 //
 public static texture2d capturecamera(ref camera _camera, rect _rect, int _destx, int _desty, bool _iscreatephoto = false, string _path = null)
 {
 rendertexture rendertexture = new rendertexture((int)_rect.width, (int)_rect.height, 24, rendertextureformat.argb32);
 _camera.targettexture = rendertexture;
 _camera.render();
 
 // 激活这个rendertexture, 并从中中读取像素
 rendertexture.active = _camera.targettexture;
 texture2d screenshot = new texture2d((int)_rect.width, (int)_rect.height, textureformat.argb32, false);
 screenshot.readpixels(_rect, _destx, _desty); //从(_destx,_desty)坐标开始读取_rect大小的图片
 screenshot.apply();
 
 //重置参数
 //_camera.targettexture = null;
 rendertexture.active = null;
 //gameobject.destroy(rendertexture);
 
 //生成png图片
 if (_iscreatephoto)
 {
 if (_path == null)
 _path = application.datapath + "/screenshot.png";
 
 byte[] bytes = screenshot.encodetopng();
 string filename = _path;
 system.io.file.writeallbytes(filename, bytes);
 debug.log(string.format("截屏了一张照片: {0}", filename));
 }
 
 return screenshot;
 }
}

小编再为大家分享一段:unity实现截屏功能,希望可以帮到大家

public class screenshot : monobehaviour 
{

 void onscreenshotclick()
 {
 //得到当前系统时间
 system.datetime now = system.datetime.now;
 string times = now.tostring();
 //去掉前后空格
 times = times.trim();
 //将斜杠替换成横杠
 times = times.replace("/", "-");

 string filename = "arscreenshot" + times + ".png";
 //判断该平台是否为安卓平台
 if (application.platform == runtimeplatform.android)
 {
 //参数依次为 屏幕宽度 屏幕高度 纹理格式 是否使用映射
 texture2d texture = new texture2d(screen.width, screen.height, textureformat.rgb24, false);
 //读取贴图
 texture.readpixels(new rect(0, 0, screen.width, screen.height), 0, 0);
 //应用截屏
 texture.apply();
 //将对象序列化
 byte[] bytes = texture.encodetopng();
 //设定存储到的手机文件夹路径
 string destination = "/sdcard/dcim/screenshots";
 //如果不存在该文件夹
 if (!directory.exists(destination))
 {
 //创建该文件夹
 directory.createdirectory(destination);
 }
 string pathsave = destination + "/" + filename;
 file.writeallbytes(pathsave, bytes);
 }
 }
}

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