DebuggerAssetListViewer.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #if UNITY_2019_4_OR_NEWER
  2. using System.IO;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityEditor.UIElements;
  8. using UnityEngine.UIElements;
  9. namespace YooAsset.Editor
  10. {
  11. internal class DebuggerAssetListViewer
  12. {
  13. private VisualTreeAsset _visualAsset;
  14. private TemplateContainer _root;
  15. private ListView _assetListView;
  16. private ListView _dependListView;
  17. private DebugReport _debugReport;
  18. /// <summary>
  19. /// 初始化页面
  20. /// </summary>
  21. public void InitViewer()
  22. {
  23. // 加载布局文件
  24. _visualAsset = EditorHelper.LoadWindowUXML<DebuggerAssetListViewer>();
  25. if (_visualAsset == null)
  26. return;
  27. _root = _visualAsset.CloneTree();
  28. _root.style.flexGrow = 1f;
  29. // 资源列表
  30. _assetListView = _root.Q<ListView>("TopListView");
  31. _assetListView.makeItem = MakeAssetListViewItem;
  32. _assetListView.bindItem = BindAssetListViewItem;
  33. #if UNITY_2020_1_OR_NEWER
  34. _assetListView.onSelectionChange += AssetListView_onSelectionChange;
  35. #else
  36. _assetListView.onSelectionChanged += AssetListView_onSelectionChange;
  37. #endif
  38. // 依赖列表
  39. _dependListView = _root.Q<ListView>("BottomListView");
  40. _dependListView.makeItem = MakeDependListViewItem;
  41. _dependListView.bindItem = BindDependListViewItem;
  42. }
  43. /// <summary>
  44. /// 清空页面
  45. /// </summary>
  46. public void ClearView()
  47. {
  48. _debugReport = null;
  49. _assetListView.Clear();
  50. _assetListView.ClearSelection();
  51. _assetListView.itemsSource.Clear();
  52. _assetListView.Rebuild();
  53. }
  54. /// <summary>
  55. /// 填充页面数据
  56. /// </summary>
  57. public void FillViewData(DebugReport debugReport, string searchKeyWord)
  58. {
  59. _debugReport = debugReport;
  60. _assetListView.Clear();
  61. _assetListView.ClearSelection();
  62. _assetListView.itemsSource = FilterViewItems(debugReport, searchKeyWord);
  63. _assetListView.Rebuild();
  64. }
  65. private List<DebugProviderInfo> FilterViewItems(DebugReport debugReport, string searchKeyWord)
  66. {
  67. var result = new List<DebugProviderInfo>(debugReport.ProviderInfos.Count);
  68. foreach (var providerInfo in debugReport.ProviderInfos)
  69. {
  70. if (string.IsNullOrEmpty(searchKeyWord) == false)
  71. {
  72. if (providerInfo.AssetPath.Contains(searchKeyWord) == false)
  73. continue;
  74. }
  75. result.Add(providerInfo);
  76. }
  77. return result;
  78. }
  79. /// <summary>
  80. /// 挂接到父类页面上
  81. /// </summary>
  82. public void AttachParent(VisualElement parent)
  83. {
  84. parent.Add(_root);
  85. }
  86. /// <summary>
  87. /// 从父类页面脱离开
  88. /// </summary>
  89. public void DetachParent()
  90. {
  91. _root.RemoveFromHierarchy();
  92. }
  93. // 资源列表相关
  94. private VisualElement MakeAssetListViewItem()
  95. {
  96. VisualElement element = new VisualElement();
  97. element.style.flexDirection = FlexDirection.Row;
  98. {
  99. var label = new Label();
  100. label.name = "Label1";
  101. label.style.unityTextAlign = TextAnchor.MiddleLeft;
  102. label.style.marginLeft = 3f;
  103. label.style.flexGrow = 1f;
  104. label.style.width = 280;
  105. element.Add(label);
  106. }
  107. {
  108. var label = new Label();
  109. label.name = "Label2";
  110. label.style.unityTextAlign = TextAnchor.MiddleLeft;
  111. label.style.marginLeft = 3f;
  112. //label.style.flexGrow = 1f;
  113. label.style.width = 150;
  114. element.Add(label);
  115. }
  116. {
  117. var label = new Label();
  118. label.name = "Label3";
  119. label.style.unityTextAlign = TextAnchor.MiddleLeft;
  120. label.style.marginLeft = 3f;
  121. //label.style.flexGrow = 1f;
  122. label.style.width = 150;
  123. element.Add(label);
  124. }
  125. {
  126. var label = new Label();
  127. label.name = "Label4";
  128. label.style.unityTextAlign = TextAnchor.MiddleLeft;
  129. label.style.marginLeft = 3f;
  130. //label.style.flexGrow = 1f;
  131. label.style.width = 100;
  132. element.Add(label);
  133. }
  134. {
  135. var label = new Label();
  136. label.name = "Label5";
  137. label.style.unityTextAlign = TextAnchor.MiddleLeft;
  138. label.style.marginLeft = 3f;
  139. //label.style.flexGrow = 1f;
  140. label.style.width = 120;
  141. element.Add(label);
  142. }
  143. return element;
  144. }
  145. private void BindAssetListViewItem(VisualElement element, int index)
  146. {
  147. var sourceData = _assetListView.itemsSource as List<DebugProviderInfo>;
  148. var providerInfo = sourceData[index];
  149. // Asset Path
  150. var label1 = element.Q<Label>("Label1");
  151. label1.text = providerInfo.AssetPath;
  152. // Spawn Scene
  153. var label2 = element.Q<Label>("Label2");
  154. label2.text = providerInfo.SpawnScene;
  155. // Spawn Time
  156. var label3 = element.Q<Label>("Label3");
  157. label3.text = providerInfo.SpawnTime;
  158. // Ref Count
  159. var label4 = element.Q<Label>("Label4");
  160. label4.text = providerInfo.RefCount.ToString();
  161. // Status
  162. StyleColor textColor;
  163. if (providerInfo.Status == (int)ProviderBase.EStatus.Fail)
  164. textColor = new StyleColor(Color.yellow);
  165. else
  166. textColor = label1.style.color;
  167. var label5 = element.Q<Label>("Label5");
  168. label5.text = providerInfo.Status.ToString();
  169. label5.style.color = textColor;
  170. }
  171. private void AssetListView_onSelectionChange(IEnumerable<object> objs)
  172. {
  173. foreach (var item in objs)
  174. {
  175. DebugProviderInfo providerInfo = item as DebugProviderInfo;
  176. FillDependListView(providerInfo);
  177. }
  178. }
  179. // 依赖列表相关
  180. private VisualElement MakeDependListViewItem()
  181. {
  182. VisualElement element = new VisualElement();
  183. element.style.flexDirection = FlexDirection.Row;
  184. {
  185. var label = new Label();
  186. label.name = "Label1";
  187. label.style.unityTextAlign = TextAnchor.MiddleLeft;
  188. label.style.marginLeft = 3f;
  189. label.style.flexGrow = 1f;
  190. label.style.width = 280;
  191. element.Add(label);
  192. }
  193. {
  194. var label = new Label();
  195. label.name = "Label3";
  196. label.style.unityTextAlign = TextAnchor.MiddleLeft;
  197. label.style.marginLeft = 3f;
  198. //label.style.flexGrow = 1f;
  199. label.style.width = 100;
  200. element.Add(label);
  201. }
  202. {
  203. var label = new Label();
  204. label.name = "Label4";
  205. label.style.unityTextAlign = TextAnchor.MiddleLeft;
  206. label.style.marginLeft = 3f;
  207. //label.style.flexGrow = 1f;
  208. label.style.width = 120;
  209. element.Add(label);
  210. }
  211. return element;
  212. }
  213. private void BindDependListViewItem(VisualElement element, int index)
  214. {
  215. List<DebugBundleInfo> bundles = _dependListView.itemsSource as List<DebugBundleInfo>;
  216. DebugBundleInfo bundleInfo = bundles[index];
  217. // Bundle Name
  218. var label1 = element.Q<Label>("Label1");
  219. label1.text = bundleInfo.BundleName;
  220. // Ref Count
  221. var label3 = element.Q<Label>("Label3");
  222. label3.text = bundleInfo.RefCount.ToString();
  223. // Status
  224. var label4 = element.Q<Label>("Label4");
  225. label4.text = bundleInfo.Status.ToString();
  226. }
  227. private void FillDependListView(DebugProviderInfo providerInfo)
  228. {
  229. _dependListView.Clear();
  230. _dependListView.ClearSelection();
  231. _dependListView.itemsSource = providerInfo.BundleInfos;
  232. _dependListView.Rebuild();
  233. }
  234. }
  235. }
  236. #endif