com.alicizax.unity.tuyoogam.../Editor/AssetBundleReporter/VisualViewers/ReporterAssetListViewer.cs

292 lines
10 KiB
C#
Raw Normal View History

2025-01-09 11:31:04 +08:00
#if UNITY_2019_4_OR_NEWER
using System;
using System.Linq;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
2025-02-28 16:11:01 +08:00
using System.IO;
2025-01-09 11:31:04 +08:00
namespace YooAsset.Editor
{
internal class ReporterAssetListViewer
{
2025-02-28 16:11:01 +08:00
private class AssetTableData : DefaultTableData
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
public ReportAssetInfo AssetInfo;
}
private class DependTableData : DefaultTableData
{
public ReportBundleInfo BundleInfo;
2025-01-09 11:31:04 +08:00
}
private VisualTreeAsset _visualAsset;
private TemplateContainer _root;
2025-04-01 21:12:28 +08:00
private TableViewer _assetTableView;
private TableViewer _dependTableView;
2025-01-09 11:31:04 +08:00
private BuildReport _buildReport;
2025-02-28 16:11:01 +08:00
private string _reportFilePath;
private List<ITableData> _sourceDatas;
2025-01-09 11:31:04 +08:00
/// <summary>
/// 初始化页面
/// </summary>
public void InitViewer()
{
// 加载布局文件
_visualAsset = UxmlLoader.LoadWindowUXML<ReporterAssetListViewer>();
if (_visualAsset == null)
return;
_root = _visualAsset.CloneTree();
_root.style.flexGrow = 1f;
// 资源列表
2025-04-01 21:12:28 +08:00
_assetTableView = _root.Q<TableViewer>("TopTableView");
2025-02-28 16:11:01 +08:00
_assetTableView.SelectionChangedEvent = OnAssetTableViewSelectionChanged;
_assetTableView.ClickTableDataEvent = OnClickAssetTableView;
CreateAssetTableViewColumns();
2025-01-09 11:31:04 +08:00
// 依赖列表
2025-04-01 21:12:28 +08:00
_dependTableView = _root.Q<TableViewer>("BottomTableView");
2025-02-28 16:11:01 +08:00
_dependTableView.ClickTableDataEvent = OnClickBundleTableView;
CreateDependTableViewColumns();
2025-01-09 11:31:04 +08:00
2025-04-01 21:12:28 +08:00
// 面板分屏
2025-02-28 16:11:01 +08:00
var topGroup = _root.Q<VisualElement>("TopGroup");
var bottomGroup = _root.Q<VisualElement>("BottomGroup");
topGroup.style.minHeight = 100;
bottomGroup.style.minHeight = 100f;
2025-04-01 21:12:28 +08:00
UIElementsTools.SplitVerticalPanel(_root, topGroup, bottomGroup);
2025-01-09 11:31:04 +08:00
}
2025-02-28 16:11:01 +08:00
private void CreateAssetTableViewColumns()
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
// AssetPath
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
var columnStyle = new ColumnStyle(600, 500, 1000);
columnStyle.Stretchable = true;
columnStyle.Searchable = true;
columnStyle.Sortable = true;
columnStyle.Counter = true;
var column = new TableColumn("AssetPath", "Asset Path", columnStyle);
column.MakeCell = () =>
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
var label = new Label();
label.style.unityTextAlign = TextAnchor.MiddleLeft;
return label;
};
column.BindCell = (VisualElement element, ITableData data, ITableCell cell) =>
{
var infoLabel = element as Label;
infoLabel.text = (string)cell.GetDisplayObject();
};
_assetTableView.AddColumn(column);
2025-01-09 11:31:04 +08:00
}
2025-02-28 16:11:01 +08:00
//MainBundle
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
var columnStyle = new ColumnStyle(600, 500, 1000);
columnStyle.Stretchable = true;
columnStyle.Searchable = true;
columnStyle.Sortable = true;
var column = new TableColumn("MainBundle", "Main Bundle", columnStyle);
column.MakeCell = () =>
{
var label = new Label();
label.style.unityTextAlign = TextAnchor.MiddleLeft;
return label;
};
column.BindCell = (VisualElement element, ITableData data, ITableCell cell) =>
{
var infoLabel = element as Label;
infoLabel.text = (string)cell.GetDisplayObject();
};
_assetTableView.AddColumn(column);
2025-01-09 11:31:04 +08:00
}
}
2025-02-28 16:11:01 +08:00
private void CreateDependTableViewColumns()
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
// DependBundles
{
var columnStyle = new ColumnStyle(600, 500, 1000);
columnStyle.Stretchable = true;
columnStyle.Searchable = true;
columnStyle.Sortable = true;
columnStyle.Counter = true;
var column = new TableColumn("DependBundles", "Depend Bundles", columnStyle);
column.MakeCell = () =>
{
var label = new Label();
label.style.unityTextAlign = TextAnchor.MiddleLeft;
return label;
};
column.BindCell = (VisualElement element, ITableData data, ITableCell cell) =>
{
var infoLabel = element as Label;
infoLabel.text = (string)cell.GetDisplayObject();
};
_dependTableView.AddColumn(column);
}
2025-01-09 11:31:04 +08:00
2025-02-28 16:11:01 +08:00
// FileSize
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
var columnStyle = new ColumnStyle(100);
columnStyle.Stretchable = false;
columnStyle.Searchable = false;
columnStyle.Sortable = true;
var column = new TableColumn("FileSize", "File Size", columnStyle);
column.MakeCell = () =>
{
var label = new Label();
label.style.unityTextAlign = TextAnchor.MiddleLeft;
return label;
};
column.BindCell = (VisualElement element, ITableData data, ITableCell cell) =>
{
var infoLabel = element as Label;
long fileSize = (long)cell.CellValue;
infoLabel.text = EditorUtility.FormatBytes(fileSize);
};
_dependTableView.AddColumn(column);
2025-01-09 11:31:04 +08:00
}
2025-02-28 16:11:01 +08:00
// FileHash
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
var columnStyle = new ColumnStyle(250);
columnStyle.Stretchable = false;
columnStyle.Searchable = false;
columnStyle.Sortable = false;
var column = new TableColumn("FileHash", "File Hash", columnStyle);
column.MakeCell = () =>
{
var label = new Label();
label.style.unityTextAlign = TextAnchor.MiddleLeft;
return label;
};
column.BindCell = (VisualElement element, ITableData data, ITableCell cell) =>
{
var infoLabel = element as Label;
infoLabel.text = (string)cell.GetDisplayObject();
};
_dependTableView.AddColumn(column);
2025-01-09 11:31:04 +08:00
}
2025-02-28 16:11:01 +08:00
}
/// <summary>
/// 填充页面数据
/// </summary>
public void FillViewData(BuildReport buildReport, string reprotFilePath)
{
_buildReport = buildReport;
_reportFilePath = reprotFilePath;
// 清空旧数据
_assetTableView.ClearAll(false, true);
_dependTableView.ClearAll(false, true);
// 填充数据源
_sourceDatas = new List<ITableData>(_buildReport.AssetInfos.Count);
foreach (var assetInfo in _buildReport.AssetInfos)
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
var rowData = new AssetTableData();
rowData.AssetInfo = assetInfo;
rowData.AddAssetPathCell("AssetPath", assetInfo.AssetPath);
rowData.AddStringValueCell("MainBundle", assetInfo.MainBundleName);
_sourceDatas.Add(rowData);
2025-01-09 11:31:04 +08:00
}
2025-02-28 16:11:01 +08:00
_assetTableView.itemsSource = _sourceDatas;
// 重建视图
RebuildView(null);
}
/// <summary>
/// 重建视图
/// </summary>
public void RebuildView(string searchKeyWord)
{
// 搜索匹配
DefaultSearchSystem.Search(_sourceDatas, searchKeyWord);
// 重建视图
_assetTableView.RebuildView();
2025-01-09 11:31:04 +08:00
}
/// <summary>
/// 挂接到父类页面上
/// </summary>
public void AttachParent(VisualElement parent)
{
parent.Add(_root);
}
/// <summary>
/// 从父类页面脱离开
/// </summary>
public void DetachParent()
{
_root.RemoveFromHierarchy();
}
2025-02-28 16:11:01 +08:00
private void OnAssetTableViewSelectionChanged(ITableData data)
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
var assetTableData = data as AssetTableData;
ReportAssetInfo assetInfo = assetTableData.AssetInfo;
2025-01-09 11:31:04 +08:00
2025-02-28 16:11:01 +08:00
// 填充依赖数据
2025-05-13 10:40:30 +08:00
var sourceDatas = new List<ITableData>(assetInfo.DependBundles.Count);
foreach (string dependBundleName in assetInfo.DependBundles)
2025-01-09 11:31:04 +08:00
{
var dependBundle = _buildReport.GetBundleInfo(dependBundleName);
2025-02-28 16:11:01 +08:00
var rowData = new DependTableData();
rowData.BundleInfo = dependBundle;
rowData.AddStringValueCell("DependBundles", dependBundle.BundleName);
rowData.AddLongValueCell("FileSize", dependBundle.FileSize);
rowData.AddStringValueCell("FileHash", dependBundle.FileHash);
sourceDatas.Add(rowData);
2025-01-09 11:31:04 +08:00
}
2025-02-28 16:11:01 +08:00
_dependTableView.itemsSource = sourceDatas;
_dependTableView.RebuildView();
2025-01-09 11:31:04 +08:00
}
2025-02-28 16:11:01 +08:00
private void OnClickAssetTableView(PointerDownEvent evt, ITableData data)
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
// 鼠标双击后检视
if (evt.clickCount != 2)
return;
2025-01-09 11:31:04 +08:00
2025-02-28 16:11:01 +08:00
foreach (var cell in data.Cells)
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
if (cell is AssetPathCell assetPathCell)
{
if (assetPathCell.PingAssetObject())
break;
}
2025-01-09 11:31:04 +08:00
}
}
2025-02-28 16:11:01 +08:00
private void OnClickBundleTableView(PointerDownEvent evt, ITableData data)
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
// 鼠标双击后检视
if (evt.clickCount != 2)
return;
2025-01-09 11:31:04 +08:00
2025-02-28 16:11:01 +08:00
var dependTableData = data as DependTableData;
if (dependTableData.BundleInfo.Encrypted)
return;
2025-01-09 11:31:04 +08:00
2025-02-28 16:11:01 +08:00
if (_buildReport.Summary.BuildBundleType == (int)EBuildBundleType.AssetBundle)
{
string rootDirectory = Path.GetDirectoryName(_reportFilePath);
string filePath = $"{rootDirectory}/{dependTableData.BundleInfo.FileName}";
if (File.Exists(filePath))
Selection.activeObject = AssetBundleRecorder.GetAssetBundle(filePath);
else
Selection.activeObject = null;
}
2025-01-09 11:31:04 +08:00
}
}
}
#endif