Search Unity

Android rear camera not found when XR->Cardboard enabled

Discussion in 'AR/VR (XR) Discussion' started by bitbang3r, Oct 9, 2018.

  1. bitbang3r

    bitbang3r

    Joined:
    Sep 22, 2018
    Posts:
    12
    I'm attempting to add functionality to my app to allow the user to "peek" at the real world using the phone's rear camera (Android phone, cardboard-type non-Daydream headpiece).

    I followed N3K EN's tutorial (embedded at bottom of post) and it worked perfectly (code below)... until I went into PlayerSettings->XR Settings, checked "Virtual Reality Supported", and added "Cardboard" as a Virtual Reality SDK. When I ran it after making the change, no cameras were found.

    What do I have to do differently to allow the camera to work when the phone is running as a CardboardVR device?

    If it matters, the phone in question is a Nexus 6P (rooted, Android 8.1.0), and I'm running Unity 2018.2.11f1.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PhoneCamera : MonoBehaviour {
    7.  
    8.     private bool camAvailable = false;
    9.     private WebCamTexture backCam;
    10.     private Texture defaultBackground;
    11.  
    12.     public RawImage background;
    13.     public AspectRatioFitter fit;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.         defaultBackground = background.texture;
    18.         WebCamDevice[] devices = WebCamTexture.devices;
    19.         if (devices.Length == 0) {
    20.             Debug.Log("no camera detected");
    21.             return;
    22.         }
    23.         for (int x=0; x < devices.Length; x++) {
    24.             if (devices[x].isFrontFacing == false) {
    25.                 backCam = new WebCamTexture(devices[x].name, Screen.width, Screen.height);
    26.                 break;
    27.             }
    28.         }
    29.         if (backCam == null) {
    30.             Debug.Log("no rear-facing camera detected");
    31.             return;
    32.         }
    33.         backCam.Play();
    34.         background.texture = backCam;
    35.         camAvailable = true;
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void Update () {
    40.         if (!camAvailable)
    41.             return;
    42.  
    43.         float ratio = (float)backCam.width / (float)backCam.height;
    44.         fit.aspectRatio = ratio;
    45.         float scaleY = backCam.videoVerticallyMirrored ? -1f : 1f;
    46.         background.rectTransform.localScale = new Vector3(1f, scaleY, 1f);
    47.         int orient = -backCam.videoRotationAngle;
    48.         background.rectTransform.localEulerAngles = new Vector3(0, 0, orient);
    49.  
    50.     }
    51. }
    (
    )
     
  2. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958
    Are your permissions correct in your manifest after enabling VR?
     
  3. bitbang3r

    bitbang3r

    Joined:
    Sep 22, 2018
    Posts:
    12
    That's a good question. Probably not. I remember seeing Unity Player ask for permission (and be granted it) automatically when I ran it without VR, and just kind of took for granted that if Unity did it for non-VR Android apps, it would do it automatically for VR, too.

    Where do I need to look, and what do I need to explicitly set?
     
  4. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958
    Try looking in Player Settings to see if it's set in the same place as other permissions. You can also check in Settings->Apps->App Permissions and see if your app has the Camera permission as well.
     
  5. bitbang3r

    bitbang3r

    Joined:
    Sep 22, 2018
    Posts:
    12
    Hmmm... now I'm officially confused.

    According to the page about Android permissions in Unity's documentation...
    1. Unity automatically declares the permissions for Camera if something uses WebCamTexture, HOWEVER...
    2. Further down the same page, it says that Unity doesn't support API23+ ("Android 6.0/Marshmallow") runtime permissions.
    Which is weird, because I was asked to grant camera permission the first time I ran it (but before I enabled XR with Cardboard). It almost looks like Unity quietly implemented runtime permissions for non-XR without updating the docs, but not for XR-using apps.
     
  6. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958
    Did you check your built APK or the installed application to see if the app is actually requesting/declaring the Camera permission anymore?
     
  7. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958