You need to enable JavaScript to run this app. 如何在Unity中将Agora.io的视频流渲染输出到WebCamTexture
优惠活动
大模型
产品
解决方案
定价
更多

如何在Unity中将Agora.io的视频流渲染输出到WebCamTexture

Agora视频流渲染到Unity WebCamTexture实现方案

最优方案(无Agora自带渲染依赖、无额外纹理复制)

该方案完全绕开Agora自带的VideoSurface渲染逻辑,直接从SDK开放的回调接口取原始视频帧填充WebCamTexture,无需修改Agora SDK源码,稳定性和成功率最高:

  • 首先实现Agora公开的IVideoFrameObserver接口,注册视频原始帧回调,注册时指定帧输出格式为RGBA32(和WebCamTexture默认格式匹配,无转换开销)
  • 回调触发时直接从回调参数中获取二进制帧数据
  • 调用WebCamTexture的LoadRawTextureData()方法传入帧数据,再调用Apply()方法刷新纹理即可完成渲染,全程无额外纹理复制操作

核心代码示例:

// 注册回调
agoraEngine.RegisterVideoFrameObserver(videoFrameObserver);
// 回调中填充纹理
public bool OnCaptureVideoFrame(VideoFrame videoFrame)
{
    targetWebCamTexture.LoadRawTextureData(videoFrame.buffer);
    targetWebCamTexture.Apply();
    return true;
}

低修改量备选方案(无需重写VideoSurface代码)

如果不想自己处理原始帧回调,可以用反射替代修改私有变量权限的操作,完全不需要改动Agora SDK的源码,避免后续版本更新产生代码冲突:

  • 通过反射获取VideoSurface实例的私有Texture2D字段,参考代码:var textureField = typeof(VideoSurface).GetField("mTexture", BindingFlags.Instance | BindingFlags.NonPublic); Texture2D agoraTexture = (Texture2D)textureField.Get(videoSurfaceIns);
  • 拿到Agora输出的纹理后,调用Graphics.Blit(agoraTexture, yourTargetTexture)即可完成纹理复制

注意:如果选用原始帧回调方案,需要提前对齐Agora输出帧和WebCamTexture的分辨率、帧率、像素格式,避免出现花屏问题。

内容的提问来源于stack exchange,提问作者Kanyade

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 18:30:03