简体   繁体   English

Unity3d检查用户是否已授权设备摄像头

[英]Unity3d check if user has authorized device camera

I have developed an app with Vuforia for iOS and Android, when the app is run for the first time, the user is prompted to authorize the use of the device's camera. 我开发了一个适用于iOS和Android的Vuforia应用程序,当应用程序第一次运行时,系统会提示用户授权使用设备的相机。

If the user does not authorize the camera, when the user navigates to the "capture" scene, the camera view is black. 如果用户未授权摄像机,则当用户导航到“捕获”场景时,摄像机视图为黑色。

I have looked at the Untiy3D Documentation and found the Application.HasUserAuthorization class, but if I understand correctly, this only works on the Unity web player? 我查看了Untiy3D文档并找到了Application.HasUserAuthorization类,但如果我理解正确,这只适用于Unity网络播放器?

How can I check if the user has allowed the use of the camera in Unity3D (c#). 如何检查用户是否允许在Unity3D中使用相机(c#)。

Any suggestions would be greatly appreciated! 任何建议将不胜感激! TIA! TIA!

As a user on Android I get prompted when installing the apk that it will use the Camera, once I accept that is it. 作为Android上的用户,我在安装apk时会收到提示,它会使用相机,一旦我接受就是这样。 As a developer I don't need to check. 作为开发人员,我不需要检查。

This is how I start the camera using Vuforia API. 这就是我使用Vuforia API启动相机的方法。

 CameraDevice.Instance.Stop();
 CameraDevice.Instance.Init(CameraDevice.CameraDirection.CAMERA_FRONT);
 CameraDevice.Instance.Start();

CameraDevice is a Singleton that you can call. CameraDevice是一个可以调用的Singleton。

Application.RequestUserAuthorization apparently works for any PC or mobile device camera. Application.RequestUserAuthorization显然适用于任何PC或移动设备相机。 I am using it and the NatCam plugin for Unity3D on iPhone with the following script to check / prompt for user camera permissions. 我在iPhone上使用它和Unity3D的NatCam插件,并使用以下脚本检查/提示用户摄像头权限。 The while() loop is to allow them to take their time giving permission. while()循环允许他们花时间给予许可。

int cameraPermission = 0;

// Use this for initialization
public override void Start () {

    if (!Application.HasUserAuthorization (UserAuthorization.WebCam)) {
        // request camera use
        Application.RequestUserAuthorization (UserAuthorization.WebCam);
    } else {
        cameraPermission = 1;
    }

    // if we don't have permission, wait until we do 
    while (cameraPermission != 1) {
        // wait a sec
        System.Threading.Thread.Sleep(1000);
        // if user enables preference
        if (Application.HasUserAuthorization (UserAuthorization.WebCam)) {
            // proceed to start app
            cameraPermission = 1;
        }
    }

    // Start base
    base.Start ();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM