Files
webRtc/Assets/Script/Recorder/WebcamToRenderTexture.cs

47 lines
1.1 KiB
C#
Raw Permalink Normal View History

2026-06-03 22:05:03 +08:00
using Unity.WebRTC;
using UnityEngine;
public class WebcamToRenderTexture : MonoBehaviour
{
public RenderTexture renderTexture;
private WebCamTexture webcamTexture;
void Awake()
{
if (renderTexture == null)
{
RenderTextureFormat supportedFormat = WebRTC.GetSupportedRenderTextureFormat(SystemInfo.graphicsDeviceType);
// 创建新的RenderTexture
renderTexture = new RenderTexture(1920, 1200, 0, supportedFormat);
renderTexture.enableRandomWrite = true;
renderTexture.Create();
//_textureImage.texture = localRenderTexture;
}
webcamTexture = new WebCamTexture();
webcamTexture.Play();
}
void Update()
{
if (webcamTexture != null && webcamTexture.didUpdateThisFrame)
{
Graphics.Blit(webcamTexture, renderTexture);
}
}
void OnDestroy()
{
if (webcamTexture != null)
{
webcamTexture.Stop();
}
if (renderTexture != null)
{
renderTexture.Release();
}
}
}