123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using System.Collections.Generic;
- using AssetBank.DockableWindow.UIHelper;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.UIElements;
- namespace AssetBank.DockableWindow
- {
- public class ProjectExporterWindow : EditorWindow
- {
- private ProjectExporterController _controller;
- private VisualElement _rootElement;
- private VisualElement _contentContainer;
- private Dictionary<string, AssetModel> _assetTrees;
- private string _currentTab = "General"; // Default to General tab
- // Checkbox states for the General tab
- private bool _exportScenes = true;
- private bool _exportPrefabs = true;
- private bool _exportScriptableObjects = true;
- private bool _exportMetaFiles = true;
- private bool _exportSettings = true;
- [MenuItem("Tools/Project Exporter")]
- public static void ShowWindow()
- {
- var wnd = GetWindow<ProjectExporterWindow>();
- wnd.titleContent = new GUIContent("Project Exporter", EditorGUIUtility.IconContent("d_Profiler.GlobalIllumination").image);
- wnd.minSize = new Vector2(400, 300);
- }
- private void OnEnable()
- {
- _controller = new ProjectExporterController();
- _assetTrees = new Dictionary<string, AssetModel>();
- _rootElement = rootVisualElement;
-
- BuildUI();
- RefreshContent(_currentTab);
- }
- private void BuildUI()
- {
- _rootElement.Clear();
- var tabContainer = new VisualElement { style = { flexDirection = FlexDirection.Row } };
- tabContainer.Add(ProjectExporterUI.CreateTab("General")); // New General tab
- tabContainer.Add(ProjectExporterUI.CreateTab("Scenes"));
- tabContainer.Add(ProjectExporterUI.CreateTab("Prefabs"));
- tabContainer.Add(ProjectExporterUI.CreateTab("Scriptable Objects"));
- tabContainer.Add(ProjectExporterUI.CreateTab("Meta Files"));
- tabContainer.Add(ProjectExporterUI.CreateTab("Settings"));
- _rootElement.Add(tabContainer);
- var scrollView = new ScrollView(ScrollViewMode.Vertical);
- _rootElement.Add(scrollView);
- _contentContainer = ProjectExporterUI.CreateMainContainer();
- scrollView.Add(_contentContainer);
- var generateButton = ProjectExporterUI.CreateGenerateButton(_currentTab == "General");
- _rootElement.Add(generateButton);
-
- // The button's action will depend on the current tab
- generateButton.clicked += () => {
- if (_currentTab == "General")
- {
- PerformBulkExport();
- }
- else if (_assetTrees.ContainsKey(_currentTab))
- {
- _controller.ExportAssets(_assetTrees[_currentTab], _currentTab);
- }
- };
- var tabs = tabContainer.Query<Button>().ToList();
- tabs[0].clicked += () => RefreshContent("General");
- tabs[1].clicked += () => RefreshContent("Scenes");
- tabs[2].clicked += () => RefreshContent("Prefabs");
- tabs[3].clicked += () => RefreshContent("Scriptable Objects");
- tabs[4].clicked += () => RefreshContent("Meta Files");
- tabs[5].clicked += () => RefreshContent("Settings");
- }
- private void RefreshContent(string tabName)
- {
- _currentTab = tabName;
- _contentContainer.Clear();
- if (tabName == "General")
- {
- DrawGeneralTab();
- return;
- }
- if (!_assetTrees.ContainsKey(tabName))
- {
- LoadAssetTree(tabName);
- }
- var rootModel = _assetTrees[tabName];
- if (rootModel != null)
- {
- foreach (var child in rootModel.Children)
- {
- _contentContainer.Add(CreateAssetView(child, 0));
- }
- }
- }
- private void DrawGeneralTab()
- {
- var title = new Label("Select Export Types") { style = { unityFontStyleAndWeight = FontStyle.Bold } };
- _contentContainer.Add(title);
- var scenesToggle = ProjectExporterUI.CreateToggle("Export Scenes", _exportScenes, evt => _exportScenes = evt.newValue);
- var prefabsToggle = ProjectExporterUI.CreateToggle("Export Prefabs", _exportPrefabs, evt => _exportPrefabs = evt.newValue);
- var soToggle = ProjectExporterUI.CreateToggle("Export Scriptable Objects", _exportScriptableObjects, evt => _exportScriptableObjects = evt.newValue);
- var metaToggle = ProjectExporterUI.CreateToggle("Export Meta Files", _exportMetaFiles, evt => _exportMetaFiles = evt.newValue);
- var settingsToggle = ProjectExporterUI.CreateToggle("Export Settings", _exportSettings, evt => _exportSettings = evt.newValue);
- _contentContainer.Add(scenesToggle);
- _contentContainer.Add(prefabsToggle);
- _contentContainer.Add(soToggle);
- _contentContainer.Add(metaToggle);
- _contentContainer.Add(settingsToggle);
- }
- private void PerformBulkExport()
- {
- if (_exportScenes) ExportAllOfType("Scenes");
- if (_exportPrefabs) ExportAllOfType("Prefabs");
- if (_exportScriptableObjects) ExportAllOfType("Scriptable Objects");
- if (_exportMetaFiles) ExportAllOfType("Meta Files");
- if (_exportSettings) ExportAllOfType("Settings");
-
- EditorUtility.DisplayDialog("Bulk Export", "Bulk export process finished.", "OK");
- }
- private void ExportAllOfType(string type)
- {
- if (!_assetTrees.ContainsKey(type))
- {
- LoadAssetTree(type);
- }
-
- var rootModel = _assetTrees[type];
- if (rootModel != null)
- {
- SetChildrenSelection(rootModel, true); // Select all assets
- _controller.ExportAssets(rootModel, type);
- }
- }
- private void LoadAssetTree(string tabName)
- {
- EditorUtility.DisplayProgressBar("Loading...", $"Fetching {tabName}...", 0.5f);
- try
- {
- switch (tabName)
- {
- case "Scenes":
- _assetTrees[tabName] = _controller.GetAssets("t:Scene");
- break;
- case "Prefabs":
- _assetTrees[tabName] = _controller.GetAssets("t:Prefab");
- break;
- case "Scriptable Objects":
- _assetTrees[tabName] = _controller.GetAssets("t:ScriptableObject");
- break;
- case "Meta Files":
- _assetTrees[tabName] = _controller.GetAllMetaFiles();
- break;
- case "Settings":
- _assetTrees[tabName] = _controller.GetProjectSettingsFiles();
- break;
- }
- }
- finally
- {
- EditorUtility.ClearProgressBar();
- }
- }
- private VisualElement CreateAssetView(AssetModel model, int indentLevel)
- {
- const int indentWidth = 20;
- if (model.Children.Count > 0)
- {
- var row = new VisualElement { style = { flexDirection = FlexDirection.Row, alignItems = Align.Center, marginLeft = indentLevel * indentWidth } };
- var foldout = ProjectExporterUI.CreateFoldout(model.Name);
- foldout.style.flexGrow = 1;
- var toggle = new Toggle { value = model.IsSelected, text = "" };
-
- toggle.RegisterValueChangedCallback(evt => {
- model.IsSelected = evt.newValue;
- SetChildrenSelection(model, evt.newValue);
- UpdateChildTogglesUI(foldout, evt.newValue);
- });
- row.Add(toggle);
- row.Add(foldout);
- foreach (var child in model.Children)
- {
- foldout.Add(CreateAssetView(child, indentLevel + 1));
- }
- return row;
- }
- else
- {
- var toggle = ProjectExporterUI.CreateToggle(model.Name);
- toggle.style.marginLeft = (indentLevel + 1) * indentWidth;
- toggle.value = model.IsSelected;
- toggle.RegisterValueChangedCallback(evt => model.IsSelected = evt.newValue);
- return toggle;
- }
- }
-
- private void UpdateChildTogglesUI(VisualElement container, bool isSelected)
- {
- container.Query<Toggle>().ForEach(childToggle => {
- childToggle.SetValueWithoutNotify(isSelected);
- });
- }
- private void SetChildrenSelection(AssetModel parent, bool isSelected)
- {
- parent.IsSelected = isSelected;
- foreach (var child in parent.Children)
- {
- SetChildrenSelection(child, isSelected);
- }
- }
- }
- }
|