XRGeneralSettingsPerBuildTarget.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.XR.Management;
  6. using UnityEditor.XR.Management.Metadata;
  7. namespace UnityEditor.XR.Management
  8. {
  9. #if UNITY_EDITOR
  10. [InitializeOnLoad]
  11. #endif
  12. /// <summary>Container class that holds general settings for each build target group installed in Unity.</summary>
  13. public class XRGeneralSettingsPerBuildTarget : ScriptableObject, ISerializationCallbackReceiver
  14. {
  15. [SerializeField]
  16. List<BuildTargetGroup> Keys = new List<BuildTargetGroup>();
  17. [SerializeField]
  18. List<XRGeneralSettings> Values = new List<XRGeneralSettings>();
  19. Dictionary<BuildTargetGroup, XRGeneralSettings> Settings = new Dictionary<BuildTargetGroup, XRGeneralSettings>();
  20. #if UNITY_EDITOR
  21. // Simple class to give us updates when the asset database changes.
  22. class AssetCallbacks : AssetPostprocessor
  23. {
  24. static bool m_Upgrade = true;
  25. static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
  26. {
  27. if (m_Upgrade)
  28. {
  29. m_Upgrade = false;
  30. BeginUpgradeSettings();
  31. }
  32. }
  33. static void BeginUpgradeSettings()
  34. {
  35. string searchText = "t:XRGeneralSettings";
  36. string[] assets = AssetDatabase.FindAssets(searchText);
  37. if (assets.Length > 0)
  38. {
  39. string path = AssetDatabase.GUIDToAssetPath(assets[0]);
  40. XRGeneralSettingsUpgrade.UpgradeSettingsToPerBuildTarget(path);
  41. }
  42. }
  43. }
  44. static XRGeneralSettingsPerBuildTarget()
  45. {
  46. EditorApplication.playModeStateChanged += PlayModeStateChanged;
  47. }
  48. void OnEnable()
  49. {
  50. foreach (var setting in Settings.Values)
  51. {
  52. var assignedSettings = setting.AssignedSettings;
  53. if (assignedSettings == null)
  54. continue;
  55. var filteredLoaders = from ldr in assignedSettings.loaders where ldr != null select ldr;
  56. assignedSettings.loaders = filteredLoaders.ToList<XRLoader>();
  57. }
  58. XRGeneralSettings.Instance = XRGeneralSettingsForBuildTarget(BuildTargetGroup.Standalone);
  59. }
  60. static void PlayModeStateChanged(PlayModeStateChange state)
  61. {
  62. XRGeneralSettingsPerBuildTarget buildTargetSettings = null;
  63. EditorBuildSettings.TryGetConfigObject(XRGeneralSettings.k_SettingsKey, out buildTargetSettings);
  64. if (buildTargetSettings == null)
  65. return;
  66. XRGeneralSettings instance = buildTargetSettings.SettingsForBuildTarget(BuildTargetGroup.Standalone);
  67. if (instance == null || !instance.InitManagerOnStart)
  68. return;
  69. instance.InternalPlayModeStateChanged(state);
  70. }
  71. internal static bool ContainsLoaderForAnyBuildTarget(string loaderTypeName)
  72. {
  73. XRGeneralSettingsPerBuildTarget buildTargetSettings = null;
  74. EditorBuildSettings.TryGetConfigObject(XRGeneralSettings.k_SettingsKey, out buildTargetSettings);
  75. if (buildTargetSettings == null)
  76. return false;
  77. foreach (var settings in buildTargetSettings.Settings.Values)
  78. {
  79. if (XRPackageMetadataStore.IsLoaderAssigned(settings.Manager, loaderTypeName))
  80. return true;
  81. }
  82. return false;
  83. }
  84. #endif
  85. /// <summary>Set specific settings for a given build target.</summary>
  86. ///
  87. /// <param name="targetGroup">An enum specifying which platform group this build is for.</param>
  88. /// <param name="settings">An instance of <see cref="XRGeneralSettings"/> to assign for the given key.</param>
  89. public void SetSettingsForBuildTarget(BuildTargetGroup targetGroup, XRGeneralSettings settings)
  90. {
  91. // Ensures the editor's "runtime instance" is the most current for standalone settings
  92. if (targetGroup == BuildTargetGroup.Standalone)
  93. XRGeneralSettings.Instance = settings;
  94. Settings[targetGroup] = settings;
  95. }
  96. /// <summary>Get specific settings for a given build target.</summary>
  97. /// <param name="targetGroup">An enum specifying which platform group this build is for.</param>
  98. /// <returns>The instance of <see cref="XRGeneralSettings"/> assigned to the key, or null if not.</returns>
  99. public XRGeneralSettings SettingsForBuildTarget(BuildTargetGroup targetGroup)
  100. {
  101. XRGeneralSettings ret = null;
  102. Settings.TryGetValue(targetGroup, out ret);
  103. return ret;
  104. }
  105. /// <summary>Serialization override.</summary>
  106. public void OnBeforeSerialize()
  107. {
  108. Keys.Clear();
  109. Values.Clear();
  110. foreach (var kv in Settings)
  111. {
  112. Keys.Add(kv.Key);
  113. Values.Add(kv.Value);
  114. }
  115. }
  116. /// <summary>Serialization override.</summary>
  117. public void OnAfterDeserialize()
  118. {
  119. Settings = new Dictionary<BuildTargetGroup, XRGeneralSettings>();
  120. for (int i = 0; i < Math.Min(Keys.Count, Values.Count); i++)
  121. {
  122. Settings.Add(Keys[i], Values[i]);
  123. }
  124. }
  125. /// <summary>Given a build target, get the general settings container assigned to it.</summary>
  126. /// <param name="targetGroup">An enum specifying which platform group this build is for.</param>
  127. /// <returns>The instance of <see cref="XRGeneralSettings"/> assigned to the key, or null if not.</returns>
  128. public static XRGeneralSettings XRGeneralSettingsForBuildTarget(BuildTargetGroup targetGroup)
  129. {
  130. XRGeneralSettingsPerBuildTarget buildTargetSettings = null;
  131. EditorBuildSettings.TryGetConfigObject(XRGeneralSettings.k_SettingsKey, out buildTargetSettings);
  132. if (buildTargetSettings == null)
  133. return null;
  134. return buildTargetSettings.SettingsForBuildTarget(targetGroup);
  135. }
  136. }
  137. }