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

Question Enable/Disable VR at runtime using OpenXR

Discussion in 'VR' started by LSerafino, Nov 9, 2022.

  1. LSerafino

    LSerafino

    Joined:
    Dec 1, 2020
    Posts:
    2
    I'm using XR Plugin Management 4.2.1 and OpenXR 1.5.3 on Unity 2020.3.39f1.
    I'm using only OpenXRLoader, that's why i'm using index 0.
    Code (CSharp):
    1.  
    2.         if (m_SelectedXRLoader == null)
    3.         {
    4.             m_SelectedXRLoader = XRGeneralSettings.Instance.Manager.activeLoaders[0];
    5.         }
    6.  
    7.         var initSuccess = m_SelectedXRLoader.Initialize();
    8.         if (!initSuccess)
    9.         {
    10.             Debug.LogWarning("Error initializing selected loader.");
    11.         }
    12.  
    When i call the Initialize(), if my device is not connected i got the warning as i want, but also a log from
    OpenXRRestarter saying "Please make sure the device is connected. Will try to restart xr periodically".
    Then a loop starts giving always same error:
    Code (CSharp):
    1. Failure to restart OpenXRLoader after shutdown.
    2. UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)
    Is there a way to avoid this loop?
    Thank you.
     
    Armegalo and Ony like this.
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,971
    This error will loop as long as no HMD is active.
    As far as I know there is no easy way around it, unless you dig into the source code.
    You can file a bug report for it, since for usecases like you this loop can be very annoying
     
    Armegalo likes this.
  3. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    yeah this began with the previous version of unity and it is annoying because it does a CPU spike on your game every 5 seconds
     
    Armegalo and Ony like this.
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,971
    Maybe disable 'enable xr on startup' and only enable it when you want to start VR?
     
  5. LSerafino

    LSerafino

    Joined:
    Dec 1, 2020
    Posts:
    2
    That's what we are doing, so is not a solution, the issue occuring everytime we try to start vr service while the device is not connected. If we set "enable xr on startup" to true, the loop starts when the app starts.
     
    Ony likes this.
  6. Kroporo

    Kroporo

    Joined:
    Dec 5, 2012
    Posts:
    1
    I've written a small workaround for the issue while waiting for a proper fix being release. The bug report I wrote was marked as a duplicate unfortunately so who knows the status of it.

    Simply drop this class into an editor script folder of your choice:
    https://gist.github.com/Kroporo/f7201d7c9ce6dd015a461992c62cb946

    You'll need to modify how it hooks if you want this behavior in a build, but it works fine in the editor.
    I also fully expect this script to break with updates to OpenXR, but then we hopefully won't need it anymore.
    Tested with Unity 2021.3.13f1
     
    HiziProjects and Ony like this.
  7. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,971
    1.6 also will have this fixed!
    Got a reply from the QA team
     
    Armegalo and Ony like this.
  8. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    any mention on when 1.6 will be out? this CPU spike is pretty annoying
     
    Ony likes this.
  9. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,971
    Sadly not. I asked but no reply yet
     
  10. aquadragon3d

    aquadragon3d

    Unity Technologies

    Joined:
    Sep 20, 2021
    Posts:
    11
    OpenXR 1.6 is still in progress, we'll let you know when it goes live.
     
    DevDunk likes this.
  11. alloystorm

    alloystorm

    Joined:
    Jul 25, 2019
    Posts:
    88
    Having the same issue but I found that you can revert back to OpenXR 1.4.2 to solve this issue.

    Just close your project and go to "Packages" folder and edit both manifest and packages-lock file to change version of OpenXR plugin back to 1.4.2.
     
  12. aquadragon3d

    aquadragon3d

    Unity Technologies

    Joined:
    Sep 20, 2021
    Posts:
    11
    Hi all, OpenXR 1.6 should now be live. Please let us know if you have further issues. Thanks!
     
    Ony and DevDunk like this.
  13. Hexalted

    Hexalted

    Joined:
    Nov 16, 2017
    Posts:
    23
    1.6 was not listed in the package manager so I manually changed the version in the manifest, and the errors disappeared indeed. Dynamically loading the XR seems to be finally working (no headset to test at the moment but at least not crashing anymore)
     
  14. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    same here, the update doesn't show up, i had to manually modify the manifest