unity3d摄像机输出图片代码
时间: 2023-04-05 12:00:30 浏览: 195
以下是 Unity3D 摄像机输出图片的代码示例:
```csharp
using UnityEngine;
public class CameraCapture : MonoBehaviour
{
public Camera cameraToCapture;
public string savePath = "Assets/CapturedImage.png";
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Capture();
}
}
void Capture()
{
RenderTexture renderTexture = cameraToCapture.targetTexture;
Texture2D texture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.RGB24, false);
cameraToCapture.Render();
RenderTexture.active = renderTexture;
texture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture.Apply();
RenderTexture.active = null;
byte[] bytes = texture.EncodeToPNG();
System.IO.File.WriteAllBytes(savePath, bytes);
Debug.Log("Image captured and saved to " + savePath);
}
}
```
这段代码可以让你在 Unity3D 中捕获摄像机的输出图像,并将其保存为 PNG 图片。你可以将其添加到一个空对象上,并将需要捕获的摄像机指定为 `cameraToCapture` 变量。按下空格键即可触发捕获操作。
阅读全文