欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Unity实现相机截图功能

程序员文章站 2023-02-19 16:31:12
最近做项目的时候需要在游戏里截一张高清截图,研究了一下写成脚本,方便以后使用。脚本可以自定义分辨率,用相机截高清截图。可以用代码动态截图,也可以在编辑模式下截图。注意截图宽高比要正确,宽高比不正确时可...

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

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

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

截图效果:

Unity实现相机截图功能

Unity实现相机截图功能

Unity实现相机截图功能

脚本:

cameracapture.cs

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

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);
 }
 
}

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