Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Live stream from iphone camera with Google Cardboard

Discussion in 'AR/VR (XR) Discussion' started by bammoblammo, Jun 10, 2020.

  1. bammoblammo

    bammoblammo

    Joined:
    Jun 8, 2020
    Posts:
    1
    Hi All,

    I am relatively new to XR, and am working with my iPhone, mac, and Google cardboard headset. I would like to access the camera stream from my phone and have it display in google cardboard. In principle I have it working - I followed the tutorial outlined here:
    but just before building I enabled VR, enabled the cardboard SDK, and chose ios as my platform.
    When I launch my app I can see the stereoscopic window and everything looks promising, but then as the camera stream starts it simply comes up full screen, not in two separate windows as I would expect.

    Attached is my cs script that I use to define my camera. Any tips or suggestions would be greatly welcome!

    Thank you!
    B

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7.  
    8. public class PhoneCamera : MonoBehaviour
    9. {
    10.     private bool camAvailable;
    11.     private WebCamTexture backCam;
    12.     private Texture defaultBackground;
    13.  
    14.     public RawImage background;
    15.     public AspectRatioFitter fit;
    16.  
    17.     private void Start()
    18.     {
    19.         defaultBackground = background.texture;
    20.         WebCamDevice[] devices = WebCamTexture.devices;
    21.  
    22.         if (devices.Length == 0)
    23.         {
    24.             Debug.Log("No camera detected");
    25.             camAvailable = false;
    26.             return;
    27.         }
    28.  
    29.         for (int i = 0; i < devices.Length; i++)
    30.         {
    31.             if (!devices[i].isFrontFacing)
    32.             {
    33.                 backCam = new WebCamTexture(devices[i].name, Screen.width, Screen.height);
    34.             }
    35.         }
    36.  
    37.         if(backCam == null)
    38.         {
    39.             Debug.Log("Unable to find back camera");
    40.             return;
    41.         }
    42.  
    43.         backCam.Play();
    44.         background.texture = backCam;
    45.  
    46.         camAvailable = true;
    47.  
    48.     }
    49.  
    50.     private void Update()
    51.     {
    52.         if(!camAvailable)
    53.             return;
    54.  
    55.         float ratio = (float)backCam.width / (float)backCam.height;
    56.         fit.aspectRatio = ratio;
    57.  
    58.         float scaleY = backCam.videoVerticallyMirrored ? -1f: 1f;
    59.         background.rectTransform.localScale = new Vector3(1f, scaleY, 1f);
    60.  
    61.         int orient = -backCam.videoRotationAngle;
    62.         background.rectTransform.localEulerAngles = new Vector3(0, 0, orient);
    63.     }
    64. }
    65.  
     

    Attached Files: