365 lines
14 KiB
C#
365 lines
14 KiB
C#
using System;
|
|
using System.IO;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
using UnityEditor.XR.Management;
|
|
using UnityEditor.XR.OpenXR;
|
|
#endif
|
|
using UnityEngine.XR.Management;
|
|
using UnityEngine.XR.OpenXR.Features;
|
|
using UnityEngine.XR.OpenXR.Features.Mock;
|
|
|
|
namespace UnityEngine.XR.OpenXR.TestTooling
|
|
{
|
|
/// <summary>
|
|
/// Generates blank OpenXR settings and loader assets for testing purposes, while
|
|
/// preserving existing settings to be restored after testing is completed.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Use this class to activate and get OpenXR features, and add OpenXR extension strings.
|
|
///
|
|
/// Instances of this class are generated by <see cref="MockOpenXREnvironment"/> when
|
|
/// setting up a Mock XR environment, so you do not need to create instances of this class directly.
|
|
/// Instead, use <see cref="MockOpenXREnvironment"/> to obtain an instance of the settings used by the environment.
|
|
/// </remarks>
|
|
public class MockOpenXREnvironmentSettings : IDisposable
|
|
{
|
|
static readonly string[] s_TestGeneralSettings = { "Temp", "Test" };
|
|
static readonly string[] s_TempSettingsPath = { "Temp", "Test", "Settings" };
|
|
|
|
string m_TestPathToGeneralSettings;
|
|
string m_TestPathToSettings;
|
|
|
|
string m_OriginalOpenXRExtensionStrings;
|
|
|
|
#if UNITY_EDITOR
|
|
OpenXRLoader m_OpenXrLoader;
|
|
OpenXRSettings m_OpenXrSettings;
|
|
|
|
XRManagerSettings m_TestManagerSettings;
|
|
XRGeneralSettings m_TestXrGeneralSettings;
|
|
XRGeneralSettings m_OriginalGeneralSettings;
|
|
|
|
XRGeneralSettingsPerBuildTarget m_SettingsPerBuildTarget;
|
|
XRGeneralSettingsPerBuildTarget m_OriginalSettingsPerBuildTarget;
|
|
#endif
|
|
|
|
static bool AnyLoaderIsRunning => OpenXRLoaderBase.Instance != null;
|
|
|
|
internal MockOpenXREnvironmentSettings()
|
|
{
|
|
BackupOriginalSettings();
|
|
CreateXrManagementAssets();
|
|
CreateXRLoaderAndOpenXRSettings();
|
|
|
|
#if UNITY_EDITOR
|
|
IPackageSettings2 packageSettings = OpenXRPackageSettings.GetOrCreateInstance();
|
|
packageSettings.OverrideSettingsLocatorFunc(GetMockSettingsForAnyBuildTarget);
|
|
UnityEditor.XR.OpenXR.Features.FeatureHelpers.RefreshFeatures(BuildTargetGroup.Standalone);
|
|
#endif
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
OpenXRSettings GetMockSettingsForAnyBuildTarget(BuildTargetGroup _) => m_OpenXrSettings;
|
|
#endif
|
|
|
|
/// <summary>
|
|
/// Disposes the MockXR environment settings and restores any backed up settings.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This method does nothing if the MockXR environment settings have already been disposed.
|
|
///
|
|
/// Usually, this method will be called automatically by the <see cref="MockOpenXREnvironment"/>
|
|
/// this instance is associated with.
|
|
/// </remarks>
|
|
public void Dispose()
|
|
{
|
|
#if UNITY_EDITOR
|
|
IPackageSettings2 packageSettings = OpenXRPackageSettings.GetOrCreateInstance();
|
|
packageSettings.RestoreDefaultSettingsLocatorFunc();
|
|
UnityEditor.XR.OpenXR.Features.FeatureHelpers.RefreshFeatures(BuildTargetGroup.Standalone);
|
|
#endif
|
|
DestroyTempAssets();
|
|
RestoreOriginalSettings();
|
|
}
|
|
|
|
void BackupOriginalSettings()
|
|
{
|
|
m_OriginalOpenXRExtensionStrings = MockRuntime.Instance.openxrExtensionStrings;
|
|
#if UNITY_EDITOR
|
|
if (XRGeneralSettings.Instance != null)
|
|
{
|
|
m_OriginalGeneralSettings = XRGeneralSettings.Instance;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void RestoreOriginalSettings()
|
|
{
|
|
#if UNITY_EDITOR
|
|
if (m_OriginalGeneralSettings != null)
|
|
{
|
|
XRGeneralSettings.Instance = m_OriginalGeneralSettings;
|
|
}
|
|
#endif
|
|
MockRuntime.Instance.openxrExtensionStrings = m_OriginalOpenXRExtensionStrings;
|
|
}
|
|
|
|
void CreateXrManagementAssets()
|
|
{
|
|
#if UNITY_EDITOR
|
|
// Create mock for platform-specific XR settings
|
|
m_TestPathToGeneralSettings = GetAssetPathForComponents(s_TestGeneralSettings);
|
|
m_TestPathToGeneralSettings = Path.Combine(m_TestPathToGeneralSettings, "Test_XRGeneralSettingsPerBuildTarget.asset");
|
|
if (File.Exists(m_TestPathToGeneralSettings))
|
|
{
|
|
AssetDatabase.DeleteAsset(m_TestPathToGeneralSettings);
|
|
}
|
|
|
|
m_SettingsPerBuildTarget = ScriptableObject.CreateInstance<XRGeneralSettingsPerBuildTarget>();
|
|
AssetDatabase.CreateAsset(m_SettingsPerBuildTarget, m_TestPathToGeneralSettings);
|
|
|
|
// Create mock for general XR settings
|
|
m_TestPathToSettings = GetAssetPathForComponents(s_TempSettingsPath);
|
|
m_TestPathToSettings = Path.Combine(m_TestPathToSettings, "Test_XRGeneralSettings.asset");
|
|
if (File.Exists(m_TestPathToSettings))
|
|
{
|
|
AssetDatabase.DeleteAsset(m_TestPathToSettings);
|
|
}
|
|
|
|
m_TestManagerSettings = ScriptableObject.CreateInstance<XRManagerSettings>();
|
|
|
|
m_TestXrGeneralSettings = ScriptableObject.CreateInstance<XRGeneralSettings>();
|
|
XRGeneralSettings.Instance = m_TestXrGeneralSettings;
|
|
m_TestXrGeneralSettings.Manager = m_TestManagerSettings;
|
|
|
|
m_SettingsPerBuildTarget.SetSettingsForBuildTarget(BuildTargetGroup.Standalone, m_TestXrGeneralSettings);
|
|
m_SettingsPerBuildTarget.SetSettingsForBuildTarget(GetBuildTargetGroup(), m_TestXrGeneralSettings);
|
|
|
|
AssetDatabase.CreateAsset(m_TestXrGeneralSettings, m_TestPathToSettings);
|
|
AssetDatabase.AddObjectToAsset(m_TestManagerSettings, m_TestXrGeneralSettings);
|
|
|
|
AssetDatabase.SaveAssets();
|
|
|
|
EditorBuildSettings.TryGetConfigObject(XRGeneralSettings.k_SettingsKey, out m_OriginalSettingsPerBuildTarget);
|
|
EditorBuildSettings.AddConfigObject(XRGeneralSettings.k_SettingsKey, m_SettingsPerBuildTarget, true);
|
|
#endif
|
|
}
|
|
|
|
void CreateXRLoaderAndOpenXRSettings()
|
|
{
|
|
#if UNITY_EDITOR
|
|
// Setup Loader
|
|
var path = GetAssetPathForComponents(s_TempSettingsPath);
|
|
var loaderPath = Path.Combine(path, $"Test_{nameof(OpenXRLoader)}.asset");
|
|
m_OpenXrLoader = GetOrCreateAsset<OpenXRLoader>(loaderPath);
|
|
|
|
#pragma warning disable CS0618
|
|
m_TestXrGeneralSettings?.Manager.loaders.Clear();
|
|
m_TestXrGeneralSettings?.Manager.loaders.Add(m_OpenXrLoader);
|
|
#pragma warning restore CS0618
|
|
|
|
// Setup OpenXR Settings
|
|
m_OpenXrSettings = ScriptableObject.CreateInstance<OpenXRSettings>();
|
|
m_OpenXrSettings.name = "Mock OpenXRSettings";
|
|
var openxrSettingsPath = Path.Combine(path, "MockOpenXRSettings.asset");
|
|
AssetDatabase.CreateAsset(m_OpenXrSettings, openxrSettingsPath);
|
|
#endif
|
|
}
|
|
|
|
void DestroyTempAssets()
|
|
{
|
|
#if UNITY_EDITOR
|
|
if (m_OriginalSettingsPerBuildTarget != null)
|
|
EditorBuildSettings.AddConfigObject(
|
|
XRGeneralSettings.k_SettingsKey, m_OriginalSettingsPerBuildTarget, true);
|
|
else
|
|
EditorBuildSettings.RemoveConfigObject(XRGeneralSettings.k_SettingsKey);
|
|
|
|
AssetDatabase.SaveAssets();
|
|
m_TestManagerSettings = null;
|
|
m_TestXrGeneralSettings = null;
|
|
m_SettingsPerBuildTarget = null;
|
|
m_OriginalSettingsPerBuildTarget = null;
|
|
AssetDatabase.DeleteAsset(Path.Combine("Assets", "Temp"));
|
|
#endif
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
static string GetAssetPathForComponents(string[] pathComponents, string root = "Assets")
|
|
{
|
|
if (pathComponents.Length <= 0)
|
|
return null;
|
|
|
|
string path = root;
|
|
foreach (var pc in pathComponents)
|
|
{
|
|
string subFolder = Path.Combine(path, pc);
|
|
bool shouldCreate = true;
|
|
foreach (var f in AssetDatabase.GetSubFolders(path))
|
|
{
|
|
if (String.Compare(Path.GetFullPath(f), Path.GetFullPath(subFolder), true) == 0)
|
|
{
|
|
shouldCreate = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (shouldCreate)
|
|
AssetDatabase.CreateFolder(path, pc);
|
|
path = subFolder;
|
|
}
|
|
|
|
return path;
|
|
}
|
|
|
|
static T GetOrCreateAsset<T>(string path) where T : ScriptableObject
|
|
{
|
|
T asset;
|
|
if (!File.Exists(path))
|
|
{
|
|
asset = ScriptableObject.CreateInstance<T>();
|
|
AssetDatabase.CreateAsset(asset, path);
|
|
AssetDatabase.SaveAssets();
|
|
}
|
|
else
|
|
{
|
|
asset = AssetDatabase.LoadAssetAtPath<T>(path);
|
|
}
|
|
|
|
return asset;
|
|
}
|
|
#endif
|
|
|
|
#if UNITY_EDITOR
|
|
/// <summary>
|
|
/// Gets the instance of an OpenXR feature currently set up in the MockXR environment for a given build target group
|
|
/// </summary>
|
|
/// <typeparam name="F">
|
|
/// Type of the OpenXR feature to be retrieved.
|
|
/// The feature must be a subclass of <see cref="OpenXRFeature"/>.
|
|
/// </typeparam>
|
|
/// <param name="buildTargetGroup">Unity BuildTargetGroup</param>
|
|
/// <returns>
|
|
/// The instance of the OpenXR feature or <see langword="null"/> if no feature of the
|
|
/// specified type was found.
|
|
/// </returns>
|
|
public F GetFeature<F>(BuildTargetGroup buildTargetGroup = BuildTargetGroup.Standalone)
|
|
where F : OpenXRFeature
|
|
{
|
|
return OpenXRSettings.GetSettingsForBuildTargetGroup(buildTargetGroup).GetFeature(typeof(F)) as F;
|
|
}
|
|
#else
|
|
/// <summary>
|
|
/// Gets the instance of an OpenXR feature currently set up in the MockXR environment for a given build target group
|
|
/// </summary>
|
|
/// <typeparam name="F">
|
|
/// Type of the OpenXR feature to be retrieved.
|
|
/// The feature must be a subclass of <see cref="OpenXRFeature"/>.
|
|
/// </typeparam>
|
|
/// <returns>
|
|
/// The instance of the OpenXR feature or <see langword="null"/> if no feature of the
|
|
/// specified type was found.
|
|
/// </returns>
|
|
public F GetFeature<F>() where F : OpenXRFeature
|
|
{
|
|
return OpenXRSettings.ActiveBuildTargetInstance.GetFeature(typeof(F)) as F;
|
|
}
|
|
#endif
|
|
|
|
#if UNITY_EDITOR
|
|
/// <summary>
|
|
/// Deactivates all OpenXR features to Mock XR environment for a given build target.
|
|
/// </summary>
|
|
/// <param name="buildTargetGroup">Unity BuildTargetGroup</param>
|
|
public static void DisableAllFeaturesForBuildTarget(BuildTargetGroup buildTargetGroup = BuildTargetGroup.Standalone)
|
|
{
|
|
var settings = OpenXRSettings.GetSettingsForBuildTargetGroup(buildTargetGroup);
|
|
|
|
foreach (var feature in settings.features)
|
|
{
|
|
feature.enabled = false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Activates or deactivates an OpenXR feature to be used in the Mock XR environment for a given build target group.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The feature status can't be changed if any OpenXR loader is running at the moment you call this method.
|
|
/// </remarks>
|
|
/// <typeparam name="F">
|
|
/// Type of the OpenXR feature to be modified.
|
|
/// The feature must be a subclass of <see cref="OpenXRFeature"/>.
|
|
/// </typeparam>
|
|
/// <param name="enable">Set to <c>true</c> to enable the feature; <c>false</c> to disable it</param>
|
|
/// <param name="buildTargetGroup">Unity BuildTargetGroup</param>
|
|
/// <returns>
|
|
/// <c>true</c> if the feature status was changed successfully or already had the desired state.
|
|
/// <c>false</c> if the feature status could not be changed or the feature was not found.
|
|
/// </returns>
|
|
public bool EnableFeature<F>(bool enable, BuildTargetGroup buildTargetGroup = BuildTargetGroup.Standalone)
|
|
#else
|
|
/// <summary>
|
|
/// Activates or deactivates an OpenXR feature to be used in the Mock XR environment for a given build target group.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The feature status can't be changed if any OpenXR loader is running at the moment you call this method.
|
|
/// </remarks>
|
|
/// <typeparam name="F">
|
|
/// Type of the OpenXR feature to be modified.
|
|
/// The feature must be a subclass of <see cref="OpenXRFeature"/>.
|
|
/// </typeparam>
|
|
/// <param name="enable">Set to <c>true</c> to enable the feature; <c>false</c> to disable it</param>
|
|
/// <returns>
|
|
/// <c>true</c> if the feature status was changed successfully or already had the desired state.
|
|
/// <c>false</c> if the feature status could not be changed or the feature was not found.
|
|
/// </returns>
|
|
public bool EnableFeature<F>(bool enable)
|
|
#endif
|
|
where F : OpenXRFeature
|
|
{
|
|
if (AnyLoaderIsRunning)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
var feature = GetFeature<F>(buildTargetGroup);
|
|
#else
|
|
var feature = GetFeature<F>();
|
|
#endif
|
|
if (feature == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
feature.enabled = enable;
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Requests the use of the provided extension to the list of extensions to be requested to the Mock Runtime.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The extensions can only be added before starting the Mock XR environment. Any extension added
|
|
/// afterwards will be ignored.
|
|
///
|
|
/// Note that adding the extension name doesn't ensure that it will be activated, Mock Runtime must
|
|
/// support it in order to work.
|
|
/// </remarks>
|
|
/// <param name="extensionName">Name of the extension to add.</param>
|
|
public void RequestUseExtension(string extensionName)
|
|
{
|
|
MockRuntime.Instance.openxrExtensionStrings += $" {extensionName}";
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
static BuildTargetGroup GetBuildTargetGroup()
|
|
{
|
|
return BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);
|
|
}
|
|
#endif
|
|
}
|
|
}
|