Search Unity

[SOLVED. SOLUTION IN THREAD] Can I override when unity initializes VR?

Discussion in 'AR/VR (XR) Discussion' started by JoelGab, Jul 3, 2019.

  1. JoelGab

    JoelGab

    Joined:
    Jul 17, 2018
    Posts:
    25
    So we have recently migrated to unity's Native VR Support, however, we are experiencing issues where VR initializes in our bootstrap scene, at this point it is not yet VR capable.

    For some insight, our software is capable of VR, but it is not strictly a VR app, so It would be ideal if we could override unity initializing VR.

    Unity does not allow toggling VR Enabled in play mode either, so we can't even just turn that on when we need it.
     
  2. JoelGab

    JoelGab

    Joined:
    Jul 17, 2018
    Posts:
    25
    For anyone interested, I have solved this issue, I just accidentally had the default SDK not set on none.

    So for anyone who wants to control when VR is enabled and disabled it's quite simple. Make sure you first have the top supported SDK as "none".

    Enabling VR on scene load.
    Simply call XRSettings.LoadDeviceByName("NAME OF SDK TO LOAD");

    Disabling VR on scene load.
    Simply call XRSettings.LoadDeviceByName("none");

    This effectively detaches the application from the VR SDK. :)
     
  3. JoelGab

    JoelGab

    Joined:
    Jul 17, 2018
    Posts:
    25
    Small little helper class (It's a singleton). For clearer control over device loading and unloading in unity.

    Code (CSharp):
    1. public class VRSDKLoader : MonoBehaviour
    2.     {
    3.         // Instance
    4.         public static VRSDKLoader Instance;
    5.  
    6.         public bool InitializeVROnAwake = false;
    7.  
    8.         public const string NO_SDK_STRING = "None";
    9.  
    10.         /// <summary>This event is fired when an SDK is successfully loaded.</summary>
    11.         public SDKLoadSuccess OnLoadSDKSuccess = new SDKLoadSuccess();
    12.         /// <summary>This event will fire when all SDKs fail to load.</summary>
    13.         public SDKLoadFail OnLoadSDKFailed = new SDKLoadFail();
    14.  
    15.  
    16.         void Awake()
    17.         {
    18.             // Create an instance as we need to use/start coroutines so this can't just be a static class.
    19.             // Plus you only ever need one of these so a singleton does in fact make sense here.
    20.             if (Instance == null) Instance = this;
    21.             else Destroy(gameObject);
    22.  
    23.             // If true it will just initialize VR on awake.
    24.             if (InitializeVROnAwake)
    25.                 StartCoroutine(LoadAnyAvailableSDK());
    26.         }
    27.  
    28.         /// <summary>
    29.         /// Goes through all the supported SDKs and tries to load them in one by one.
    30.         /// </summary>
    31.         /// <returns></returns>
    32.         public IEnumerator LoadAnyAvailableSDK()
    33.         {
    34.             // Go through all the devices supported.
    35.             foreach (var device in XRSettings.supportedDevices)
    36.             {
    37.                 // wait for the load sdk coroutine to finish on this device (Next Frame).
    38.                 yield return StartCoroutine(LoadSpecificSDK(device));
    39.              
    40.                 // Did it succeed loading in this device.
    41.                 if (XRSettings.loadedDeviceName.Equals(device))
    42.                 {
    43.                     // Yes, Invoke success.
    44.                     OnLoadSDKSuccess.Invoke(device);
    45.                     break;
    46.                 }
    47.             }
    48.  
    49.             // Is there nothing loaded?
    50.             if (string.IsNullOrEmpty(XRSettings.loadedDeviceName))
    51.             {
    52.                 // If so, invoke a failed load and log a warning.
    53.                 OnLoadSDKFailed.Invoke();
    54.                 Debug.LogWarning("No VR SDK Loaded, falling back to desktop");
    55.             }
    56.         }
    57.  
    58.         /// <summary>
    59.         /// Loads a VR SDK from the name.
    60.         /// </summary>
    61.         /// <param name="sdkToLoad">name of SDK to load.</param>
    62.         /// <returns></returns>
    63.         /// <remarks>yield return StartCoroutine(LoadSpecificSDK(sdkToLoad)); will return the frame after the load SDK is called.
    64.         /// This is because the <see cref="XRSettings.LoadDeviceByName(string)"/> method loads the requested device at the begginning of the next frame.</remarks>
    65.         public IEnumerator LoadSpecificSDK(string sdkToLoad)
    66.         {
    67.             XRSettings.LoadDeviceByName(sdkToLoad);
    68.             yield return null;
    69.         }
    70.  
    71.         /// <summary>
    72.         /// Loads the "None" device, which effectively detaches the VR Headset from the application.
    73.         /// </summary>
    74.         public void DetatchVR() => XRSettings.LoadDeviceByName(NO_SDK_STRING);
    75.  
    76.         public class SDKLoadFail : UnityEvent { }
    77.         public class SDKLoadSuccess : UnityEvent<string> { }
    78.     }
     
    linojon likes this.