ProjectExporterSettingsProvider.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System.Collections.Generic;
  2. using AssetBank.Editor.DockableWindow;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace AssetBank.Settings
  6. {
  7. internal class ProjectExporterSettingsProvider : SettingsProvider
  8. {
  9. private SerializedObject m_Settings;
  10. private SerializedProperty m_FoldersToIgnore;
  11. private SerializedProperty m_FileExtensionsToIgnore;
  12. private SerializedProperty m_UnityTypesToIgnore;
  13. private SerializedProperty m_ConvertToNewSchema;
  14. private SerializedProperty m_OptimiseExport;
  15. private SerializedProperty m_CategoriesToOptimise;
  16. private SerializedProperty m_SafeComponents;
  17. private SerializedProperty m_HardcodedComponents;
  18. private SerializedProperty m_OverrideHardcodedDefaults;
  19. const string k_SettingsPath = "Project/Project AssetDatabase";
  20. public ProjectExporterSettingsProvider(string path, SettingsScope scope = SettingsScope.Project)
  21. : base(path, scope) {}
  22. public override void OnActivate(string searchContext, UnityEngine.UIElements.VisualElement rootElement)
  23. {
  24. m_Settings = ProjectExporterSettings.GetSerializedSettings();
  25. m_FoldersToIgnore = m_Settings.FindProperty("m_FoldersToIgnore");
  26. m_FileExtensionsToIgnore = m_Settings.FindProperty("m_FileExtensionsToIgnore");
  27. m_UnityTypesToIgnore = m_Settings.FindProperty("m_UnityTypesToIgnore");
  28. m_ConvertToNewSchema = m_Settings.FindProperty("m_ConvertToNewSchema");
  29. m_OptimiseExport = m_Settings.FindProperty("m_OptimiseExport");
  30. m_CategoriesToOptimise = m_Settings.FindProperty("m_CategoriesToOptimise");
  31. m_SafeComponents = m_Settings.FindProperty("m_SafeComponents");
  32. m_HardcodedComponents = m_Settings.FindProperty("m_HardcodedComponents");
  33. m_OverrideHardcodedDefaults = m_Settings.FindProperty("m_OverrideHardcodedDefaults");
  34. }
  35. public override void OnGUI(string searchContext)
  36. {
  37. if (m_Settings == null || m_Settings.targetObject == null)
  38. {
  39. EditorGUILayout.HelpBox("Settings asset not found. It will be created automatically.", MessageType.Info);
  40. m_Settings = ProjectExporterSettings.GetSerializedSettings();
  41. return;
  42. }
  43. m_Settings.Update();
  44. EditorGUILayout.PropertyField(m_FoldersToIgnore, new GUIContent("Folders to Ignore"), true);
  45. EditorGUILayout.PropertyField(m_FileExtensionsToIgnore, new GUIContent("File Extensions to Ignore"), true);
  46. EditorGUILayout.PropertyField(m_UnityTypesToIgnore, new GUIContent("Unity Types to Ignore"), true);
  47. EditorGUILayout.Space();
  48. EditorGUILayout.LabelField("JSON Processing", EditorStyles.boldLabel);
  49. EditorGUILayout.PropertyField(m_ConvertToNewSchema, new GUIContent("Convert to Hierarchical Schema", "Converts the final JSON to the new, structured format."));
  50. EditorGUILayout.PropertyField(m_OptimiseExport, new GUIContent("Optimise Export (Trim Data)", "Runs the initial data trimming process based on the rules below."));
  51. if(m_OptimiseExport.boolValue)
  52. {
  53. DrawCategoriesToOptimise();
  54. EditorGUILayout.Space();
  55. EditorGUILayout.LabelField("Hardcoded Rules", EditorStyles.boldLabel);
  56. EditorGUILayout.PropertyField(m_OverrideHardcodedDefaults, new GUIContent("Override Defaults", "Check this to ignore the hardcoded safety rules and use only the 'Safe Components' list below. Warning: This can break scene hierarchy if not configured correctly."));
  57. EditorGUI.BeginDisabledGroup(true);
  58. EditorGUILayout.PropertyField(m_HardcodedComponents, new GUIContent("Hardcoded Safe Components (Read-Only)"), true);
  59. EditorGUI.EndDisabledGroup();
  60. EditorGUILayout.Space();
  61. EditorGUILayout.LabelField("User-Defined Rules", EditorStyles.boldLabel);
  62. EditorGUILayout.PropertyField(m_SafeComponents, new GUIContent("Safe Components"), true);
  63. }
  64. if (m_Settings.hasModifiedProperties)
  65. {
  66. m_Settings.ApplyModifiedProperties();
  67. }
  68. }
  69. private void DrawCategoriesToOptimise()
  70. {
  71. EditorGUILayout.LabelField("Categories to Optimise", EditorStyles.boldLabel);
  72. EditorGUI.indentLevel++;
  73. var categories = new List<string>();
  74. for (int i = 0; i < m_CategoriesToOptimise.arraySize; i++)
  75. {
  76. categories.Add(m_CategoriesToOptimise.GetArrayElementAtIndex(i).stringValue);
  77. }
  78. foreach (var availableCategory in ProjectExporterCategories.All)
  79. {
  80. var isEnabled = categories.Contains(availableCategory);
  81. var newIsEnabled = EditorGUILayout.Toggle(availableCategory, isEnabled);
  82. if (newIsEnabled && !isEnabled)
  83. {
  84. // Add to list
  85. var index = m_CategoriesToOptimise.arraySize;
  86. m_CategoriesToOptimise.InsertArrayElementAtIndex(index);
  87. m_CategoriesToOptimise.GetArrayElementAtIndex(index).stringValue = availableCategory;
  88. }
  89. else if (!newIsEnabled && isEnabled)
  90. {
  91. // Remove from list
  92. for (int i = 0; i < m_CategoriesToOptimise.arraySize; i++)
  93. {
  94. if (m_CategoriesToOptimise.GetArrayElementAtIndex(i).stringValue == availableCategory)
  95. {
  96. m_CategoriesToOptimise.DeleteArrayElementAtIndex(i);
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. EditorGUI.indentLevel--;
  103. }
  104. [SettingsProvider]
  105. public static SettingsProvider CreateSettingsProvider()
  106. {
  107. var provider = new ProjectExporterSettingsProvider(k_SettingsPath, SettingsScope.Project)
  108. {
  109. keywords = GetSearchKeywordsFromGUIContentProperties<Styles>()
  110. };
  111. return provider;
  112. }
  113. private class Styles
  114. {
  115. public static GUIContent foldersToIgnore = new GUIContent("Folders to Ignore");
  116. public static GUIContent fileExtensionsToIgnore = new GUIContent("File Extensions to Ignore");
  117. public static GUIContent unityTypesToIgnore = new GUIContent("Unity Types to Ignore");
  118. public static GUIContent optimiseExport = new GUIContent("Optimise Export");
  119. public static GUIContent safeComponents = new GUIContent("Safe Components");
  120. }
  121. }
  122. }