XRConfigurationProvider.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. using UnityEditor.XR.Management.Metadata;
  6. namespace UnityEditor.XR.Management
  7. {
  8. internal class XRConfigurationProvider : SettingsProvider
  9. {
  10. static readonly GUIContent s_WarningToCreateSettings = EditorGUIUtility.TrTextContent("You must create a serialized instance of the settings data in order to modify the settings in this UI. Until then only default settings set by the provider will be available.");
  11. Type m_BuildDataType = null;
  12. string m_BuildSettingsKey;
  13. Editor m_CachedEditor;
  14. SerializedObject m_SettingsWrapper;
  15. public XRConfigurationProvider(string path, string buildSettingsKey, Type buildDataType, SettingsScope scopes = SettingsScope.Project) : base(path, scopes)
  16. {
  17. m_BuildDataType = buildDataType;
  18. m_BuildSettingsKey = buildSettingsKey;
  19. if (currentSettings == null)
  20. {
  21. Create();
  22. }
  23. }
  24. ScriptableObject currentSettings
  25. {
  26. get
  27. {
  28. ScriptableObject settings = null;
  29. EditorBuildSettings.TryGetConfigObject(m_BuildSettingsKey, out settings);
  30. if (settings == null)
  31. {
  32. string searchText = String.Format("t:{0}", m_BuildDataType.Name);
  33. string[] assets = AssetDatabase.FindAssets(searchText);
  34. if (assets.Length > 0)
  35. {
  36. string path = AssetDatabase.GUIDToAssetPath(assets[0]);
  37. settings = AssetDatabase.LoadAssetAtPath(path, m_BuildDataType) as ScriptableObject;
  38. EditorBuildSettings.AddConfigObject(m_BuildSettingsKey, settings, true);
  39. }
  40. }
  41. return settings;
  42. }
  43. }
  44. void InitEditorData(ScriptableObject settings)
  45. {
  46. if (settings != null)
  47. {
  48. m_SettingsWrapper = new SerializedObject(settings);
  49. Editor.CreateCachedEditor(settings, null, ref m_CachedEditor);
  50. }
  51. }
  52. public override void OnActivate(string searchContext, VisualElement rootElement)
  53. {
  54. InitEditorData(currentSettings);
  55. }
  56. public override void OnDeactivate()
  57. {
  58. m_CachedEditor = null;
  59. m_SettingsWrapper = null;
  60. }
  61. public override void OnGUI(string searchContext)
  62. {
  63. if (m_SettingsWrapper == null || m_SettingsWrapper.targetObject == null)
  64. {
  65. ScriptableObject settings = (currentSettings != null) ? currentSettings : Create();
  66. InitEditorData(settings);
  67. }
  68. if (m_SettingsWrapper != null && m_SettingsWrapper.targetObject != null && m_CachedEditor != null)
  69. {
  70. m_SettingsWrapper.Update();
  71. m_CachedEditor.OnInspectorGUI();
  72. m_SettingsWrapper.ApplyModifiedProperties();
  73. }
  74. }
  75. ScriptableObject Create()
  76. {
  77. ScriptableObject settings = ScriptableObject.CreateInstance(m_BuildDataType) as ScriptableObject;
  78. if (settings != null)
  79. {
  80. var package = XRPackageMetadataStore.GetPackageForSettingsTypeNamed(m_BuildDataType.FullName);
  81. package?.PopulateNewSettingsInstance(settings);
  82. string newAssetName = String.Format("{0}.asset", EditorUtilities.TypeNameToString(m_BuildDataType));
  83. string assetPath = EditorUtilities.GetAssetPathForComponents(EditorUtilities.s_DefaultSettingsPath);
  84. if (!string.IsNullOrEmpty(assetPath))
  85. {
  86. assetPath = Path.Combine(assetPath, newAssetName);
  87. AssetDatabase.CreateAsset(settings, assetPath);
  88. EditorBuildSettings.AddConfigObject(m_BuildSettingsKey, settings, true);
  89. return settings;
  90. }
  91. }
  92. return null;
  93. }
  94. }
  95. }