#if UNITY_2019_4_OR_NEWER using System.IO; using System.Linq; using System.Collections.Generic; using UnityEditor; using UnityEngine; using UnityEditor.UIElements; using UnityEngine.UIElements; namespace YooAsset.Editor { internal class DebuggerBundleListViewer { private VisualTreeAsset _visualAsset; private TemplateContainer _root; private ListView _bundleListView; private ListView _usingListView; private DebugReport _debugReport; /// /// 初始化页面 /// public void InitViewer() { // 加载布局文件 _visualAsset = UxmlLoader.LoadWindowUXML(); if (_visualAsset == null) return; _root = _visualAsset.CloneTree(); _root.style.flexGrow = 1f; // 资源包列表 _bundleListView = _root.Q("TopListView"); _bundleListView.makeItem = MakeBundleListViewItem; _bundleListView.bindItem = BindBundleListViewItem; #if UNITY_2020_1_OR_NEWER _bundleListView.onSelectionChange += BundleListView_onSelectionChange; #else _bundleListView.onSelectionChanged += BundleListView_onSelectionChange; #endif // 使用列表 _usingListView = _root.Q("BottomListView"); _usingListView.makeItem = MakeIncludeListViewItem; _usingListView.bindItem = BindIncludeListViewItem; #if UNITY_2020_3_OR_NEWER SplitView.Adjuster(_root); #endif } /// /// 清空页面 /// public void ClearView() { _debugReport = null; _bundleListView.Clear(); _bundleListView.ClearSelection(); _bundleListView.itemsSource.Clear(); _bundleListView.Rebuild(); } /// /// 填充页面数据 /// public void FillViewData(DebugReport debugReport, string searchKeyWord) { _debugReport = debugReport; _bundleListView.Clear(); _bundleListView.ClearSelection(); _bundleListView.itemsSource = FilterViewItems(debugReport, searchKeyWord); _bundleListView.Rebuild(); } private List FilterViewItems(DebugReport debugReport, string searchKeyWord) { List result = new List(1000); foreach (var pakcageData in debugReport.PackageDatas) { Dictionary tempDic = new Dictionary(1000); foreach (var providerInfo in pakcageData.ProviderInfos) { foreach (var bundleInfo in providerInfo.DependBundleInfos) { if (string.IsNullOrEmpty(searchKeyWord) == false) { if (bundleInfo.BundleName.Contains(searchKeyWord) == false) continue; } if (tempDic.ContainsKey(bundleInfo.BundleName) == false) { bundleInfo.PackageName = pakcageData.PackageName; tempDic.Add(bundleInfo.BundleName, bundleInfo); } } } var tempList = tempDic.Values.ToList(); tempList.Sort(); result.AddRange(tempList); } return result; } /// /// 挂接到父类页面上 /// public void AttachParent(VisualElement parent) { parent.Add(_root); } /// /// 从父类页面脱离开 /// public void DetachParent() { _root.RemoveFromHierarchy(); } // 顶部列表相关 private VisualElement MakeBundleListViewItem() { VisualElement element = new VisualElement(); element.style.flexDirection = FlexDirection.Row; { var label = new Label(); label.name = "Label0"; label.style.unityTextAlign = TextAnchor.MiddleLeft; label.style.marginLeft = 3f; //label.style.flexGrow = 1f; label.style.width = 150; element.Add(label); } { var label = new Label(); label.name = "Label1"; label.style.unityTextAlign = TextAnchor.MiddleLeft; label.style.marginLeft = 3f; label.style.flexGrow = 1f; label.style.width = 280; element.Add(label); } { var label = new Label(); label.name = "Label3"; label.style.unityTextAlign = TextAnchor.MiddleLeft; label.style.marginLeft = 3f; //label.style.flexGrow = 1f; label.style.width = 100; element.Add(label); } { var label = new Label(); label.name = "Label4"; label.style.unityTextAlign = TextAnchor.MiddleLeft; label.style.marginLeft = 3f; //label.style.flexGrow = 1f; label.style.width = 120; element.Add(label); } return element; } private void BindBundleListViewItem(VisualElement element, int index) { var sourceData = _bundleListView.itemsSource as List; var bundleInfo = sourceData[index]; // Package Name var label0 = element.Q