服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity实现截屏以及根据相机画面截图

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

2022-09-03 14:35程序猴子sy C#

这篇文章主要为大家详细介绍了Unity实现截屏以及根据相机画面截图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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实现截屏功能,希望可以帮到大家

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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);
 }
 }
}

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

原文链接:https://blog.csdn.net/qq_34818497/article/details/79464366

延伸 · 阅读

精彩推荐
  • C#c#使用dynamic类型优化反射的方法

    c#使用dynamic类型优化反射的方法

    dynamic是FrameWork4.0的新特性,下面这篇文章主要给大家介绍了关于c#使用dynamic类型优化反射的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或...

    Fode6822022-03-06
  • C#C#RSA对接JAVA中RSA方式代码实例

    C#RSA对接JAVA中RSA方式代码实例

    在本篇文章中小编给各位整理的是关于C#RSA对接JAVA中RSA方式代码实例,有需要的朋友们可以参考一下。...

    盛开的雨季5892022-08-04
  • C#使用设计模式中的工厂方法模式进行C#编程的示例讲解

    使用设计模式中的工厂方法模式进行C#编程的示例讲解

    这篇文章主要介绍了使用设计模式中的工厂方法模式进行C#编程的示例讲解,工厂方法模式可以看作是对简单工厂模式的进一步扩展,需要的朋友可以参考下...

    LearningHard6072021-11-12
  • C#C#减少垃圾回收压力的字符串操作详解

    C#减少垃圾回收压力的字符串操作详解

    这篇文章给大家详细分析了C#减少垃圾回收压力的字符串操作的相关知识点,有兴趣的朋友参考学习下吧。...

    衣舞晨风7782022-02-20
  • C#详解C#用new和override来实现抽象类的重写区别

    详解C#用new和override来实现抽象类的重写区别

    本篇文章主要介绍了详解C#用new和override来实现抽象类的重写区别,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧。...

    叶丶梓轩6932021-12-23
  • C#NancyFx框架检测任务管理器详解

    NancyFx框架检测任务管理器详解

    这篇文章主要为大家详细介绍了NancyFx框架检测任务管理器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Lexan5042022-01-25
  • C#c#实现识别图片上的验证码数字

    c#实现识别图片上的验证码数字

    这篇文章主要介绍了c#实现识别图片上的验证码数字的方法,本文给大家汇总了2种方法,有需要的小伙伴可以参考下。...

    C#教程网5612021-11-02
  • C#C#实现打造气泡屏幕保护效果

    C#实现打造气泡屏幕保护效果

    本文是介给大家介绍一个很好玩的小程序:气泡屏幕保护!类似于windows的屏保功能,有需要的朋友可以参考一下。...

    李sir4312021-12-08