You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Unity C#中实现跨PC/Android的Oculus头显视频录屏?

跨平台Oculus VR视频录制解决方案(Unity C#)

一、Oculus官方OVRRecorder API(推荐)

这是Oculus原生支持的方案,完美兼容Quest系列(Android)和Rift系列(PC),录制的是合成后的流畅单眼画面,不会出现双透镜问题。

实现步骤:

  1. 在Unity Package Manager中导入Oculus Integration包,或从Unity Asset Store获取。
  2. 场景中添加OVRManager组件,确保VR环境初始化正常。
  3. 编写C#调用录制API:
using Oculus.Platform;
using Oculus.Platform.Models;
using UnityEngine;

public class OculusRecorder : MonoBehaviour
{
    private bool _isRecording;

    void Start()
    {
        // 初始化Oculus Platform SDK,需提前在Oculus开发者后台配置App ID
        Core.Initialize();
    }

    // 绑定UI按钮或手柄按键调用此方法
    public void ToggleRecording()
    {
        if (!_isRecording)
        {
            var options = new RecordingOptions
            {
                VideoQuality = VideoQuality.High,
                EnableMic = true // 可选开启麦克风录制
            };
            Recorder.StartRecording(options);
            _isRecording = true;
            Debug.Log("录制已启动");
        }
        else
        {
            Recorder.StopRecording().OnComplete(HandleRecordingStop);
            _isRecording = false;
        }
    }

    private void HandleRecordingStop(Message<Recording> msg)
    {
        if (msg.IsError)
        {
            Debug.LogError($"录制失败: {msg.GetError().Message}");
            return;
        }

        var recording = msg.Data;
        Debug.Log($"录制完成,文件路径: {recording.FilePath}");
        // 可在此处添加文件保存、分享逻辑
    }
}

注意事项:

  • PC端录制文件默认存于Oculus用户目录,Android端存于设备存储的Oculus文件夹
  • 必须在Oculus开发者后台配置对应App ID,否则Platform SDK无法初始化

二、Unity原生RenderTexture + MediaCapture方案(无第三方依赖)

如果不想依赖Oculus SDK,可使用Unity原生API实现跨平台录制,核心是捕获VR相机的渲染输出。

示例代码:

using UnityEngine;
using System.IO;
using UnityEngine.Video;

public class VRVideoRecorder : MonoBehaviour
{
    [SerializeField] private Camera _vrCamera;
    private RenderTexture _renderTexture;
    private VideoWriter _videoWriter;
    private bool _isRecording;
    private string _savePath;

    void Start()
    {
        // 初始化RenderTexture匹配VR相机分辨率
        _renderTexture = new RenderTexture(_vrCamera.pixelWidth, _vrCamera.pixelHeight, 24);
        _vrCamera.targetTexture = _renderTexture;

        // 区分PC/Android保存路径
        #if UNITY_ANDROID && !UNITY_EDITOR
            _savePath = Path.Combine(Application.persistentDataPath, "VR_Recording.mp4");
        #else
            _savePath = Path.Combine(Application.dataPath, "VR_Recording.mp4");
        #endif
    }

    public void StartRecording()
    {
        if (_isRecording) return;

        _videoWriter = new VideoWriter();
        _videoWriter.open(_savePath, _renderTexture.width, _renderTexture.height, 30, VideoCodec.H264);
        _isRecording = true;
        Debug.Log($"开始录制,保存路径: {_savePath}");
    }

    public void StopRecording()
    {
        if (!_isRecording) return;

        _videoWriter.close();
        _videoWriter = null;
        _isRecording = false;
        Debug.Log("录制完成");
    }

    void Update()
    {
        if (_isRecording && _videoWriter != null)
        {
            RenderTexture.active = _renderTexture;
            var frame = new Texture2D(_renderTexture.width, _renderTexture.height, TextureFormat.RGB24, false);
            frame.ReadPixels(new Rect(0, 0, _renderTexture.width, _renderTexture.height), 0, 0);
            frame.Apply();
            _videoWriter.writeFrame(frame);
            Destroy(frame);
        }
    }

    void OnDestroy()
    {
        Destroy(_renderTexture);
        _videoWriter?.close();
    }
}

注意事项:

  • Android端需在Player Settings中申请WRITE_EXTERNAL_STORAGE权限
  • 若需要双眼拼接画面,可自行处理两个Eye相机的RenderTexture合并

三、第三方Unity包推荐

  • AVPro Video:支持跨平台VR视频录制,可直接捕获VR相机输出,配置灵活,兼容PC/Android
  • Unity Recorder(官方工具):在Package Manager中直接安装,选择「VR Camera」录制模式即可自动处理单眼/双眼输出,操作简单

补充:解决adb录制双透镜问题

若坚持使用adb录制,可在Oculus头显的开发者选项中开启「平面录制」模式,此时adb捕获的是合成后的单眼画面,而非原始双透镜渲染结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 20:15:04