Unregistering OpenXRLoader via editor script

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.

        public static void Oculus()
        {
            try
            {
                AssetDatabase.StartAssetEditing();
               
                // This method simply wraps AssetDatabase.FindAsset("t:{..}")
                if (!TryFindXRLoaderAsset<OculusLoader>(out var oculusLoader))
                {
                    return;
                }

                var androidSettings = XRGeneralSettingsPerBuildTarget.XRGeneralSettingsForBuildTarget(BuildTargetGroup.Android);

                // Remove all current loaders
                var activeLoaders = new List<XRLoader>(androidSettings.AssignedSettings.activeLoaders);
                foreach (var loader in activeLoaders)
                {
                    androidSettings.AssignedSettings.TryRemoveLoader(loader); // returns true
                    loader.Deinitialize(); // not sure this is required at build time?
                }
               
                // Set new loaders
                var newLoaders = new List<XRLoader> { oculusLoader };
                androidSettings.AssignedSettings.TrySetLoaders(newLoaders); // returns true, but OpenXR is still there and OculusLoader is not added
               
                EditorUtility.SetDirty(androidSettings);
                AssetDatabase.SaveAssets();
               
                PlayerSettings.Android.minSdkVersion = AndroidSdkVersions.AndroidApiLevel29;
            }
            finally
            {
                AssetDatabase.SaveAssets();
                AssetDatabase.StopAssetEditing();
            }
        }

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.

1 Like