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

如何在Unity中使用ARCore录制视频?求高效可行方案

解决Unity AR应用录屏帧率低/内存占用高的方案

哥们,我完全懂你这种被录屏帧率卡到崩溃的感觉!之前做AR项目时也踩过类似的坑,试过逐帧截图、存纹理数组这些笨办法,结果要么帧率低到没法看,要么内存直接爆掉。给你几个亲测有效的方案,应该能解决你的问题:

方案1:使用Unity原生MediaRecorder API(Unity 2020+)

这是目前最推荐的方案,Unity从2020版本开始提供了硬件加速的原生录屏API,不需要自己处理逐帧截图或纹理存储,直接调用系统级的编码能力,内存占用极低,帧率能稳定在30/60帧。

核心代码示例

using UnityEngine;
using UnityEngine.Media;

public class ARScreenRecorder : MonoBehaviour
{
    private MediaRecorder _recorder;
    private bool _isRecording = false;

    void Start()
    {
        // 配置录制参数,可根据AR场景需求调整
        var recordSettings = new MediaRecorderSettings
        {
            outputPath = $"{Application.persistentDataPath}/AR_Recording.mp4",
            videoWidth = Screen.width,
            videoHeight = Screen.height,
            videoFrameRate = 30,
            videoBitRate = 8000000, // 8Mbps,平衡画质和性能
            audioEnabled = false // 如果不需要录声音可以关闭
        };

        _recorder = new MediaRecorder(recordSettings);
        _recorder.Prepare();
    }

    // 绑定UI按钮的开始/停止录制方法
    public void ToggleRecording()
    {
        if (!_isRecording)
        {
            _recorder.Start();
            _isRecording = true;
            Debug.Log("✅ 开始录制AR画面");
        }
        else
        {
            _recorder.Stop();
            _isRecording = false;
            Debug.Log($"✅ 录制结束,文件路径:{_recorder.settings.outputPath}");
        }
    }

    void OnDestroy()
    {
        _recorder?.Dispose();
    }
}

优势

  • 原生硬件加速,编码效率极高,几乎不占用额外CPU/GPU资源
  • 不需要手动处理纹理或帧数据,避免内存暴涨
  • 支持自定义分辨率、帧率、码率,适配不同移动端性能

方案2:AR Foundation专属VideoCapture(适配旧版Unity)

如果你的项目用的是AR Foundation且Unity版本低于2020,推荐用VideoCapture API,它是专门为AR场景优化的,能直接捕获AR相机的混合画面(真实场景+虚拟物体),不需要额外渲染处理。

核心代码示例

using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

public class ARVideoCapture : MonoBehaviour
{
    private VideoCapture _videoCapture;
    private bool _isRecording = false;

    void Start()
    {
        // 异步创建VideoCapture实例
        VideoCapture.CreateAsync(false, OnVideoCaptureCreated);
    }

    void OnVideoCaptureCreated(VideoCapture capture)
    {
        if (capture == null)
        {
            Debug.LogError("❌ 无法创建VideoCapture实例,请检查AR权限");
            return;
        }

        _videoCapture = capture;

        // 配置AR相机参数,确保虚拟物体能被录制
        var cameraParams = new CameraParameters
        {
            hologramOpacity = 0f, // 显示AR虚拟内容,设为0
            cameraResolutionWidth = Screen.width,
            cameraResolutionHeight = Screen.height,
            frameRate = 30
        };

        // 开始录制
        _videoCapture.StartRecordingAsync(
            $"{Application.persistentDataPath}/AR_Record.mp4",
            cameraParams,
            OnRecordingStarted
        );
    }

    void OnRecordingStarted(VideoCaptureResult result)
    {
        _isRecording = result.success;
        Debug.Log(result.success ? "✅ AR录制启动成功" : "❌ 录制启动失败");
    }

    // 停止录制方法
    public void StopRecording()
    {
        if (_isRecording && _videoCapture != null)
        {
            _videoCapture.StopRecordingAsync(OnRecordingStopped);
        }
    }

    void OnRecordingStopped(VideoCaptureResult result)
    {
        _isRecording = false;
        if (result.success)
        {
            Debug.Log($"✅ 录制完成,文件路径:{Application.persistentDataPath}/AR_Record.mp4");
        }
        _videoCapture?.Dispose();
        _videoCapture = null;
    }

    void OnDestroy()
    {
        _videoCapture?.Dispose();
    }
}

优势

  • 完美适配AR Foundation的相机逻辑,不需要额外处理渲染纹理
  • 针对移动端AR场景做了性能优化,帧率稳定
  • 支持控制是否录制声音、虚拟物体透明度等AR专属参数

额外优化建议

  1. 降低录制分辨率:如果不需要4K画质,用1080p甚至720p能大幅降低编码压力
  2. 调整码率:码率越高画质越好,但性能消耗也大,移动端建议5-10Mbps
  3. 录制时临时优化AR渲染:比如关闭抗锯齿、降低虚拟物体的纹理分辨率、关闭不必要的特效,录完再恢复
  4. 权限检查:确保移动端已经授予相机、存储权限,否则可能导致录制失败

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:37:41