利用Unity提供函数"ScreenCapture"可以屏幕截取,不过只能截全屏,不区分层级,屏幕内显示的会全部截取下来,与手机截屏功能比较类似,实用性不大 ···
下面是一组简单测试的数据:
//屏幕尺寸: 720*1280
//保存截图到本地
//ScreenCapture.CaptureScreenshot(截屏图片保存路径);
//后缀名, 需要自定义后缀名,png、jpg等图片格式,也可以是其他格式,比如txt,不过保存的是一堆乱码
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName01.png"); //尺寸: 512*1024 大小: 341.4KB
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName0a.jpg"); //尺寸: 512*1024 大小: 341.4KB
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName0b.txt"); //尺寸: *** 大小: ***
//ScreenCapture.CaptureScreenshot(截屏图片保存路径, 分辨率指数);
//分辨率指数或分辨率影响参数,测试发现,数值应为正整数,<=0时,默认为1,同时也有上限,不知跟截图对象有没有关系,测试发现是有1跟2有区别
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName02.png", 0); //尺寸: 512*1024 大小: 341.4KB
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName03.png", -1); //尺寸: 512*1024 大小: 341.4KB
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName04.png", 1); //尺寸: 512*1024 大小: 341.4KB
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName05.png", 2); //尺寸: 1024*2048 大小: 1.3MB
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName06.png", 3); //尺寸: 1024*2048 大小: 1.3MB
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName07.png", 4); //尺寸: 1024*2048 大小: 1.3MB
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName08.png", 5); //尺寸: 1024*2048 大小: 1.3MB
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName09.png", 10); //尺寸: 1024*2048 大小: 1.3MB
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName10.png", 20); //尺寸: 1024*2048 大小: 1.3MB
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName11.png", 50); //尺寸: 1024*2048 大小: 1.3MB
//ScreenCapture.CaptureScreenshot(截屏图片保存路径, 截屏方式);
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName12.png", ScreenCapture.StereoScreenCaptureMode.BothEyes); //尺寸: 512*1024 大小: 341.4KB
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName13.png", ScreenCapture.StereoScreenCaptureMode.BothEyes); //尺寸: 512*1024 大小: 341.4KB
ScreenCapture.CaptureScreenshot(Application.dataPath + "/fileName14.png", ScreenCapture.StereoScreenCaptureMode.BothEyes); //尺寸: 512*1024 大小: 341.4KB
//获取 Texture2D 截图,参数同上
Texture2D texture2D01 = ScreenCapture.CaptureScreenshotAsTexture();
Texture2D texture2D02 = ScreenCapture.CaptureScreenshotAsTexture(1);
Texture2D texture2D03 = ScreenCapture.CaptureScreenshotAsTexture(ScreenCapture.StereoScreenCaptureMode.BothEyes);
//获取 RenderTexture 截图
RenderTexture renderTexture = new RenderTexture(720, 1280, 1);
ScreenCapture.CaptureScreenshotIntoRenderTexture(renderTexture);