unity3d中如何用按钮实现当前摄影机拍摄的画面并保存的代码
时间: 2023-04-01 17:02:00 浏览: 112
「unity」按钮开关实现代码
您好,以下是回答:
您可以使用RenderTexture来实现将当前摄像机拍摄的画面保存为纹理,然后将纹理保存为图片文件。以下是示例代码:
// 获取当前摄像机
Camera currentCamera = Camera.main;
// 创建RenderTexture
RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 24);
currentCamera.targetTexture = rt;
// 渲染当前摄像机
currentCamera.Render();
// 恢复默认RenderTexture
currentCamera.targetTexture = null;
// 将RenderTexture转换为Texture2D
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
RenderTexture.active = rt;
texture.ReadPixels(new Rect(, , Screen.width, Screen.height), , );
texture.Apply();
RenderTexture.active = null;
// 将Texture2D保存为图片文件
byte[] bytes = texture.EncodeToPNG();
string fileName = "screenshot.png";
File.WriteAllBytes(Application.dataPath + "/" + fileName, bytes);
希望能帮到您。
阅读全文