unity3d摄像机输出模型图片代码
时间: 2023-04-05 13:02:41 浏览: 153
unity3d 代码
5星 · 资源好评率100%
以下是我对您的问题的回答:
您可以使用以下代码将 Unity3D 摄像机输出模型图片:
```
using UnityEngine;
using System.Collections;
public class CameraScreenshot : MonoBehaviour {
public int captureWidth = 1920;
public int captureHeight = 1080;
private bool capturingScreenshot = false;
void Update () {
if (Input.GetKeyDown(KeyCode.Space)) {
capturingScreenshot = true;
}
}
void LateUpdate () {
if (capturingScreenshot) {
capturingScreenshot = false;
RenderTexture rt = new RenderTexture(captureWidth, captureHeight, 24);
GetComponent<Camera>().targetTexture = rt;
Texture2D screenShot = new Texture2D(captureWidth, captureHeight, TextureFormat.RGB24, false);
GetComponent<Camera>().Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, captureWidth, captureHeight), 0, 0);
GetComponent<Camera>().targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
Destroy(rt);
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/../screenshot.png";
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("Took screenshot to: {0}", filename));
}
}
}
```
这段代码会在按下空格键时,将当前摄像机的渲染结果保存为一张 PNG 格式的图片,并保存在应用程序的根目录下。您可以根据需要修改图片的分辨率和保存路径。
阅读全文