Search Unity

Question XR Plugin Management - toggling VR on and off doesn't work right

Discussion in 'XR Interaction Toolkit and Input' started by RumbleStrut, Dec 14, 2020.

  1. RumbleStrut

    RumbleStrut

    Joined:
    Apr 15, 2017
    Posts:
    1
    I'm using Unity 2019.4 and the latest XR Plugin Management with preview XR Interaction Toolkit 0.10.

    The display activates using StartXR but the XR rig stops working. The view/pose in VR gets stuck and controllers don't work. With startup initialization in the plugin manager enabled, everything works fine until I exit VR and go back into VR where the same problem occurs.

    Am I missing something?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR.Management;
    5.  
    6. public class XRSwitch : MonoBehaviour
    7. {
    8.     public bool VRMode = false;
    9.     private bool started = false;
    10.  
    11.     void Update ()
    12.     {
    13.         if (VRMode)
    14.         {
    15.             if (!started)
    16.             {
    17.                 started = true;
    18.                 StartCoroutine(StartXR());
    19.             }
    20.         } else
    21.         {
    22.             if (started)
    23.             {
    24.                 started = false;
    25.                 StopXR();
    26.             }
    27.         }
    28.     }
    29.  
    30.     IEnumerator StartXR()
    31.     {      
    32.         Debug.Log("Initializing XR...");
    33.         if (XRGeneralSettings.Instance.Manager.activeLoader == null)
    34.         {
    35.             yield return XRGeneralSettings.Instance.Manager.InitializeLoader();
    36.         }
    37.  
    38.  
    39.         if (XRGeneralSettings.Instance.Manager.activeLoader == null)
    40.         {
    41.             Debug.LogError("Initializing XR Failed. Check Editor or Player log for details.");
    42.         }
    43.         else
    44.         {
    45.             Debug.Log("Starting XR...");
    46.             XRGeneralSettings.Instance.Manager.activeLoader.Start();
    47.         }
    48.     }
    49.  
    50.     void StopXR()
    51.     {
    52.         Debug.Log("Stopping XR...");
    53.  
    54.         if (XRGeneralSettings.Instance.Manager.activeLoader != null)
    55.         {
    56.             XRGeneralSettings.Instance.Manager.activeLoader.Stop();
    57.             XRGeneralSettings.Instance.Manager.DeinitializeLoader();
    58.             Debug.Log("XR stopped completely.");
    59.         }
    60.     }
    61. }
    62.  
     
  2. chris-massie

    chris-massie

    Unity Technologies

    Joined:
    Jun 23, 2020
    Posts:
    231
    If you want to repeatedly start and stop the subsystem, don't call to
    Deinitialize
    . You should generally just initialize once and deinitialize once. Then just call to
    StartSubsystems
    or
    StopSubsystems
    as needed in your code when you need to stop or restart VR mode.

    Another user had a similar issue, see my related post here.