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

所属分类: 软件编程 / C#教程 阅读数: 53
收藏 0 赞 0 分享

在游戏开发和软件开发中,经常需要截图的功能,分带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);
 }
 }
}

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

更多精彩内容其他人还在看

C# 导出Excel的6种简单方法实现

C# 导出 Excel 的6种简单方法:数据表导出到 Excel,对象集合导出到 Excel,数据库导出到 Excel,微软网格控件导出到 Excel,数组导出到 Excel,CSV 导出到 Excel,你都会了吗?需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

c# Winform自定义控件-仪表盘功能

这篇文章主要介绍了c#Winform自定义控件-仪表盘功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#调用百度API实现活体检测的方法

这篇文章主要给大家介绍了关于C#调用百度API实现活体检测的方法,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

C#客户端程序Visual Studio远程调试的方法详解

这篇文章主要给大家介绍了关于C#客户端程序Visual Studio远程调试的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

C#中标准的IDispose模式代码详解

在本篇文章中小编给大家分享的是关于C#中标准的IDispose模式的实例用法相关内容,有需要的朋友们测试下。
收藏 0 赞 0 分享

C# 获取某个时间的0点0分和23点59分59秒

这篇文章主要介绍了C# 获取某个时间的0点0分和23点59分59秒,文中给大家提到了java 获取某一日期的0点0分0秒和23点59分59秒,需要的朋友可以参考下
收藏 0 赞 0 分享

Unity实现简易日志输出功能

这篇文章主要为大家详细介绍了Unity实现简易日志输出功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Unity3D开发实战之五子棋游戏

这篇文章主要为大家详细介绍了Unity3D开发实战之五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Unity3D实现简易五子棋源码

这篇文章主要为大家详细介绍了Unity3D实现简易五子棋源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Unity3D在Preview中打印日志的方法

这篇文章主要为大家详细介绍了Unity3D在Preview中打印日志的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多