ProjectExporterWindow.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System.Collections.Generic;
  2. using AssetBank.DockableWindow.UIHelper;
  3. using AssetBank.Editor.DockableWindow;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.UIElements;
  7. namespace AssetBank.DockableWindow
  8. {
  9. public class ProjectExporterWindow : EditorWindow
  10. {
  11. private ProjectExporterController _controller;
  12. private VisualElement _rootElement;
  13. private VisualElement _contentContainer;
  14. private Dictionary<string, AssetModel> _assetTrees;
  15. private string _currentTab = "General"; // Default to General tab
  16. // Checkbox states for the General tab
  17. private bool _exportScenes = true;
  18. private bool _exportPrefabs = true;
  19. private bool _exportScriptableObjects = true;
  20. private bool _exportMetaFiles = true;
  21. private bool _exportSettings = true;
  22. [MenuItem("Tools/Project Exporter")]
  23. public static void ShowWindow()
  24. {
  25. var wnd = GetWindow<ProjectExporterWindow>();
  26. wnd.titleContent = new GUIContent("Project Exporter", EditorGUIUtility.IconContent("d_Profiler.GlobalIllumination").image);
  27. wnd.minSize = new Vector2(400, 300);
  28. }
  29. private void OnEnable()
  30. {
  31. _controller = new ProjectExporterController();
  32. _assetTrees = new Dictionary<string, AssetModel>();
  33. _rootElement = rootVisualElement;
  34. BuildUI();
  35. RefreshContent(_currentTab);
  36. }
  37. private void BuildUI()
  38. {
  39. _rootElement.Clear();
  40. var tabContainer = new VisualElement { style = { flexDirection = FlexDirection.Row } };
  41. tabContainer.Add(ProjectExporterUI.CreateTab("General")); // New General tab
  42. tabContainer.Add(ProjectExporterUI.CreateTab("Scenes"));
  43. tabContainer.Add(ProjectExporterUI.CreateTab("Prefabs"));
  44. tabContainer.Add(ProjectExporterUI.CreateTab("Scriptable Objects"));
  45. tabContainer.Add(ProjectExporterUI.CreateTab("Meta Files"));
  46. tabContainer.Add(ProjectExporterUI.CreateTab("Settings"));
  47. _rootElement.Add(tabContainer);
  48. var scrollView = new ScrollView(ScrollViewMode.Vertical);
  49. _rootElement.Add(scrollView);
  50. _contentContainer = ProjectExporterUI.CreateMainContainer();
  51. scrollView.Add(_contentContainer);
  52. var generateButton = ProjectExporterUI.CreateGenerateButton(_currentTab == "General");
  53. _rootElement.Add(generateButton);
  54. // The button's action will depend on the current tab
  55. generateButton.clicked += () => {
  56. if (_currentTab == "General")
  57. {
  58. PerformBulkExport();
  59. }
  60. else if (_assetTrees.ContainsKey(_currentTab))
  61. {
  62. _controller.ExportAssets(_assetTrees[_currentTab]);
  63. }
  64. };
  65. var tabs = tabContainer.Query<Button>().ToList();
  66. tabs[0].clicked += () => RefreshContent("General");
  67. tabs[1].clicked += () => RefreshContent("Scenes");
  68. tabs[2].clicked += () => RefreshContent("Prefabs");
  69. tabs[3].clicked += () => RefreshContent("Scriptable Objects");
  70. tabs[4].clicked += () => RefreshContent("Meta Files");
  71. tabs[5].clicked += () => RefreshContent("Settings");
  72. }
  73. private void RefreshContent(string tabName)
  74. {
  75. _currentTab = tabName;
  76. _contentContainer.Clear();
  77. if (tabName == "General")
  78. {
  79. DrawGeneralTab();
  80. return;
  81. }
  82. if (!_assetTrees.ContainsKey(tabName))
  83. {
  84. ProjectExporterDataHelper.LoadAssetTree(_assetTrees, _controller, tabName);
  85. }
  86. var rootModel = _assetTrees[tabName];
  87. if (rootModel != null)
  88. {
  89. foreach (var child in rootModel.Children)
  90. {
  91. _contentContainer.Add(CreateAssetView(child, 0));
  92. }
  93. }
  94. }
  95. private void DrawGeneralTab()
  96. {
  97. var title = new Label("Select Export Types") { style = { unityFontStyleAndWeight = FontStyle.Bold } };
  98. _contentContainer.Add(title);
  99. var scenesToggle = ProjectExporterUI.CreateToggle("Export Scenes", _exportScenes, evt => _exportScenes = evt.newValue);
  100. var prefabsToggle = ProjectExporterUI.CreateToggle("Export Prefabs", _exportPrefabs, evt => _exportPrefabs = evt.newValue);
  101. var soToggle = ProjectExporterUI.CreateToggle("Export Scriptable Objects", _exportScriptableObjects, evt => _exportScriptableObjects = evt.newValue);
  102. var metaToggle = ProjectExporterUI.CreateToggle("Export Meta Files", _exportMetaFiles, evt => _exportMetaFiles = evt.newValue);
  103. var settingsToggle = ProjectExporterUI.CreateToggle("Export Settings", _exportSettings, evt => _exportSettings = evt.newValue);
  104. _contentContainer.Add(scenesToggle);
  105. _contentContainer.Add(prefabsToggle);
  106. _contentContainer.Add(soToggle);
  107. _contentContainer.Add(metaToggle);
  108. _contentContainer.Add(settingsToggle);
  109. }
  110. private void PerformBulkExport()
  111. {
  112. if (_exportScenes) ExportAllOfType("Scenes");
  113. if (_exportPrefabs) ExportAllOfType("Prefabs");
  114. if (_exportScriptableObjects) ExportAllOfType("Scriptable Objects");
  115. if (_exportMetaFiles) ExportAllOfType("Meta Files");
  116. if (_exportSettings) ExportAllOfType("Settings");
  117. EditorUtility.DisplayDialog("Bulk Export", "Bulk export process finished.", "OK");
  118. }
  119. private void ExportAllOfType(string type)
  120. {
  121. if (!_assetTrees.ContainsKey(type))
  122. {
  123. ProjectExporterDataHelper.LoadAssetTree(_assetTrees, _controller, type);
  124. }
  125. var rootModel = _assetTrees[type];
  126. if (rootModel != null)
  127. {
  128. ProjectExporterDataHelper.SetChildrenSelection(rootModel, true); // Select all assets
  129. _controller.ExportAssets(rootModel);
  130. }
  131. }
  132. private VisualElement CreateAssetView(AssetModel model, int indentLevel)
  133. {
  134. const int indentWidth = 20;
  135. if (model.Children.Count > 0)
  136. {
  137. var row = new VisualElement { style = { flexDirection = FlexDirection.Row, alignItems = Align.Center, marginLeft = indentLevel * indentWidth } };
  138. var foldout = ProjectExporterUI.CreateFoldout(model.Name);
  139. foldout.style.flexGrow = 1;
  140. var toggle = new Toggle { value = model.IsSelected, text = "" };
  141. toggle.RegisterValueChangedCallback(evt => {
  142. model.IsSelected = evt.newValue;
  143. ProjectExporterDataHelper.SetChildrenSelection(model, evt.newValue);
  144. UpdateChildTogglesUI(foldout, evt.newValue);
  145. });
  146. row.Add(toggle);
  147. row.Add(foldout);
  148. foreach (var child in model.Children)
  149. {
  150. foldout.Add(CreateAssetView(child, indentLevel + 1));
  151. }
  152. return row;
  153. }
  154. else
  155. {
  156. var toggle = ProjectExporterUI.CreateToggle(model.Name);
  157. toggle.style.marginLeft = (indentLevel + 1) * indentWidth;
  158. toggle.value = model.IsSelected;
  159. toggle.RegisterValueChangedCallback(evt => model.IsSelected = evt.newValue);
  160. return toggle;
  161. }
  162. }
  163. private void UpdateChildTogglesUI(VisualElement container, bool isSelected)
  164. {
  165. container.Query<Toggle>().ForEach(childToggle => {
  166. childToggle.SetValueWithoutNotify(isSelected);
  167. });
  168. }
  169. }
  170. }