ComponentStatsSettingsProvider.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using AssetBank.Editor.Tools;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace AssetBank.Settings
  8. {
  9. /// <summary>
  10. /// Hosts the Component Stats UI within the Project Settings window.
  11. /// </summary>
  12. internal class ComponentStatsSettingsProvider : SettingsProvider
  13. {
  14. private ComponentStatsController _controller;
  15. private ComponentStatsController.ScanSource _scanSource = ComponentStatsController.ScanSource.Both;
  16. private Dictionary<string, ComponentStats> _stats = new();
  17. private Vector2 _scrollPosition;
  18. private GUIStyle _alternatingRowStyle;
  19. private ComponentStatsSettings _statsSettings;
  20. const string k_SettingsPath = "Project/Project AssetDatabase/Component Stats";
  21. public ComponentStatsSettingsProvider(string path, SettingsScope scope = SettingsScope.Project)
  22. : base(path, scope) {}
  23. public override void OnActivate(string searchContext, UnityEngine.UIElements.VisualElement rootElement)
  24. {
  25. _controller = new ComponentStatsController();
  26. _statsSettings = ComponentStatsSettings.GetOrCreateSettings();
  27. LoadStatsFromSettings();
  28. // Create a style for alternating row colors
  29. _alternatingRowStyle = new GUIStyle();
  30. var texture = new Texture2D(1, 1);
  31. var color = EditorGUIUtility.isProSkin
  32. ? new Color(1f, 1f, 1f, 0.05f)
  33. : new Color(0f, 0f, 0f, 0.05f);
  34. texture.SetPixel(0, 0, color);
  35. texture.Apply();
  36. _alternatingRowStyle.normal.background = texture;
  37. }
  38. public override void OnGUI(string searchContext)
  39. {
  40. EditorGUILayout.LabelField("Component Stats Generator", EditorStyles.boldLabel);
  41. EditorGUILayout.Space();
  42. EditorGUILayout.HelpBox("Scan project assets to find all unique component types and their statistics.", MessageType.Info);
  43. if (!string.IsNullOrEmpty(_statsSettings.LastScanTime))
  44. {
  45. EditorGUILayout.HelpBox($"Last Scan: {_statsSettings.LastScanTime}", MessageType.None);
  46. }
  47. _scanSource = (ComponentStatsController.ScanSource)EditorGUILayout.EnumPopup("Asset Source to Scan", _scanSource);
  48. if (GUILayout.Button("Scan Project for Components"))
  49. {
  50. var newStats = _controller.ScanProject(_scanSource);
  51. _stats = newStats;
  52. // Immediately save the new scan results and update the timestamp
  53. _controller.UpdateAndSaveScanResults(_stats);
  54. }
  55. EditorGUILayout.Space();
  56. DrawHeader();
  57. DrawComponentList();
  58. EditorGUILayout.Space();
  59. if (GUILayout.Button("Save Stats"))
  60. {
  61. _controller.SaveStats(_stats);
  62. }
  63. if (GUI.changed)
  64. {
  65. EditorUtility.SetDirty(_statsSettings);
  66. AssetDatabase.SaveAssets();
  67. }
  68. }
  69. private void DrawHeader()
  70. {
  71. EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
  72. GUILayout.Label("Component Name", GUILayout.MinWidth(150), GUILayout.ExpandWidth(true));
  73. GUILayout.Label("Occurrences", GUILayout.Width(80));
  74. GUILayout.Label("Max Props", GUILayout.Width(70));
  75. GUILayout.Label("Total Props", GUILayout.Width(80));
  76. EditorGUILayout.EndHorizontal();
  77. }
  78. private void DrawComponentList()
  79. {
  80. _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition, GUILayout.ExpandHeight(true));
  81. if (_stats.Count == 0)
  82. {
  83. EditorGUILayout.LabelField("No components found. Please run a scan.", EditorStyles.centeredGreyMiniLabel);
  84. }
  85. else
  86. {
  87. var sortedStats = _stats.Values.OrderBy(s => s.Name).ToList();
  88. int i = 0;
  89. foreach (var stat in sortedStats)
  90. {
  91. var style = (i++ % 2 == 0) ? GUIStyle.none : _alternatingRowStyle;
  92. EditorGUILayout.BeginHorizontal(style);
  93. EditorGUILayout.LabelField(stat.Name, GUILayout.MinWidth(150), GUILayout.ExpandWidth(true));
  94. EditorGUILayout.LabelField(stat.TotalOccurrences.ToString(), GUILayout.Width(80));
  95. EditorGUILayout.LabelField(stat.MaxProperties.ToString(), GUILayout.Width(70));
  96. EditorGUILayout.LabelField(stat.TotalProperties.ToString(), GUILayout.Width(80));
  97. EditorGUILayout.EndHorizontal();
  98. }
  99. }
  100. EditorGUILayout.EndScrollView();
  101. }
  102. private void LoadStatsFromSettings()
  103. {
  104. _stats = _statsSettings.ComponentStats.ToDictionary(s => s.Name, s => s);
  105. }
  106. [SettingsProvider]
  107. public static SettingsProvider CreateSettingsProvider()
  108. {
  109. var provider = new ComponentStatsSettingsProvider(k_SettingsPath, SettingsScope.Project);
  110. return provider;
  111. }
  112. }
  113. }