UnityのEditorスクリプトでXR Plug-in ManagementのPlug-in Providersをセットする方法です。
using UnityEngine;
using System;
using System.Linq;
using UnityEditor;
using UnityEditor.XR.Management;
using UnityEngine.XR.Management;
public class EnablePluginProviders : MonoBehaviour
{
[MenuItem("Tools/Enable XRPlugin")]
public static void EnableOpenXR()
{
EnableXRPlugin(BuildTargetGroup.Standalone, typeof(UnityEngine.XR.OpenXR.OpenXRLoader));
EnableXRPlugin(BuildTargetGroup.Android, typeof(UnityEngine.XR.OpenXR.OpenXRLoader));
EnableXRPlugin(BuildTargetGroup.VisionOS, typeof(UnityEngine.XR.VisionOS.VisionOSLoader));
}
public static void EnableXRPlugin(BuildTargetGroup buildTargetGroup, Type loaderType)
{
// Get the current XRGeneralSettings instance
XRGeneralSettings xrGeneralSettings = XRGeneralSettingsPerBuildTarget.XRGeneralSettingsForBuildTarget(buildTargetGroup);
if (xrGeneralSettings == null)
{
Debug.LogError("XRGeneralSettings is null. Make sure XR Plugin Management is installed and configured.");
return;
}
// Get or create the XRManagerSettings instance
XRManagerSettings xrManagerSettings = xrGeneralSettings.AssignedSettings;
if (xrManagerSettings == null)
{
xrManagerSettings = ScriptableObject.CreateInstance<XRManagerSettings>();
xrGeneralSettings.AssignedSettings = xrManagerSettings;
}
// Enable the specified loader if it's not already enabled
if (!xrManagerSettings.activeLoaders.Any(loader => loader.GetType() == loaderType))
{
var xrLoader = ScriptableObject.CreateInstance(loaderType) as XRLoader;
xrManagerSettings.TryAddLoader(xrLoader);
}
// Set the initialization mode to OnDemand or Automatic as needed
xrManagerSettings.automaticLoading = true;
xrManagerSettings.automaticRunning = true;
// Save the changes
EditorUtility.SetDirty(xrGeneralSettings);
EditorUtility.SetDirty(xrManagerSettings);
AssetDatabase.SaveAssets();
Debug.Log($"{loaderType.Name} has been enabled successfully for {buildTargetGroup}.");
}
}