Files
YooAsset/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/DebuggerAssetListViewer.cs

445 lines
18 KiB
C#
Raw Normal View History

2022-03-15 22:30:57 +08:00
#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
{
2023-12-21 19:29:26 +08:00
internal class DebuggerAssetListViewer
{
2025-01-20 19:14:29 +08:00
private class ProviderTableData : DefaultTableData
{
public DebugProviderInfo ProviderInfo;
}
private class DependTableData : DefaultTableData
{
public DebugBundleInfo BundleInfo;
}
2023-12-21 19:29:26 +08:00
private VisualTreeAsset _visualAsset;
private TemplateContainer _root;
2025-01-20 19:14:29 +08:00
private TableView _providerTableView;
private TableView _dependTableView;
2023-12-21 19:29:26 +08:00
private DebugReport _debugReport;
2025-01-20 19:14:29 +08:00
private List<ITableData> _sourceDatas;
2023-12-21 19:29:26 +08:00
/// <summary>
/// 初始化页面
/// </summary>
public void InitViewer()
{
// 加载布局文件
_visualAsset = UxmlLoader.LoadWindowUXML<DebuggerAssetListViewer>();
if (_visualAsset == null)
return;
_root = _visualAsset.CloneTree();
_root.style.flexGrow = 1f;
// 资源列表
2025-01-20 19:14:29 +08:00
_providerTableView = _root.Q<TableView>("TopTableView");
_providerTableView.SelectionChangedEvent = OnProviderTableViewSelectionChanged;
CreateAssetTableViewColumns();
2022-03-31 10:51:43 +08:00
2023-12-21 19:29:26 +08:00
// 依赖列表
2025-01-20 19:14:29 +08:00
_dependTableView = _root.Q<TableView>("BottomTableView");
CreateDependTableViewColumns();
#if UNITY_2020_3_OR_NEWER
2023-12-21 19:29:26 +08:00
SplitView.Adjuster(_root);
#endif
2023-12-21 19:29:26 +08:00
}
2025-01-20 19:14:29 +08:00
private void CreateAssetTableViewColumns()
2023-12-21 19:29:26 +08:00
{
2025-01-20 19:14:29 +08:00
// PackageName
2023-12-21 19:29:26 +08:00
{
2025-01-20 19:14:29 +08:00
var columnStyle = new ColumnStyle();
columnStyle.Width = 150;
columnStyle.Stretchable = false;
columnStyle.Searchable = false;
columnStyle.Sortable = true;
var column = new TableColumn("PackageName", "Package Name", columnStyle);
column.MakeCell = () =>
2023-12-21 19:29:26 +08:00
{
2025-01-20 19:14:29 +08:00
var label = new Label();
label.style.unityTextAlign = TextAnchor.MiddleLeft;
label.style.marginLeft = 3f;
label.style.flexGrow = columnStyle.Stretchable ? 1f : 0f;
label.style.width = columnStyle.Width;
label.style.maxWidth = columnStyle.MaxWidth;
label.style.minWidth = columnStyle.MinWidth;
return label;
};
column.BindCell = (VisualElement element, ITableData data, ITableCell cell) =>
{
var infoLabel = element as Label;
infoLabel.text = (string)cell.GetDisplayObject();
};
_providerTableView.AddColumn(column);
2023-12-21 19:29:26 +08:00
}
2025-01-20 19:14:29 +08:00
// AssetPath
2023-12-21 19:29:26 +08:00
{
2025-01-20 19:14:29 +08:00
var columnStyle = new ColumnStyle();
columnStyle.Width = 300;
columnStyle.Stretchable = true;
columnStyle.Searchable = true;
columnStyle.Sortable = true;
var column = new TableColumn("AssetPath", "Asset Path", columnStyle);
column.MakeCell = () =>
{
var label = new Label();
label.style.unityTextAlign = TextAnchor.MiddleLeft;
label.style.marginLeft = 3f;
label.style.flexGrow = columnStyle.Stretchable ? 1f : 0f;
label.style.width = columnStyle.Width;
label.style.maxWidth = columnStyle.MaxWidth;
label.style.minWidth = columnStyle.MinWidth;
return label;
};
column.BindCell = (VisualElement element, ITableData data, ITableCell cell) =>
{
var infoLabel = element as Label;
infoLabel.text = (string)cell.GetDisplayObject();
};
_providerTableView.AddColumn(column);
2023-12-21 19:29:26 +08:00
}
2025-01-20 19:14:29 +08:00
// SpawnScene
2023-12-21 19:29:26 +08:00
{
2025-01-20 19:14:29 +08:00
var columnStyle = new ColumnStyle();
columnStyle.Width = 150;
columnStyle.Stretchable = false;
columnStyle.Searchable = false;
columnStyle.Sortable = true;
var column = new TableColumn("SpawnScene", "Spawn Scene", columnStyle);
column.MakeCell = () =>
{
var label = new Label();
label.style.unityTextAlign = TextAnchor.MiddleLeft;
label.style.marginLeft = 3f;
label.style.flexGrow = columnStyle.Stretchable ? 1f : 0f;
label.style.width = columnStyle.Width;
label.style.maxWidth = columnStyle.MaxWidth;
label.style.minWidth = columnStyle.MinWidth;
return label;
};
column.BindCell = (VisualElement element, ITableData data, ITableCell cell) =>
{
var infoLabel = element as Label;
infoLabel.text = (string)cell.GetDisplayObject();
};
_providerTableView.AddColumn(column);
2023-12-21 19:29:26 +08:00
}
2025-01-20 19:14:29 +08:00
// SpawnTime
2023-12-21 19:29:26 +08:00
{
2025-01-20 19:14:29 +08:00
var columnStyle = new ColumnStyle();
columnStyle.Width = 150;
columnStyle.Stretchable = false;
columnStyle.Searchable = false;
columnStyle.Sortable = true;
var column = new TableColumn("SpawnTime", "Spawn Time", columnStyle);
column.MakeCell = () =>
{
var label = new Label();
label.style.unityTextAlign = TextAnchor.MiddleLeft;
label.style.marginLeft = 3f;
label.style.flexGrow = columnStyle.Stretchable ? 1f : 0f;
label.style.width = columnStyle.Width;
label.style.maxWidth = columnStyle.MaxWidth;
label.style.minWidth = columnStyle.MinWidth;
return label;
};
column.BindCell = (VisualElement element, ITableData data, ITableCell cell) =>
{
var infoLabel = element as Label;
infoLabel.text = (string)cell.GetDisplayObject();
};
_providerTableView.AddColumn(column);
2023-12-21 19:29:26 +08:00
}
2025-01-20 19:14:29 +08:00
// LoadingTime
2023-12-21 19:29:26 +08:00
{
2025-01-20 19:14:29 +08:00
var columnStyle = new ColumnStyle();
columnStyle.Width = 150;
columnStyle.Stretchable = false;
columnStyle.Searchable = false;
columnStyle.Sortable = true;
var column = new TableColumn("LoadingTime", "Loading Time", columnStyle);
column.MakeCell = () =>
{
var label = new Label();
label.style.unityTextAlign = TextAnchor.MiddleLeft;
label.style.marginLeft = 3f;
label.style.flexGrow = columnStyle.Stretchable ? 1f : 0f;
label.style.width = columnStyle.Width;
label.style.maxWidth = columnStyle.MaxWidth;
label.style.minWidth = columnStyle.MinWidth;
return label;
};
column.BindCell = (VisualElement element, ITableData data, ITableCell cell) =>
{
var infoLabel = element as Label;
infoLabel.text = (string)cell.GetDisplayObject();
};
_providerTableView.AddColumn(column);
2023-12-21 19:29:26 +08:00
}
2025-01-20 19:14:29 +08:00
// RefCount
2023-12-21 19:29:26 +08:00
{
2025-01-20 19:14:29 +08:00
var columnStyle = new ColumnStyle();
columnStyle.Width = 150;
columnStyle.Stretchable = false;
columnStyle.Searchable = false;
columnStyle.Sortable = true;
var column = new TableColumn("RefCount", "Ref Count", columnStyle);
column.MakeCell = () =>
{
var label = new Label();
label.style.unityTextAlign = TextAnchor.MiddleLeft;
label.style.marginLeft = 3f;
label.style.flexGrow = columnStyle.Stretchable ? 1f : 0f;
label.style.width = columnStyle.Width;
label.style.maxWidth = columnStyle.MaxWidth;
label.style.minWidth = columnStyle.MinWidth;
return label;
};
column.BindCell = (VisualElement element, ITableData data, ITableCell cell) =>
{
var infoLabel = element as Label;
infoLabel.text = (string)cell.GetDisplayObject();
};
_providerTableView.AddColumn(column);
2023-12-21 19:29:26 +08:00
}
2025-01-20 19:14:29 +08:00
// Status
2023-12-21 19:29:26 +08:00
{
2025-01-20 19:14:29 +08:00
var columnStyle = new ColumnStyle();
columnStyle.Width = 150;
columnStyle.Stretchable = false;
columnStyle.Searchable = false;
columnStyle.Sortable = true;
var column = new TableColumn("Status", "Status", columnStyle);
column.MakeCell = () =>
{
var label = new Label();
label.style.unityTextAlign = TextAnchor.MiddleLeft;
label.style.marginLeft = 3f;
label.style.flexGrow = columnStyle.Stretchable ? 1f : 0f;
label.style.width = columnStyle.Width;
label.style.maxWidth = columnStyle.MaxWidth;
label.style.minWidth = columnStyle.MinWidth;
return label;
};
column.BindCell = (VisualElement element, ITableData data, ITableCell cell) =>
{
StyleColor textColor;
var providerTableData = data as ProviderTableData;
if(providerTableData.ProviderInfo.Status == EOperationStatus.Failed.ToString())
textColor = new StyleColor(Color.yellow);
else
textColor = new StyleColor(Color.white);
var infoLabel = element as Label;
infoLabel.text = (string)cell.GetDisplayObject();
infoLabel.style.color = textColor;
};
_providerTableView.AddColumn(column);
2023-12-21 19:29:26 +08:00
}
}
2025-01-20 19:14:29 +08:00
private void CreateDependTableViewColumns()
2023-12-21 19:29:26 +08:00
{
2025-01-20 19:14:29 +08:00
//DependBundles
{
var columnStyle = new ColumnStyle();
columnStyle.Width = 300;
columnStyle.Stretchable = true;
columnStyle.Searchable = true;
columnStyle.Sortable = true;
var column = new TableColumn("DependBundles", "Depend Bundles", columnStyle);
column.MakeCell = () =>
{
var label = new Label();
label.style.unityTextAlign = TextAnchor.MiddleLeft;
label.style.marginLeft = 3f;
label.style.flexGrow = columnStyle.Stretchable ? 1f : 0f;
label.style.width = columnStyle.Width;
label.style.maxWidth = columnStyle.MaxWidth;
label.style.minWidth = columnStyle.MinWidth;
return label;
};
column.BindCell = (VisualElement element, ITableData data, ITableCell cell) =>
{
var infoLabel = element as Label;
infoLabel.text = (string)cell.GetDisplayObject();
};
_dependTableView.AddColumn(column);
}
2023-12-21 19:29:26 +08:00
2025-01-20 19:14:29 +08:00
// RefCount
{
var columnStyle = new ColumnStyle();
columnStyle.Width = 150;
columnStyle.Stretchable = false;
columnStyle.Searchable = false;
columnStyle.Sortable = true;
var column = new TableColumn("RefCount", "Ref Count", columnStyle);
column.MakeCell = () =>
{
var label = new Label();
label.style.unityTextAlign = TextAnchor.MiddleLeft;
label.style.marginLeft = 3f;
label.style.flexGrow = columnStyle.Stretchable ? 1f : 0f;
label.style.width = columnStyle.Width;
label.style.maxWidth = columnStyle.MaxWidth;
label.style.minWidth = columnStyle.MinWidth;
return label;
};
column.BindCell = (VisualElement element, ITableData data, ITableCell cell) =>
{
var infoLabel = element as Label;
infoLabel.text = (string)cell.GetDisplayObject();
};
_dependTableView.AddColumn(column);
}
2023-12-21 19:29:26 +08:00
// Status
{
2025-01-20 19:14:29 +08:00
var columnStyle = new ColumnStyle();
columnStyle.Width = 150;
columnStyle.Stretchable = false;
columnStyle.Searchable = false;
columnStyle.Sortable = true;
var column = new TableColumn("Status", "Status", columnStyle);
column.MakeCell = () =>
{
var label = new Label();
label.style.unityTextAlign = TextAnchor.MiddleLeft;
label.style.marginLeft = 3f;
label.style.flexGrow = columnStyle.Stretchable ? 1f : 0f;
label.style.width = columnStyle.Width;
label.style.maxWidth = columnStyle.MaxWidth;
label.style.minWidth = columnStyle.MinWidth;
return label;
};
column.BindCell = (VisualElement element, ITableData data, ITableCell cell) =>
{
StyleColor textColor;
var dependTableData = data as DependTableData;
if (dependTableData.BundleInfo.Status == EOperationStatus.Failed)
textColor = new StyleColor(Color.yellow);
else
textColor = new StyleColor(Color.white);
var infoLabel = element as Label;
infoLabel.text = (string)cell.GetDisplayObject();
infoLabel.style.color = textColor;
};
_dependTableView.AddColumn(column);
2023-12-21 19:29:26 +08:00
}
}
2025-01-20 19:14:29 +08:00
/// <summary>
/// 填充页面数据
/// </summary>
public void FillViewData(DebugReport debugReport)
2023-12-21 19:29:26 +08:00
{
2025-01-20 19:14:29 +08:00
_debugReport = debugReport;
2023-12-21 19:29:26 +08:00
2025-01-20 19:14:29 +08:00
// 清空旧数据
_providerTableView.ClearAll(false, true);
_dependTableView.ClearAll(false, true);
2023-12-21 19:29:26 +08:00
2025-01-20 19:14:29 +08:00
// 填充数据源
_sourceDatas = new List<ITableData>(1000);
foreach (var packageData in debugReport.PackageDatas)
2023-12-21 19:29:26 +08:00
{
2025-01-20 19:14:29 +08:00
foreach (var providerInfo in packageData.ProviderInfos)
{
var rowData = new ProviderTableData();
rowData.ProviderInfo = providerInfo;
rowData.AddAssetPathCell("PackageName", packageData.PackageName);
rowData.AddStringValueCell("AssetPath", providerInfo.AssetPath);
rowData.AddStringValueCell("SpawnScene", providerInfo.SpawnScene);
rowData.AddStringValueCell("SpawnTime", providerInfo.SpawnTime);
rowData.AddLongValueCell("LoadingTime", providerInfo.LoadingTime);
rowData.AddLongValueCell("RefCount", providerInfo.RefCount);
rowData.AddStringValueCell("Status", providerInfo.Status.ToString());
_sourceDatas.Add(rowData);
}
2023-12-21 19:29:26 +08:00
}
2025-01-20 19:14:29 +08:00
_providerTableView.itemsSource = _sourceDatas;
2023-12-21 19:29:26 +08:00
2025-01-20 19:14:29 +08:00
// 重建视图
RebuildView(null);
}
2023-12-21 19:29:26 +08:00
2025-01-20 19:14:29 +08:00
/// <summary>
/// 清空页面
/// </summary>
public void ClearView()
{
_debugReport = null;
_providerTableView.ClearAll(false, true);
_dependTableView.ClearAll(false, true);
RebuildView(null);
2023-12-21 19:29:26 +08:00
}
2025-01-20 19:14:29 +08:00
/// <summary>
/// 重建视图
/// </summary>
public void RebuildView(string searchKeyWord)
2023-12-21 19:29:26 +08:00
{
2025-01-20 19:14:29 +08:00
// 搜索匹配
DefaultSearchSystem.Search(_sourceDatas, searchKeyWord);
2023-12-21 19:29:26 +08:00
2025-01-20 19:14:29 +08:00
// 重建视图
_providerTableView.RebuildView();
}
2023-12-21 19:29:26 +08:00
2025-01-20 19:14:29 +08:00
/// <summary>
/// 挂接到父类页面上
/// </summary>
public void AttachParent(VisualElement parent)
{
parent.Add(_root);
}
2023-12-21 19:29:26 +08:00
2025-01-20 19:14:29 +08:00
/// <summary>
/// 从父类页面脱离开
/// </summary>
public void DetachParent()
{
_root.RemoveFromHierarchy();
2023-12-21 19:29:26 +08:00
}
2025-01-20 19:14:29 +08:00
private void OnProviderTableViewSelectionChanged(ITableData data)
2023-12-21 19:29:26 +08:00
{
2025-01-20 19:14:29 +08:00
var providerTableData = data as ProviderTableData;
DebugProviderInfo providerInfo = providerTableData.ProviderInfo;
// 填充依赖数据
var sourceDatas = new List<ITableData>(providerInfo.DependBundleInfos.Count);
foreach (var dependBundleInfo in providerInfo.DependBundleInfos)
{
var rowData = new DependTableData();
rowData.BundleInfo = dependBundleInfo;
rowData.AddStringValueCell("DependBundles", dependBundleInfo.BundleName);
rowData.AddLongValueCell("RefCount", dependBundleInfo.RefCount);
rowData.AddStringValueCell("Status", dependBundleInfo.Status.ToString());
sourceDatas.Add(rowData);
}
_dependTableView.itemsSource = sourceDatas;
_dependTableView.RebuildView();
2023-12-21 19:29:26 +08:00
}
}
2022-03-15 22:30:57 +08:00
}
#endif