ProjectExporterWindow.cs 6.9 KB

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