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

Unity中PhotoCapture的SupportedResolutions为空,HoloLens拍照报错求助

解决HoloLens Unity开发中PhotoCapture报错及SupportedResolutions为空的问题

嘿,我之前在HoloLens项目里踩过一模一样的坑!这种情况大多是权限配置、初始化流程或者Unity项目设置出了问题,咱们一步步来排查解决:

1. 先搞定相机权限配置

HoloLens对硬件权限管控很严,没开权限的话相机根本没法正常工作:

  • 打开Unity的Player Settings,切换到Windows平台标签
  • 在Capabilities里必须勾选WebCam,顺带把Microphone也勾上(有些版本的XR插件依赖这个权限,哪怕你不用麦克风)
  • 确认Package Manager里的Windows XR Plugin是最新稳定版,旧版本可能有兼容性问题

2. 修正PhotoCapture的初始化逻辑

你大概率是在设备还没完成相机初始化时就去获取SupportedResolutions了,这时候肯定是空的。一定要用异步回调的方式等待相机准备就绪,给你个靠谱的代码示例:

private PhotoCapture _photoCapture;

// 按钮点击触发的方法
public void OnCaptureButtonClicked()
{
    // 先异步创建PhotoCapture实例,不要直接拿分辨率
    PhotoCapture.CreateAsync(false, OnPhotoCaptureInitialized);
}

private void OnPhotoCaptureInitialized(PhotoCapture captureInstance)
{
    _photoCapture = captureInstance;

    // 现在再获取分辨率就不会为空了
    var availableResolutions = PhotoCapture.SupportedResolutions;
    if (availableResolutions.Count == 0)
    {
        Debug.LogError("还是没拿到分辨率!检查权限和设备是否支持相机");
        _photoCapture.Dispose();
        _photoCapture = null;
        return;
    }

    // 选一个合适的分辨率(比如最高清的)
    Resolution targetRes = availableResolutions.OrderByDescending(res => res.width * res.height).First();

    // 配置拍照参数
    CameraParameters captureParams = new CameraParameters
    {
        hologramOpacity = 0f, // 隐藏全息内容,只拍现实画面
        cameraResolutionWidth = targetRes.width,
        cameraResolutionHeight = targetRes.height,
        pixelFormat = CapturePixelFormat.BGRA32
    };

    // 异步进入拍照模式
    _photoCapture.StartPhotoModeAsync(captureParams, OnPhotoModeStarted);
}

private void OnPhotoModeStarted(PhotoCapture.PhotoCaptureResult result)
{
    if (result.success)
    {
        // 开始拍照
        _photoCapture.TakePhotoAsync(OnPhotoCaptured);
    }
    else
    {
        Debug.LogError($"进入拍照模式失败:{result.resultType}");
        CleanupCapture();
    }
}

private void OnPhotoCaptured(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame frame)
{
    if (result.success)
    {
        // 处理拍摄到的图片,比如转成Texture2D保存
        Texture2D captureTexture = new Texture2D(frame.width, frame.height);
        frame.UploadImageDataToTexture(captureTexture);
        
        // 这里可以加保存到本地或上传的逻辑
        Debug.Log("照片拍摄成功!");
    }
    else
    {
        Debug.LogError($"拍照失败:{result.resultType}");
    }

    // 结束拍照模式并释放资源
    CleanupCapture();
}

private void CleanupCapture()
{
    if (_photoCapture != null)
    {
        _photoCapture.StopPhotoModeAsync(_ => 
        {
            _photoCapture.Dispose();
            _photoCapture = null;
        });
    }
}

3. 检查Unity项目的核心设置

  • 确认Player Settings里的Target Device选的是HoloLens,Architecture选ARM64(HoloLens 2的标准架构)
  • 在XR Plug-in Management中启用Windows Mixed Reality,确保XR管线正常工作

4. 测试时的小细节

  • 尽量用真实HoloLens设备测试,模拟器的虚拟相机有时候会出现分辨率为空的bug
  • 第一次运行应用时,一定要在设备上允许相机权限,不然会直接失败

至于你碰到的System.InvalidOperationException,大概率是因为SupportedResolutions为空时,你直接去创建了CameraParameters,导致参数无效触发了异常,按照上面的流程先确保拿到有效分辨率再往下走就不会有问题了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:15:46