Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Using VRSettings.LoadDeviceByName to toggle Cardboard enabled causing android to crash

Discussion in 'AR/VR (XR) Discussion' started by MadMan6787, Mar 22, 2017.

  1. MadMan6787

    MadMan6787

    Joined:
    Oct 27, 2013
    Posts:
    3
    Hi,

    I am creating a VR app for Android and iOS that is basically a menu system and 360 degree movie player for a series of short films.
    I want the user to be able to choose to either use Google Cardboard or or just to view the app in monoscopic by rotating the phone and using the gyro data. The current problem is that if I try to load the cardboard device at runtime on my android phone the app crashes around 3/5 times. As far as I am aware there seems to be no difference to how I interact with the app when it crashes or works.
    When the app crashes the screen just goes black and the app is closed by the phone and I get a pop up saying the app has stopped and I can click to "close app".
    I am testing on a Nexus 6P using the native cardboard functionality in the beta 5.6 unity. I'm not using the GVR plugin but I did try importing it and building my app and it didn't help.

    To do this I have set up the Player settings to enable virtual reality and added "None" and "Cardboard" to the virtual reality SDK's so that the app loads with virtual reality disabled and I can enable it based on the users first choice when the app starts.

    I am using the coroutine written below to load the device and then set the vr settings to enabled

    Code (CSharp):
    1. private IEnumerator SetVRDevice(string device, bool vrEnabled) {
    2.         VRSettings.LoadDeviceByName(device);
    3.         yield return null;
    4.         VRSettings.enabled = vrEnabled;
    5.     }
    I am calling the coroutine from the start function in the same script to set the device to "None" and in a function fired on a button press to set the device to "cardboard"

    Code (CSharp):
    1. void Start () {
    2.         StartCoroutine(SetVRDevice("None", false));
    3.         //rest of start function stuff
    4. }
    Code (CSharp):
    1. StartCoroutine(SetVRDevice("cardboard", true));
    I have tried building the app from 5.6.0b10, 5.6.0b11 and 5.6.0f1 and the all end in the same problem: the phone crashes just over half the time.

    I wasn't previously setting the VR device to none in the start function but doing this has seemed to decrease the frequency that the app crashes so I have kept it in for now.


    I have searched the forums on here and have found a couple of people trying to do the same thing but the fixes posted on those forums haven't fixed my problem. I can post more information if necessary but I'm not sure what is relevant.
    Has anyone else came across similar issues? Any recommendations on what I could try?
     
  2. MadMan6787

    MadMan6787

    Joined:
    Oct 27, 2013
    Posts:
    3
    When checking the logcat with adb I get this back

    Code (CSharp):
    1. 03-22 14:37:33.658  2202  2231 E Unity   : [EGL] Failed to create window surface: EGL_BAD_ALLOC: EGL failed to allocate resources for the requested operation.
    2. 03-22 14:37:33.658  2202  2231 E Unity   :
    3. 03-22 14:37:33.658  2202  2231 E Unity   : (Filename: ./Runtime/GfxDevice/egl/WindowContextEGL.cpp Line: 141)
    4. 03-22 14:37:33.658  2202  2231 E Unity   :
    5. 03-22 14:37:33.658  2202  2231 I Unity   : Skipped rendering frame because GfxDevice is in invalid state (device lost)
    6. 03-22 14:37:33.658  2202  2231 I Unity   :
    7. 03-22 14:37:33.658  2202  2231 I Unity   : (Filename:  Line: 772)
    8. 03-22 14:37:33.658  2202  2231 I Unity   :
    9. 03-22 14:37:33.819  2202  2231 I Unity   : Skipped rendering frame because GfxDevice is in invalid state (device lost)
    10. 03-22 14:37:33.819  2202  2231 I Unity   :
    11. 03-22 14:37:33.819  2202  2231 I Unity   : (Filename:  Line: 772)
    12. 03-22 14:37:33.819  2202  2231 I Unity   :
    Not sure if this is helpful as I can't make sense of it. This is the log that is printed starting when I click the button that should switch the VR mode to cardboard.
     
  3. MadMan6787

    MadMan6787

    Joined:
    Oct 27, 2013
    Posts:
    3
  4. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958
    Yes, according to the docs you always need to wait at least one Update/Frame between loading the device and enabling vrsettings. The check for device loaded and yield if not seems like a good way to catch a case where loading the device may for some reason take longer than one frame. Usually when I do this I actually do the switching in a MonoBehaviour::Update where I can make sure that I don't enable vr settings until the next Update after the load device is called.
     
    Mistabullit likes this.
  5. xipeluife

    xipeluife

    Joined:
    Jan 15, 2013
    Posts:
    14
  6. cariaga

    cariaga

    Joined:
    Feb 6, 2013
    Posts:
    13
    this issue is back in 2017.2 and 2017.3
     
  7. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958
    When you say it's back, what exactly are you seeing? Do you have logcat output showing a crash log? Are you doing the correct wait a frame procedure as outlined above?
     
  8. cariaga

    cariaga

    Joined:
    Feb 6, 2013
    Posts:
    13
    it crashes on vr start or awake even adding a waitframe or long seconds it goes back to non vr mode
     
  9. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958
    Some more Qs:

    Can you pare your repro case down to the simplest code?
    What is logcat telling you about errors being spit out?
    What device are you using?
     
  10. cariaga

    cariaga

    Joined:
    Feb 6, 2013
    Posts:
    13
    i've tested in on devices android api 21 to 24 but my primary device is a note 3 samsung
    the error is produced in 2017.2 and 2017.3 but the error doesn't seam to pop up in 2018.1.b9 but will still crash on vr mode

    in the sdk manager
    i also have all the android build tools installed and sdk from api 21 to api 27
    it doesn't seam to be project related because another project of mine got affected

    error
    Could not recreate VR window because GfxDevice is in an invalid state

    i even tried adding time delays

    IEnumerator testco()
    {

    yield return new WaitForSeconds(3);
    UnityEngine.XR.XRSettings.enabled = true;
    yield return new WaitForSeconds(3);
    Screen.orientation = ScreenOrientation.LandscapeLeft;
    yield return new WaitForSeconds(3);
    UnityEngine.XR.XRSettings.LoadDeviceByName("cardboard");


    yield return new WaitForSeconds(3);
    UnityEngine.XR.XRSettings.enabled = true;

    }
     
    Last edited: Mar 2, 2018
  11. cariaga

    cariaga

    Joined:
    Feb 6, 2013
    Posts:
    13
    after testing i finally got it working again but it doesn't work with .net 4.6 only 3.5
    i was able to fix it by adding "NONE" not just the cardboard on 2.1.1f . but it dosn't work with the current beta 2018.
    i assume the cause of the problem is when i tried to update my unity version
     
    Last edited: Mar 2, 2018
  12. Benjams

    Benjams

    Unity Technologies

    Joined:
    Oct 27, 2017
    Posts:
    20
    Try disabling VR before changing Screen.orientation:


    UnityEngine.XR.XRSettings.LoadDeviceByName("");
    yield return null;


    This will ensure that your app is in 2D mode prior to attempting to change the screen orientation. This might be why you're seeing correct behavior when adding "none" to your SDK list.
     
  13. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958
    And don't set enabled to true before you load the device.