Unity中的截图方法(包括全屏截图、区域截图、Camera截图和摄像头截图)
之前项目中需要用到截图功能,经过查找找到3种方式,这里做一个记录。
Application.CaptureScreenshot
Application类下的CaptureScreenshot方法,截取的是某一帧时整个游戏的画面,或者说是全屏截图吧。
以下是两个重载的函数。其中filename参数为截图后保存下来的文件名;superSize参数为增加分辨率的因数:
//
// 摘要:
// Captures a screenshot at path filename as a PNG file.
//
// 参数:
// filename:
// Pathname to save the screenshot file to.
//
// superSize:
// Factor by which to increase resolution.
[Obsolete("Application.CaptureScreenshot is obsolete. Use ScreenCapture.CaptureScreenshot instead (UnityUpgradable) -> [UnityEngine] UnityEngine.ScreenCapture.CaptureScreenshot(*)", true)]
public static void CaptureScreenshot(string filename);
//
// 摘要:
// Captures a screenshot at path filename as a PNG file.
//
// 参数:
// filename:
// Pathname to save the screenshot file to.
//
// superSize:
// Factor by which to increase resolution.
[Obsolete("Application.CaptureScreenshot is obsolete. Use ScreenCapture.CaptureScreenshot instead (UnityUpgradable) -> [UnityEngine] UnityEngine.ScreenCapture.CaptureScreenshot(*)", true)]
public static void CaptureScreenshot(string filename, int superSize);
通过上面的代码也可以看出,在新版的Unity中该函数被废弃了。新的系统函数类是ScreenCapture。
ScreenCapture
以下为系统类ScreenCapture:
//
// 摘要:
// Functionality to take Screenshots.
[NativeHeader("Modules/ScreenCapture/Public/CaptureScreenshot.h")]
public static class ScreenCapture
{
public static void CaptureScreenshot(string filename);
//
// 摘要:
// Captures a screenshot at path filename as a PNG file.
//
// 参数:
// filename:
// Pathname to save the screenshot file to.
//
// superSize:
// Factor by which to increase resolution.
//
// stereoCaptureMode:
// Specifies the eye texture to capture when stereo rendering is enabled.
public static void CaptureScreenshot(string filename, int superSize);
public static void CaptureScreenshot(string filename, StereoScreenCaptureMode stereoCaptureMode);
public static Texture2D CaptureScreenshotAsTexture

本文详细介绍了Unity中四种截图方法:全屏截图、区域截图、Camera截图和摄像头截图。全屏截图可通过Application.CaptureScreenshot(已废弃)和ScreenCapture.CaptureScreenshot实现;区域截图利用Texture2D.ReadPixels读取特定视口像素;Camera截图借助RenderTexture和Camera.targetTexture完成;摄像头截图则涉及WebCamTexture.GetPixels。每种方法都包含关键代码示例,适用于不同场景的截图需求。
最低0.47元/天 解锁文章
1903

被折叠的 条评论
为什么被折叠?



