判断相机是否可用的最有效方法?

时间:2018-04-22 19:17:39

标签: c# unity3d

鉴于不需要实际的相机图像,在Unity中检查物理相机是否可用于C#的最有效方法是什么?

编辑:我目前正在使用此功能,但由于我必须检查每一帧,所以任何改进都很棒:

var image = Frame.CameraImage.AcquireCameraImageBytes();
if (!image.IsAvailable){ //do stuff
}

2 个答案:

答案 0 :(得分:1)

如果您使用的是WebCamTexture,则它具有isPlaying变量。

  

isPlaying 如果相机当前正在播放,则返回。

它还有一个设备列表。

  

设备返回可用设备列表。

我没有使用此功能的经验,但听起来非常相关。

来源:https://docs.unity3d.com/ScriptReference/WebCamTexture.html

答案 1 :(得分:0)

您需要提供更多详细信息。你是什​​么意思'可用'? 您可以检查Camera.allcameras返回场景中所有已启用的摄像头。 您可以检查Camera.current我们当前正在渲染的相机,仅用于低级渲染控制(只读)。

public class Example : MonoBehaviour
{
    //This is Main Camera in the scene
    Camera m_MainCamera;
    //This is the second Camera and is assigned in inspector
    public Camera m_CameraTwo;

    void Start()
    {
        //This gets the Main Camera from the scene
        m_MainCamera = Camera.main;
        //This enables Main Camera
        m_MainCamera.enabled = true;
        //Use this to disable secondary Camera
        m_CameraTwo.enabled = false;
    }
}

您可以在https://docs.unity3d.com/ScriptReference/Camera.html

查看统一文档