本文最后更新于 2024-11-26T12:13:00+00:00
首先我知道相机对象可以直接调用内部接口实现截图的效果,但是我们要拍摄一张不带UI的截图,那么我们可以使用第二个相机,不添加UI图层来实现这一操作。
我们要在场景中创建第二个相机,然后关闭它的Adiuo listener
(我好像打错了,不过你应该能意会),然后将它的剔除遮罩中的UI取消勾选,并将你的UI图层设置为UI注意是图层,不是排序图层
像这样:
一定要取消Audio Listenner
选项,不然会导致你的点击事件变得奇怪。然后创建一个脚本加入以下代码:
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
| #region 截图
public Camera screenshotCamera;
public string SaveCurrenScreen(string filename) { string path = Path.Combine(Application.persistentDataPath, RecordData.NAME, "screenshot", "screenshot_" + filename + ".png"); Debug.Log("ScreenShot Save to: " + path); if (!Directory.Exists(Path.GetDirectoryName(path))) { Directory.CreateDirectory(Path.GetDirectoryName(path)); } SaveCameraView(path); return path; }
void SaveCameraView(string path) { RenderTexture screenTexture = new RenderTexture(Screen.width, Screen.height, 24); screenshotCamera.targetTexture = screenTexture; RenderTexture.active = screenTexture; screenshotCamera.Render(); Texture2D renderTexture = new Texture2D(Screen.width, Screen.height); renderTexture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); RenderTexture.active = null; byte[] byArray = renderTexture.EncodeToPNG(); File.WriteAllBytes(path, byArray); } #endregion
|
然后只需要调用SaveCurrenScreen
就好了。