1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

UnityのEditorスクリプトでXR Plug-in ManagementのPlug-in Providersをセットする

Posted at

UnityのEditorスクリプトでXR Plug-in ManagementのPlug-in Providersをセットする方法です。
Screenshot 2024-05-31 at 20.59.02.png

Assemble Definitionを適切に設定
Screenshot 2024-05-31 at 20.59.22.png

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}.");
    }
}

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?