AssetBundleReporterWindow.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #if UNITY_2019_4_OR_NEWER
  2. using System;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityEditor.UIElements;
  6. using UnityEngine.UIElements;
  7. namespace YooAsset.Editor
  8. {
  9. public class AssetBundleReporterWindow : EditorWindow
  10. {
  11. [MenuItem("YooAsset/AssetBundle Reporter", false, 103)]
  12. public static void ShowExample()
  13. {
  14. AssetBundleReporterWindow window = GetWindow<AssetBundleReporterWindow>("资源包报告工具", true, EditorDefine.DockedWindowTypes);
  15. window.minSize = new Vector2(800, 600);
  16. }
  17. /// <summary>
  18. /// 视图模式
  19. /// </summary>
  20. private enum EViewMode
  21. {
  22. /// <summary>
  23. /// 概览视图
  24. /// </summary>
  25. Summary,
  26. /// <summary>
  27. /// 资源对象视图
  28. /// </summary>
  29. AssetView,
  30. /// <summary>
  31. /// 资源包视图
  32. /// </summary>
  33. BundleView,
  34. }
  35. private ToolbarMenu _viewModeMenu;
  36. private ReporterSummaryViewer _summaryViewer;
  37. private ReporterAssetListViewer _assetListViewer;
  38. private ReporterBundleListViewer _bundleListViewer;
  39. private EViewMode _viewMode;
  40. private BuildReport _buildReport;
  41. private string _reportFilePath;
  42. private string _searchKeyWord;
  43. public void CreateGUI()
  44. {
  45. try
  46. {
  47. VisualElement root = this.rootVisualElement;
  48. // 加载布局文件
  49. var visualAsset = EditorHelper.LoadWindowUXML<AssetBundleReporterWindow>();
  50. if (visualAsset == null)
  51. return;
  52. visualAsset.CloneTree(root);
  53. // 导入按钮
  54. var importBtn = root.Q<Button>("ImportButton");
  55. importBtn.clicked += ImportBtn_onClick;
  56. // 视图模式菜单
  57. _viewModeMenu = root.Q<ToolbarMenu>("ViewModeMenu");
  58. _viewModeMenu.menu.AppendAction(EViewMode.Summary.ToString(), ViewModeMenuAction0, ViewModeMenuFun0);
  59. _viewModeMenu.menu.AppendAction(EViewMode.AssetView.ToString(), ViewModeMenuAction1, ViewModeMenuFun1);
  60. _viewModeMenu.menu.AppendAction(EViewMode.BundleView.ToString(), ViewModeMenuAction2, ViewModeMenuFun2);
  61. // 搜索栏
  62. var searchField = root.Q<ToolbarSearchField>("SearchField");
  63. searchField.RegisterValueChangedCallback(OnSearchKeyWordChange);
  64. // 加载视图
  65. _summaryViewer = new ReporterSummaryViewer();
  66. _summaryViewer.InitViewer();
  67. // 加载视图
  68. _assetListViewer = new ReporterAssetListViewer();
  69. _assetListViewer.InitViewer();
  70. // 加载视图
  71. _bundleListViewer = new ReporterBundleListViewer();
  72. _bundleListViewer.InitViewer();
  73. // 显示视图
  74. _viewMode = EViewMode.Summary;
  75. _viewModeMenu.text = EViewMode.Summary.ToString();
  76. _summaryViewer.AttachParent(root);
  77. }
  78. catch (Exception e)
  79. {
  80. Debug.LogError(e.ToString());
  81. }
  82. }
  83. public void OnDestroy()
  84. {
  85. AssetBundleRecorder.UnloadAll();
  86. }
  87. private void ImportBtn_onClick()
  88. {
  89. string selectFilePath = EditorUtility.OpenFilePanel("导入报告", EditorTools.GetProjectPath(), "json");
  90. if (string.IsNullOrEmpty(selectFilePath))
  91. return;
  92. _reportFilePath = selectFilePath;
  93. string jsonData = FileUtility.ReadFile(_reportFilePath);
  94. _buildReport = BuildReport.Deserialize(jsonData);
  95. _assetListViewer.FillViewData(_buildReport, _searchKeyWord);
  96. _bundleListViewer.FillViewData(_buildReport, _reportFilePath, _searchKeyWord);
  97. _summaryViewer.FillViewData(_buildReport);
  98. }
  99. private void OnSearchKeyWordChange(ChangeEvent<string> e)
  100. {
  101. _searchKeyWord = e.newValue;
  102. if (_buildReport != null)
  103. {
  104. _assetListViewer.FillViewData(_buildReport, _searchKeyWord);
  105. _bundleListViewer.FillViewData(_buildReport, _reportFilePath, _searchKeyWord);
  106. }
  107. }
  108. private void ViewModeMenuAction0(DropdownMenuAction action)
  109. {
  110. if (_viewMode != EViewMode.Summary)
  111. {
  112. _viewMode = EViewMode.Summary;
  113. VisualElement root = this.rootVisualElement;
  114. _viewModeMenu.text = EViewMode.Summary.ToString();
  115. _summaryViewer.AttachParent(root);
  116. _assetListViewer.DetachParent();
  117. _bundleListViewer.DetachParent();
  118. }
  119. }
  120. private void ViewModeMenuAction1(DropdownMenuAction action)
  121. {
  122. if (_viewMode != EViewMode.AssetView)
  123. {
  124. _viewMode = EViewMode.AssetView;
  125. VisualElement root = this.rootVisualElement;
  126. _viewModeMenu.text = EViewMode.AssetView.ToString();
  127. _summaryViewer.DetachParent();
  128. _assetListViewer.AttachParent(root);
  129. _bundleListViewer.DetachParent();
  130. }
  131. }
  132. private void ViewModeMenuAction2(DropdownMenuAction action)
  133. {
  134. if (_viewMode != EViewMode.BundleView)
  135. {
  136. _viewMode = EViewMode.BundleView;
  137. VisualElement root = this.rootVisualElement;
  138. _viewModeMenu.text = EViewMode.BundleView.ToString();
  139. _summaryViewer.DetachParent();
  140. _assetListViewer.DetachParent();
  141. _bundleListViewer.AttachParent(root);
  142. }
  143. }
  144. private DropdownMenuAction.Status ViewModeMenuFun0(DropdownMenuAction action)
  145. {
  146. if (_viewMode == EViewMode.Summary)
  147. return DropdownMenuAction.Status.Checked;
  148. else
  149. return DropdownMenuAction.Status.Normal;
  150. }
  151. private DropdownMenuAction.Status ViewModeMenuFun1(DropdownMenuAction action)
  152. {
  153. if (_viewMode == EViewMode.AssetView)
  154. return DropdownMenuAction.Status.Checked;
  155. else
  156. return DropdownMenuAction.Status.Normal;
  157. }
  158. private DropdownMenuAction.Status ViewModeMenuFun2(DropdownMenuAction action)
  159. {
  160. if (_viewMode == EViewMode.BundleView)
  161. return DropdownMenuAction.Status.Checked;
  162. else
  163. return DropdownMenuAction.Status.Normal;
  164. }
  165. }
  166. }
  167. #endif