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

所属分类: 软件编程 / C#教程 阅读数: 117
收藏 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# 全角和半角转换以及判断的简单代码

这篇文章介绍了在C#中判断和转换全角半角的方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

解析C#中如何把控件的边框角画为圆弧

以下是对C#中把控件的边框角画为圆弧的实现代码进行了介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

如何清空文件夹里面的所有文件和文件夹

以下是对c#中清空文件夹里面的所有文件和文件夹的实现代码进行了详细的分析介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

如何让C#、VB.NET实现复杂的二进制操作

VB.NET和C#属于高级语言,对二进制位操作的支持不是很好,比如没有了移位运算等,用的时候确实很不方便,所以在闲暇之余我重新封装了一个用于C#、VB.NET的位操作类库,通过该类库可以实现数据移位、循环移位、转换为二进制、将二进制转换为数据等
收藏 0 赞 0 分享

C# 中如何利用lambda实现委托事件的挂接

在写一个小程序的时候,碰到了这样的问题,需要用委托来挂接事件,但是又想在这事件中使用局部的变量,而委托一旦定义好后,挂接方就没有办法再添加额外的形参了。那有没有什么办法,可以实现呢
收藏 0 赞 0 分享

C#技巧之快速删除bin和obj文件夹的方法

C#程序总会生成bin和obj文件夹,为了减小源码的大小,就有必要将这两个文件夹删除,于是想到用批处理文件来删除
收藏 0 赞 0 分享

C#编程实现Excel文档中搜索文本内容的方法及思路

有了在Word文档中编程实现搜索文本的经验,在Excel中实现这个功能也并非难事。
收藏 0 赞 0 分享

C#根据年月日计算星期几的函数小例子

这篇文章介绍了C#根据年月日计算星期几的函数小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c# 空合并运算符“??”的使用详解

本篇文章是对c#中空合并运算符“??”的使用进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

浅谈c# 泛型类的应用

本篇文章是对c#中泛型类的应用进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享
查看更多