Search Unity

Resolved [XR Management] Controlling the used XRLoader Manually

Discussion in 'VR' started by OwlchemyDawson, Dec 9, 2020.

  1. OwlchemyDawson

    OwlchemyDawson

    Joined:
    Aug 28, 2018
    Posts:
    21
    What is the proper way to manually control the XR loader at start-up using XR Management?

    I currently have XR Management configured to not initialize on start-up and include the Oculus and OpenVR loaders. In my start-up code I set the loader to use by removing the inappropriate one from the XRGeneralSettings.Instance.Manager.loaders collection based on the platform configuration of the application. After setting the loaders collection I then run the Manager's InitializeLoader and StartSubsystems functions. This seems to work properly in editor and the appropriate XRLoader is used; however, since the Manager is a ScriptableObject (XRManagerSettings instance) the modifications to the loaders collection actually change the XR Management configuration in the project which makes me think I am not doing this correctly.

    Based on this post it sounds like there is a better way to handle the manual control of XRLoaders to use but I cannot seem to find another post or any documentation to indicate what that would be. Should I just be creating an instance of the OpenVRLoader or OculusLoader and calling Initialize and Start on them manually? That also feels wrong as it doesn't seem the loader would get the configuration values set via the XR Management UI.

    Here is my current project setup:
    Unity 2019.4.16
    XR Plugin Management 3.2.17
    Oculus XR Plugin 1.6.1
    OpenVR XR Plugin 1.0.1
     
  2. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958
    Technically you are doing it correctly, but note that you are modifying the global XR Plug-in Management state when you do this and so are affecting that directly.

    I assume that you know the loader types you are using if you are targeting specific runtimes as you are. If you don't need to deal with fall through and just care about a specific loader initializing I would just grab the loader you want from the list and manually manage it. There is no need to modify the list in any way and, in fact is an unsupported action to perform at runtime.
     
  3. OwlchemyDawson

    OwlchemyDawson

    Joined:
    Aug 28, 2018
    Posts:
    21
    Thanks, @joejo. Seems odd that such an action is unsupported at runtime. Feels like the API should expose that as a read-only collection then.

    Does that mean pulling the specific loader from the global XR Plug-in Management state loader and just calling Initialize followed by Start on it directly?

    Also, do I need to call Stop and Deinitialize manually during application quit?

    My current code looks something like this:

    Code (CSharp):
    1. Type targetLoaderType = null;
    2.  
    3. switch (platformType)
    4. {
    5.     case PlatformType.SteamVR:
    6.         targetLoaderType = typeof(OpenVRLoader);
    7.         break;
    8.     case PlatformType.Oculus:
    9.         targetLoaderType = typeof(OculusLoader);
    10.         break;
    11. }
    12.  
    13. if (targetLoaderType != null)
    14. {
    15.     List<XRLoader> loaders = XRGeneralSettings.Instance.Manager.loaders;
    16.  
    17.     for (int loaderIndex = loaders.Count - 1; loaderIndex >= 0; --loaderIndex)
    18.     {
    19.         XRLoader loader = loaders[loaderIndex];
    20.  
    21.         if (loader.GetType() != targetLoaderType)
    22.         {
    23.             loaders.RemoveAt(loaderIndex);
    24.         }
    25.     }
    26. }
    27. else
    28. {
    29.     Debug.LogError($"No known loader type available for current platform! Platform: {platformType}");
    30. }
    31.  
    32. yield return XRGeneralSettings.Instance.Manager.InitializeLoader();
    33.  
    34. if (XRGeneralSettings.Instance.Manager.activeLoader != null)
    35. {
    36.     XRGeneralSettings.Instance.Manager.StartSubsystems();
    37.     yield return null;
    38. }
    39. else
    40. {
    41.     Debug.LogError("Failed to initialize XR loader!");
    42. }
    Would I need to maintain any of that or would I just replace the InitializeLoader and StartSubsystems calls with the direct XRLoader equivalents?
     
  4. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958
    > Thanks, @joejo. Seems odd that such an action is unsupported at runtime. Feels like the API should expose that as a read-only collection then.

    We realized that as well and that change is coming in the future.

    > Does that mean pulling the specific loader from the global XR Plug-in Management state loader and just calling Initialize followed by Start on it directly?

    Yes.

    > Also, do I need to call Stop and Deinitialize manually during application quit?

    Yes, to guarantee that things are shutdown and cleaned up correctly at least on the side of the runtime. Tearing down the process on app quit should clean up everything on the Unity side, but I can't guarantee the runtime will clean up completely since we have no control over that.

    > Would I need to maintain any of that or would I just replace the InitializeLoader and StartSubsystems calls with the direct XRLoader equivalents?

    Don't call RemoveAt. There is no need to modify that list since you are doing things manually. Just get the loader instance you want and call the Initialize, Start, Stop, DeInitialize methods directly. If Initialize or Start return false, then you know there is an issue with starting that loader and should clean up and report the fact to the user in some way.
     
  5. OwlchemyDawson

    OwlchemyDawson

    Joined:
    Aug 28, 2018
    Posts:
    21
    Thanks for the help on this @joejo. I was able to get a solution implemented that works in editor and builds.

    Unfortunately, I have run into a heap corruption issue from the Valve OpenVR XR Plugin that causes the Unity Editor to just close without resulting in caught crash. I've submitted a pull request to get the plugin fixed but it is currently preventing me from rolling the XR Management implementation out to the rest of my team.
     
  6. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958
    I'm glad to hear you were able to move forward on this. Sorry to hear about the issues with the Valve plug in. Hopefully they can help you with that.