Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Unregistering OpenXRLoader via editor script

Discussion in 'VR' started by simtrip, Sep 20, 2023.

  1. simtrip

    simtrip

    Joined:
    Oct 18, 2016
    Posts:
    4
    Hi

    I have a need to automate the process of changing XR settings in order to target different devices (to be called in a headless editor for CI builds.)

    In particular, I need to switch between OpenXRLoader and the OculusLoader. However for this particular case it seems like the OpenXR loader is not being removing itself (despite XRManagerSettings.TryRemoveLoader() returning true.) This is not the same behaviour for all other loaders (e.g ARCore, Unity Mock HMD etc) - they seem to be get removed just fine.

    Is there something additional that needs to happen in order to stop OpenXR from appearing ticked in the list of loaders? It works if I do it manually through the XR Management settings page.

    I'm using Unity 2021.3, OpenXR 1.5.3.

    My code is below with some comments on the output I'm getting.

    Code (CSharp):
    1.         public static void Oculus()
    2.         {
    3.             try
    4.             {
    5.                 AssetDatabase.StartAssetEditing();
    6.                
    7.                 // This method simply wraps AssetDatabase.FindAsset("t:{..}")
    8.                 if (!TryFindXRLoaderAsset<OculusLoader>(out var oculusLoader))
    9.                 {
    10.                     return;
    11.                 }
    12.  
    13.                 var androidSettings = XRGeneralSettingsPerBuildTarget.XRGeneralSettingsForBuildTarget(BuildTargetGroup.Android);
    14.  
    15.                 // Remove all current loaders
    16.                 var activeLoaders = new List<XRLoader>(androidSettings.AssignedSettings.activeLoaders);
    17.                 foreach (var loader in activeLoaders)
    18.                 {
    19.                     androidSettings.AssignedSettings.TryRemoveLoader(loader); // returns true
    20.                     loader.Deinitialize(); // not sure this is required at build time?
    21.                 }
    22.                
    23.                 // Set new loaders
    24.                 var newLoaders = new List<XRLoader> { oculusLoader };
    25.                 androidSettings.AssignedSettings.TrySetLoaders(newLoaders); // returns true, but OpenXR is still there and OculusLoader is not added
    26.                
    27.                 EditorUtility.SetDirty(androidSettings);
    28.                 AssetDatabase.SaveAssets();
    29.                
    30.                 PlayerSettings.Android.minSdkVersion = AndroidSdkVersions.AndroidApiLevel29;
    31.             }
    32.             finally
    33.             {
    34.                 AssetDatabase.SaveAssets();
    35.                 AssetDatabase.StopAssetEditing();
    36.             }
    37.         }
     
  2. simtrip

    simtrip

    Joined:
    Oct 18, 2016
    Posts:
    4
    Nevermind. I had the Project Settings window open at the time. It works as long as it's not open, I imagine due to how the editor checks and saves serialized properties. Strange that it only applies for OpenXR, but this is good enough for me.
     
    dfischer23 likes this.