47 lines
1.1 KiB
C#
47 lines
1.1 KiB
C#
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();
|
|
}
|
|
}
|
|
} |