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

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

服务器之家 - 编程语言 - C# - Unity实现相机截图功能

Unity实现相机截图功能

2022-09-05 14:10冰封百度 C#

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

最近做项目的时候需要在游戏里截一张高清截图,研究了一下写成脚本,方便以后使用。

脚本可以自定义分辨率,用相机截高清截图。可以用代码动态截图,也可以在编辑模式下截图。

注意截图宽高比要正确,宽高比不正确时可能会出问题。

截图效果:

Unity实现相机截图功能

Unity实现相机截图功能

Unity实现相机截图功能

脚本:

cameracapture.cs

?
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using unityengine;
using system.io;
 
/// <summary>
/// 相机截图
/// <para>zhangyu 2018-07-06</para>
/// </summary>
public class cameracapture : monobehaviour {
 
 // 截图尺寸
 public enum capturesize {
 camerasize,
 screenresolution,
 fixedsize
 }
 
 // 目标摄像机
 public camera targetcamera;
 // 截图尺寸
 public capturesize capturesize = capturesize.camerasize;
 // 像素尺寸
 public vector2 pixelsize;
 // 保存路径
 public string savepath = "streamingassets/";
 // 文件名称
 public string filename = "cameracapture.png";
 
 #if unity_editor
 private void reset() {
 targetcamera = getcomponent<camera>();
 pixelsize = new vector2(screen.currentresolution.width, screen.currentresolution.height);
 }
 #endif
 
 /// <summary> 保存截图 </summary>
 /// <param name="camera">目标摄像机</param>
 public void savecapture() {
 vector2 size = pixelsize;
 if (capturesize == capturesize.camerasize) {
  size = new vector2(targetcamera.pixelwidth, targetcamera.pixelheight);
 } else if (capturesize == capturesize.screenresolution) {
  size = new vector2(screen.currentresolution.width, screen.currentresolution.height);
 }
 string path = application.datapath + "/" + savepath + filename;
 savetexture(path, capture(targetcamera, (int)size.x, (int)size.y));
 }
 
 /// <summary> 相机截图 </summary>
 /// <param name="camera">目标相机</param>
 public static texture2d capture(camera camera) {
 return capture(camera, screen.width, screen.height);
 }
 
 /// <summary> 相机截图 </summary>
 /// <param name="camera">目标相机</param>
 /// <param name="width">宽度</param>
 /// <param name="height">高度</param>
 public static texture2d capture(camera camera, int width, int height) {
 rendertexture rt = new rendertexture(width, height, 0);
 rt.depth = 24;
 rt.antialiasing = 8;
 camera.targettexture = rt;
 camera.renderdontrestore();
 rendertexture.active = rt;
 texture2d texture = new texture2d(width, height, textureformat.argb32, false, true);
 rect rect = new rect(0, 0, width, height);
 texture.readpixels(rect, 0, 0);
 texture.filtermode = filtermode.point;
 texture.apply();
 camera.targettexture = null;
 rendertexture.active = null;
 destroy(rt);
 return texture;
 }
 
 /// <summary> 保存贴图 </summary>
 /// <param name="path">保存路径</param>
 /// <param name="texture">texture2d</param>
 public static void savetexture(string path, texture2d texture) {
 file.writeallbytes(path, texture.encodetopng());
 #if unity_editor
 debug.log("已保存截图到:" + path);
 #endif
 }
 
}

脚本编辑器:

cameracaptureeditor.cs

?
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
using unityeditor;
using unityengine;
 
/// <summary>
/// 相机截图 编辑器
/// <para>zhangyu 2018-07-06</para>
/// </summary>
[caneditmultipleobjects]
[customeditor(typeof(cameracapture))]
public class cameracaptureeditor : editor {
 
 public override void oninspectorgui() {
 // 属性
 cameracapture script = (cameracapture)target;
 int selected = (int)script.capturesize;
 
 // 重绘gui
 editorgui.beginchangecheck();
 drawproperty("targetcamera", "目标像机");
 string[] options = new string[] { "像机尺寸", "屏幕尺寸", "固定尺寸"};
 selected = editorguilayout.popup("截图尺寸", selected, options, guilayout.expandwidth(true));
 script.capturesize = (cameracapture.capturesize)selected;
 if (script.capturesize == cameracapture.capturesize.fixedsize) {
  drawproperty("pixelsize", "像素尺寸");
  editorguilayout.helpbox("请保持正确的宽高比!\n否则截图区域可能出现错误。", messagetype.info);
 }
 drawproperty("savepath", "保存路径");
 drawproperty("filename", "文件名称");
 
 // 保存截图按钮
 bool ispress = guilayout.button("保存截图", guilayout.expandwidth(true));
 if (ispress) script.savecapture();
 if (editorgui.endchangecheck()) serializedobject.applymodifiedproperties();
 }
 
 private void drawproperty(string property, string label) {
 editorguilayout.propertyfield(serializedobject.findproperty(property), new guicontent(label), true);
 }
 
}

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

原文链接:https://segmentfault.com/a/1190000015552889

延伸 · 阅读

精彩推荐
  • C#C#使用Unity实现剪刀石头布游戏

    C#使用Unity实现剪刀石头布游戏

    这篇文章主要为大家详细介绍了C#语言使用Unity实现剪刀石头布游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    莹莹carriex4722022-03-08
  • C#C#连接Oracle数据库字符串(引入DLL)的方式

    C#连接Oracle数据库字符串(引入DLL)的方式

    这篇文章主要给大家介绍了关于C#连接Oracle数据库字符串(引入DLL)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    陈彦斌8932022-08-01
  • C#c#打包文件解压缩的实例

    c#打包文件解压缩的实例

    下面小编就为大家分享一篇c#打包文件解压缩的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    浅苍蓝4352022-02-10
  • C#Unity3D UGUI特效之Image高斯模糊效果

    Unity3D UGUI特效之Image高斯模糊效果

    这篇文章主要为大家详细介绍了Unity3D UGUI特效之Image高斯模糊效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    苏小败在路上12162022-03-11
  • C#C# 在PDF文档中创建表格的实现方法

    C# 在PDF文档中创建表格的实现方法

    表格能够一目了然的让用户看到数据信息,使信息显得有条理化,那么在pdf类型的文档中如何来添加表格并对表格进行格式化操作呢?下面小编给大家带来...

    E-iceblue11422022-02-17
  • C#C#知识整理

    C#知识整理

    本文主要介绍了C#的相关知识。具有很好的参考价值,下面跟着小编一起来看下吧...

    键盘上青春11272021-12-22
  • C#C#实现发送手机验证码功能

    C#实现发送手机验证码功能

    之前基于c#实现手机发送验证码功能很复杂,真正做起来也就那回事,不过就是一个post请求就可以实现的东西,今天小编把思路分享到脚本之家平台,供大...

    Larry_TH9072022-01-11
  • C#Unity实现相机截图功能

    Unity实现相机截图功能

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

    冰封百度11842022-09-05