using System; using System.Collections.Generic; using System.Reflection; using System.Threading.Tasks; using UnityEditor.UIElements; using UnityEngine; using UnityEngine.UIElements; using Object = UnityEngine.Object; namespace UnityEditor.U2D.Tooling.Analyzer { class AnalyzerWindow : EditorWindow, IDataSourceProvider { const string k_SaveFilePath = "Library/com.unity.2d.tooling/AnalyzerWindow/AnalyzerData.json"; AnalyzerWindowSaveData m_SaveData = new(); List m_AllReports; List m_Reports; List m_DataSource; event Action m_OnDataSourceChange; Button m_AnalyzeButton; Button m_AnalyzeDataSourceButton; Button m_ClearDataSourceDataButton; ReportListView m_ReportListView; VisualElement m_ReportContentView; VisualElement m_ReportContentWithSettingsView; VisualElement m_ReportSettingView; VisualElement m_EmptyState; VisualElement m_DataSourceSetting; ToolbarButton m_HelpButton; DataSourceList m_DataSourceListView; TwoPaneSplitView m_SplitViewReport; Label m_ReportHeaderLabel; VisualElement m_ReportArea; bool m_Capturing; [MenuItem("Window/Analysis/Sprite Atlas Analyzer")] static void OpenWindow() { var window = GetWindow(); window.Show(); } internal async Task Analyze(string[] path) { if (m_Capturing) { for(int i = 0; i < m_SaveData.reportDataSource.Count; ++i) { if(m_SaveData.reportDataSource[i].reportDataSource.capturing) m_SaveData.reportDataSource[i].reportDataSource.StopCapture(); } OnDataSourceCaptureEnd(null); } if (path == null) { if (!m_Capturing) { Capture(null, false); } } else { // wait for previous capture to finish before we start a new one from link while (m_Capturing) await Task.Delay(100); Capture(path, true); } } void Capture(string[] path, bool enableAll) { m_Capturing = true; m_AnalyzeButton.AddToClassList("analyze-button-stop"); m_AnalyzeDataSourceButton.AddToClassList("analyze-button-stop"); m_AnalyzeButton.text = "Stop"; m_AnalyzeDataSourceButton.SetEnabled(false); int dataSourceCapturing = 0; enableAll |= m_SaveData.reportDataSource.Count == 1; for(int i = 0; i < m_SaveData.reportDataSource.Count; ++i) { if (m_SaveData.reportDataSource[i].enabled || enableAll) { var reportDataSource = m_SaveData.reportDataSource[i].reportDataSource; reportDataSource.onCaptureEnd -= OnDataSourceCaptureEnd; var paths = path ?? m_SaveData.reportDataSource[i].assetSearchPath; reportDataSource.Capture(Utilities.RemoveChildDirectories(paths)); ++dataSourceCapturing; } } for (int i = 0; i < m_SaveData.reportDataSource.Count; ++i) { if (m_SaveData.reportDataSource[i].enabled || enableAll) { var reportDataSource = m_SaveData.reportDataSource[i].reportDataSource; if(reportDataSource.capturing) reportDataSource.onCaptureEnd += OnDataSourceCaptureEnd; else OnDataSourceCaptureEnd(reportDataSource); } } if (dataSourceCapturing == 0) OnDataSourceCaptureEnd(null); } protected void OnEnable() { titleContent = new GUIContent("Sprite Atlas Analyzer"); m_SaveData = Utilities.LoadSaveDataFromFile(k_SaveFilePath) ?? new AnalyzerWindowSaveData(); } protected void OnDisable() { for(int i = 0; i < m_AllReports?.Count; ++i) { m_AllReports[i].onInspectObject -= SelectUnityObject; m_AllReports[i].Dispose(); } m_AllReports = null; for(int i = 0; i < m_DataSource?.Count; ++i) { m_DataSource[i].Dispose(); } m_DataSource = null; Utilities.WriteSaveDataToFile(k_SaveFilePath, m_SaveData); } public void CreateGUI() { rootVisualElement.Add(GetWindowContent()); } VisualElement GetWindowContent() { // Instantiate UXML VisualElement view = AssetDatabase.LoadAssetAtPath("Packages/com.unity.2d.tooling/Editor/Insider/AnalyzerWindow/AnalyzerWindow.uxml").Instantiate(); if(EditorGUIUtility.isProSkin) view.AddToClassList("dark"); m_ReportListView = view.Q("IssueList"); m_ReportListView.makeHeader = () => { var label = new Label { name = "IssueListHeaderLabel", text = m_ReportListView.headerTitle }; label.AddToClassList("issuelist-header-label"); return label; }; m_ReportListView.selectionChanged += OnSelectionChanged; m_ReportArea = view.Q("ReportArea"); m_ReportArea.style.display = DisplayStyle.None; m_ReportContentView = view.Q("ReportContainer"); m_ReportContentView.style.display = DisplayStyle.None; m_ReportHeaderLabel = view.Q