最近做项目的时候需要在游戏里截一张高清截图,研究了一下写成脚本,方便以后使用。
脚本可以自定义分辨率,用相机截高清截图。可以用代码动态截图,也可以在编辑模式下截图。
注意截图宽高比要正确,宽高比不正确时可能会出问题。
截图效果:
脚本:
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