WebGLVideoBridge advanced
WebGLVideoBridge is the low-level C# wrapper for the hidden browser
<video> element. Use it when direct lifecycle and texture-upload control is required. For standard
playback, use the ProperVideoPlayer component, which adds texture management, a watchdog, and a
non-WebGL fallback.
Lifecycle#
- Create the bridge with a dedicated
RenderTexture. - Configure preload, credentials, or request headers before calling
SetSource. - Call
UploadFrameIfDirty()once per rendered frame. It returnstrueonly when a frame was uploaded. - Call
Dispose()when the bridge is no longer needed. Disposal removes the browser media element.
using ProperVideoPlayerWebGL;
using UnityEngine;
public sealed class LayeredVideo : MonoBehaviour
{
[SerializeField] private RenderTexture target;
private WebGLVideoBridge bridge;
private void Start()
{
bridge = new WebGLVideoBridge(target, muted: true, looping: true);
bridge.LoadedData += () => Debug.Log("First video frame decoded");
bridge.ErrorRaised += ReportError;
bridge.SetSource("https://cdn.example.com/layer.webm");
bridge.Play();
}
private void Update()
{
bridge?.UploadFrameIfDirty();
}
private void ReportError()
{
Debug.LogWarning($"Video error {bridge.ErrorCode()}: {bridge.ErrorMessage()}");
}
private void OnDestroy()
{
bridge?.Dispose();
}
}
Texture behavior#
- Assign a different
RenderTextureto each bridge. Reusing a native texture handle is detected and logged as an error. - Video frames require a vertical UV flip when sampled in WebGL. For a
RawImage, useuvRect = new Rect(0, 1, 1, -1). - On the WebGL graphics path, the native upload code replaces the texture storage with storage sized to the
decoded video. The Unity-side
RenderTexturedescriptor does not reflect that native resize. - On the WebGPU graphics path, the existing texture storage is used. Recreate the target at the required size
and call
SetTargetTexturein response to metadata or resize events when dimensions can change.
Platform behavior#
Outside WebGL builds, the bridge methods compile to inert stubs. The class does not create Unity's
VideoPlayer fallback. Use ProperVideoPlayer when the same playback feature must operate on
WebGL and non-WebGL targets.
API surface#
The bridge exposes browser media events as C# events, playback and audio controls, HLS/DASH source selection, authentication settings, preload control, time-range queries, frame counters, upload diagnostics, and media-error details. Static methods provide codec probing and streaming-library configuration. See the API Reference for the complete member list.
WebGLVideoBridge does not apply automatic recovery. Use Reload(), playback state, and
frame counters to implement an application-specific policy, or use ProperVideoPlayer for the packaged
watchdog.