Unity PhotoCapture 的 SupportedResolutions 为空

SupportedResolutions of Unitys PhotoCapture is empty

我正在使用 Unity 为 HoloLens 编写应用程序。在应用程序中,我想要一个按钮,它在单击时记录图像。为了拍照,我尝试使用 Unitys PhotoCapture(如此处所述:https://docs.unity3d.com/560/Documentation/ScriptReference/VR.WSA.WebCam.PhotoCapture.html or here: https://docs.microsoft.com/en-us/windows/mixed-reality/locatable-camera-in-unity)。 当我 运行 代码并单击按钮时出现以下错误: System.InvalidOperationException: 'Sequence contains no elements' 关于以下行: cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First(); 通过调试我发现 SupportedResolutions 是空的。我该如何解决这个问题?

其余代码:

using System.Collections;
using System.Collections.Generic;
using System.Linq;

using UnityEngine;
using UnityEngine.XR.WSA.WebCam;
using UnityEngine.XR.WSA.Input;

using HoloToolkit.Unity.InputModule;

public class Record : MonoBehaviour, IInputClickHandler {

    PhotoCapture photoCaptureObject = null;
    Resolution cameraResolution;

    void Start () {}

    public void OnInputClicked(InputClickedEventData eventData)
    {
        cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
        // Create a PhotoCapture object and so on
    }

(在 Unity 中启用麦克风和网络摄像头作为输入)

在尝试访问其分辨率之前需要实例化 photoCaptureObject。

如您所说,您需要先调用 PhotoCapture.CreateAsync 才能填充 PhotoCapture。

您可以获得代码示例 here 和下面的代码片段:

private void Start()
{
    PhotoCapture.CreateAsync(false, this.OnPhotoCreated);
}

// This method store the PhotoCapture object just created and retrieve the high quality
// available for the camera and then request to start capturing the photo with the
// given camera parameters.
private void OnPhotoCreated(PhotoCapture captureObject)
{    
    Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();

    CameraParameters c = new CameraParameters()
    {
        hologramOpacity = 0.0f,
        cameraResolutionWidth = cameraResolution.width,
        cameraResolutionHeight = cameraResolution.height,
        pixelFormat = CapturePixelFormat.BGRA32
    };

//    captureObject.StartPhotoModeAsync(c, this.OnPhotoModeStarted);
}