XRSettingsManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor.XR.Management.Metadata;
  5. using UnityEngine;
  6. using UnityEngine.UIElements;
  7. using UnityEngine.XR.Management;
  8. namespace UnityEditor.XR.Management
  9. {
  10. class XRSettingsManager : SettingsProvider
  11. {
  12. static class Styles
  13. {
  14. public static readonly GUIStyle k_UrlLabelPersonal = new GUIStyle(EditorStyles.label)
  15. {
  16. name = "url-label",
  17. richText = true,
  18. normal = new GUIStyleState { textColor = new Color(8 / 255f, 8 / 255f, 252 / 255f) },
  19. };
  20. public static readonly GUIStyle k_UrlLabelProfessional = new GUIStyle(EditorStyles.label)
  21. {
  22. name = "url-label",
  23. richText = true,
  24. normal = new GUIStyleState { textColor = new Color(79 / 255f, 128 / 255f, 248 / 255f) },
  25. };
  26. public static readonly GUIStyle k_LabelWordWrap = new GUIStyle(EditorStyles.label) { wordWrap = true };
  27. }
  28. struct Content
  29. {
  30. public static readonly GUIContent k_InitializeOnStart = new GUIContent("Initialize XR on Startup");
  31. public static readonly GUIContent k_XRConfigurationText = new GUIContent("Information about configuration, tracking and migration can be found below.");
  32. public static readonly GUIContent k_XRConfigurationDocUriText = new GUIContent("View Documentation");
  33. public static readonly Uri k_XRConfigurationUri = new Uri(" https://docs.unity3d.com/Manual/configuring-project-for-xr.html");
  34. }
  35. internal static GUIStyle GetStyle(string styleName)
  36. {
  37. GUIStyle s = GUI.skin.FindStyle(styleName) ?? EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).FindStyle(styleName);
  38. if (s == null)
  39. {
  40. Debug.LogError("Missing built-in guistyle " + styleName);
  41. s = GUI.skin.box;
  42. }
  43. return s;
  44. }
  45. static string s_SettingsRootTitle = $"Project/{XRConstants.k_XRPluginManagement}";
  46. static XRSettingsManager s_SettingsManager = null;
  47. internal static XRSettingsManager Instance => s_SettingsManager;
  48. private bool resetUi = false;
  49. internal bool ResetUi
  50. {
  51. get
  52. {
  53. return resetUi;
  54. }
  55. set
  56. {
  57. resetUi = value;
  58. if (resetUi)
  59. Repaint();
  60. }
  61. }
  62. SerializedObject m_SettingsWrapper;
  63. private Dictionary<BuildTargetGroup, XRManagerSettingsEditor> CachedSettingsEditor = new Dictionary<BuildTargetGroup, XRManagerSettingsEditor>();
  64. private BuildTargetGroup m_LastBuildTargetGroup = BuildTargetGroup.Unknown;
  65. static XRGeneralSettingsPerBuildTarget currentSettings
  66. {
  67. get
  68. {
  69. XRGeneralSettingsPerBuildTarget generalSettings = null;
  70. EditorBuildSettings.TryGetConfigObject(XRGeneralSettings.k_SettingsKey, out generalSettings);
  71. if (generalSettings == null)
  72. {
  73. lock(s_SettingsManager)
  74. {
  75. EditorBuildSettings.TryGetConfigObject(XRGeneralSettings.k_SettingsKey, out generalSettings);
  76. if (generalSettings == null)
  77. {
  78. string searchText = "t:XRGeneralSettings";
  79. string[] assets = AssetDatabase.FindAssets(searchText);
  80. if (assets.Length > 0)
  81. {
  82. string path = AssetDatabase.GUIDToAssetPath(assets[0]);
  83. generalSettings = AssetDatabase.LoadAssetAtPath(path, typeof(XRGeneralSettingsPerBuildTarget)) as XRGeneralSettingsPerBuildTarget;
  84. }
  85. }
  86. if (generalSettings == null)
  87. {
  88. generalSettings = ScriptableObject.CreateInstance(typeof(XRGeneralSettingsPerBuildTarget)) as XRGeneralSettingsPerBuildTarget;
  89. string assetPath = EditorUtilities.GetAssetPathForComponents(EditorUtilities.s_DefaultGeneralSettingsPath);
  90. if (!string.IsNullOrEmpty(assetPath))
  91. {
  92. assetPath = Path.Combine(assetPath, "XRGeneralSettings.asset");
  93. AssetDatabase.CreateAsset(generalSettings, assetPath);
  94. }
  95. }
  96. EditorBuildSettings.AddConfigObject(XRGeneralSettings.k_SettingsKey, generalSettings, true);
  97. }
  98. }
  99. return generalSettings;
  100. }
  101. }
  102. [UnityEngine.Internal.ExcludeFromDocs]
  103. XRSettingsManager(string path, SettingsScope scopes = SettingsScope.Project) : base(path, scopes)
  104. {
  105. }
  106. [SettingsProvider]
  107. [UnityEngine.Internal.ExcludeFromDocs]
  108. static SettingsProvider Create()
  109. {
  110. if (s_SettingsManager == null)
  111. {
  112. s_SettingsManager = new XRSettingsManager(s_SettingsRootTitle);
  113. }
  114. return s_SettingsManager;
  115. }
  116. [SettingsProviderGroup]
  117. [UnityEngine.Internal.ExcludeFromDocs]
  118. static SettingsProvider[] CreateAllChildSettingsProviders()
  119. {
  120. List<SettingsProvider> ret = new List<SettingsProvider>();
  121. if (s_SettingsManager != null)
  122. {
  123. var ats = TypeLoaderExtensions.GetAllTypesWithAttribute<XRConfigurationDataAttribute>();
  124. foreach (var at in ats)
  125. {
  126. if (at.FullName.Contains("Unity.XR.Management.TestPackage"))
  127. continue;
  128. XRConfigurationDataAttribute xrbda = at.GetCustomAttributes(typeof(XRConfigurationDataAttribute), true)[0] as XRConfigurationDataAttribute;
  129. string settingsPath = String.Format("{1}/{0}", xrbda.displayName, s_SettingsRootTitle);
  130. var resProv = new XRConfigurationProvider(settingsPath, xrbda.buildSettingsKey, at);
  131. ret.Add(resProv);
  132. }
  133. }
  134. return ret.ToArray();
  135. }
  136. void InitEditorData(ScriptableObject settings)
  137. {
  138. if (settings != null)
  139. {
  140. m_SettingsWrapper = new SerializedObject(settings);
  141. }
  142. }
  143. /// <summary>See <see href="https://docs.unity3d.com/ScriptReference/SettingsProvider.html">SettingsProvider documentation</see>.</summary>
  144. public override void OnActivate(string searchContext, VisualElement rootElement)
  145. {
  146. InitEditorData(currentSettings);
  147. }
  148. /// <summary>See <see href="https://docs.unity3d.com/ScriptReference/SettingsProvider.html">SettingsProvider documentation</see>.</summary>
  149. public override void OnDeactivate()
  150. {
  151. m_SettingsWrapper = null;
  152. CachedSettingsEditor.Clear();
  153. }
  154. private void DisplayLoaderSelectionUI()
  155. {
  156. BuildTargetGroup buildTargetGroup = EditorGUILayout.BeginBuildTargetSelectionGrouping();
  157. try
  158. {
  159. bool buildTargetChanged = m_LastBuildTargetGroup != buildTargetGroup;
  160. if (buildTargetChanged)
  161. m_LastBuildTargetGroup = buildTargetGroup;
  162. XRGeneralSettings settings = currentSettings.SettingsForBuildTarget(buildTargetGroup);
  163. if (settings == null)
  164. {
  165. settings = ScriptableObject.CreateInstance<XRGeneralSettings>() as XRGeneralSettings;
  166. currentSettings.SetSettingsForBuildTarget(buildTargetGroup, settings);
  167. settings.name = $"{buildTargetGroup.ToString()} Settings";
  168. AssetDatabase.AddObjectToAsset(settings, AssetDatabase.GetAssetOrScenePath(currentSettings));
  169. }
  170. var serializedSettingsObject = new SerializedObject(settings);
  171. serializedSettingsObject.Update();
  172. SerializedProperty initOnStart = serializedSettingsObject.FindProperty("m_InitManagerOnStart");
  173. EditorGUILayout.PropertyField(initOnStart, Content.k_InitializeOnStart);
  174. EditorGUILayout.Space();
  175. SerializedProperty loaderProp = serializedSettingsObject.FindProperty("m_LoaderManagerInstance");
  176. if (!CachedSettingsEditor.ContainsKey(buildTargetGroup))
  177. {
  178. CachedSettingsEditor.Add(buildTargetGroup, null);
  179. }
  180. if (loaderProp.objectReferenceValue == null)
  181. {
  182. var xrManagerSettings = ScriptableObject.CreateInstance<XRManagerSettings>() as XRManagerSettings;
  183. xrManagerSettings.name = $"{buildTargetGroup.ToString()} Providers";
  184. AssetDatabase.AddObjectToAsset(xrManagerSettings, AssetDatabase.GetAssetOrScenePath(currentSettings));
  185. loaderProp.objectReferenceValue = xrManagerSettings;
  186. serializedSettingsObject.ApplyModifiedProperties();
  187. }
  188. var obj = loaderProp.objectReferenceValue;
  189. if (obj != null)
  190. {
  191. loaderProp.objectReferenceValue = obj;
  192. if (CachedSettingsEditor[buildTargetGroup] == null)
  193. {
  194. CachedSettingsEditor[buildTargetGroup] = Editor.CreateEditor(obj) as XRManagerSettingsEditor;
  195. if (CachedSettingsEditor[buildTargetGroup] == null)
  196. {
  197. Debug.LogError("Failed to create a view for XR Manager Settings Instance");
  198. }
  199. }
  200. if (CachedSettingsEditor[buildTargetGroup] != null)
  201. {
  202. if (ResetUi)
  203. {
  204. ResetUi = false;
  205. CachedSettingsEditor[buildTargetGroup].Reload();
  206. }
  207. CachedSettingsEditor[buildTargetGroup].BuildTarget = buildTargetGroup;
  208. CachedSettingsEditor[buildTargetGroup].OnInspectorGUI();
  209. }
  210. }
  211. else if (obj == null)
  212. {
  213. settings.AssignedSettings = null;
  214. loaderProp.objectReferenceValue = null;
  215. }
  216. serializedSettingsObject.ApplyModifiedProperties();
  217. }
  218. catch (Exception ex)
  219. {
  220. Debug.LogError($"Error trying to display plug-in assingment UI : {ex.Message}");
  221. }
  222. EditorGUILayout.EndBuildTargetSelectionGrouping();
  223. }
  224. private void DisplayLink(GUIContent text, Uri link, int leftMargin)
  225. {
  226. var labelStyle = EditorGUIUtility.isProSkin ? Styles.k_UrlLabelProfessional : Styles.k_UrlLabelPersonal;
  227. var size = labelStyle.CalcSize(Content.k_XRConfigurationDocUriText);
  228. var uriRect = GUILayoutUtility.GetRect(text, labelStyle);
  229. uriRect.x += leftMargin;
  230. uriRect.width = size.x;
  231. if (GUI.Button(uriRect, text, labelStyle))
  232. {
  233. System.Diagnostics.Process.Start(link.AbsoluteUri);
  234. }
  235. EditorGUIUtility.AddCursorRect(uriRect, MouseCursor.Link);
  236. EditorGUI.DrawRect(new Rect(uriRect.x, uriRect.y + uriRect.height - 1, uriRect.width, 1), labelStyle.normal.textColor);
  237. }
  238. private void DisplayXRTrackingDocumentationLink()
  239. {
  240. GUILayout.BeginVertical(EditorStyles.helpBox);
  241. {
  242. EditorGUILayout.LabelField(Content.k_XRConfigurationText, Styles.k_LabelWordWrap);
  243. DisplayLink(Content.k_XRConfigurationDocUriText, Content.k_XRConfigurationUri, 2);
  244. }
  245. GUILayout.EndVertical();
  246. EditorGUILayout.Space();
  247. }
  248. private void DisplayLoadOrderUi()
  249. {
  250. EditorGUILayout.Space();
  251. EditorGUI.BeginDisabledGroup(XRPackageMetadataStore.isDoingQueueProcessing || EditorApplication.isPlaying || EditorApplication.isPaused);
  252. if (m_SettingsWrapper != null && m_SettingsWrapper.targetObject != null)
  253. {
  254. m_SettingsWrapper.Update();
  255. EditorGUILayout.Space();
  256. DisplayLoaderSelectionUI();
  257. m_SettingsWrapper.ApplyModifiedProperties();
  258. }
  259. EditorGUI.EndDisabledGroup();
  260. EditorGUILayout.Space();
  261. }
  262. /// <summary>See <see href="https://docs.unity3d.com/ScriptReference/SettingsProvider.html">SettingsProvider documentation</see>.</summary>
  263. public override void OnGUI(string searchContext)
  264. {
  265. EditorGUILayout.Space();
  266. DisplayLoadOrderUi();
  267. DisplayXRTrackingDocumentationLink();
  268. base.OnGUI(searchContext);
  269. }
  270. }
  271. }