update UIElements

pull/455/head
何冠峰 2025-01-20 10:07:16 +08:00
parent 793a57647d
commit 5e2fc84ebf
8 changed files with 174 additions and 28 deletions

View File

@ -1,12 +1,33 @@
#if UNITY_2019_4_OR_NEWER
using UnityEditor;
namespace YooAsset.Editor
{
public class AssetPathCell : StringValueCell
{
public AssetPathCell(string headerTitle) : base(headerTitle)
public AssetPathCell(string headerTitle, object cellValue) : base(headerTitle, cellValue)
{
}
/// <summary>
/// 检视资源对象
/// Ping an asset object in the Scene like clicking it in an inspector.
/// </summary>
public bool PingAssetObject()
{
var assetPath = StringValue;
var assetGUID = AssetDatabase.AssetPathToGUID(assetPath);
if (string.IsNullOrEmpty(assetGUID))
return false;
UnityEngine.Object asset = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetPath);
if (asset == null)
return false;
Selection.activeObject = asset;
EditorGUIUtility.PingObject(asset);
return true;
}
}
}
#endif

View File

@ -15,9 +15,10 @@ namespace YooAsset.Editor
}
}
public IntegerValueCell(string headerTitle)
public IntegerValueCell(string headerTitle, object cellValue)
{
HeaderTitle = headerTitle;
CellValue = cellValue;
}
public object GetDisplayObject()
{

View File

@ -15,9 +15,10 @@ namespace YooAsset.Editor
}
}
public SingleValueCell(string headerTitle)
public SingleValueCell(string headerTitle, object cellValue)
{
HeaderTitle = headerTitle;
CellValue = cellValue;
}
public object GetDisplayObject()
{

View File

@ -15,9 +15,10 @@ namespace YooAsset.Editor
}
}
public StringValueCell(string headerTitle)
public StringValueCell(string headerTitle, object cellValue)
{
HeaderTitle = headerTitle;
CellValue = cellValue;
}
public object GetDisplayObject()
{

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f347ad29484952842a01a1489fbbef5d
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,50 @@
#if UNITY_2019_4_OR_NEWER
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace YooAsset.Editor
{
public class DefaultTableData : ITableData
{
/// <summary>
/// 是否可见
/// </summary>
public bool Visible { set; get; } = true;
/// <summary>
/// 单元格集合
/// </summary>
public IList<ITableCell> Cells { set; get; } = new List<ITableCell>();
#region 添加默认的单元格数据
public void AddButtonCell()
{
var cell = new ButtonCell();
Cells.Add(cell);
}
public void AddAssetPathCell(string headerTitle, string path)
{
var cell = new AssetPathCell(headerTitle, path);
Cells.Add(cell);
}
public void AddStringValueCell(string headerTitle, string value)
{
var cell = new StringValueCell(headerTitle, value);
Cells.Add(cell);
}
public void AddIntegerValueCell(string headerTitle, long value)
{
var cell = new IntegerValueCell(headerTitle, value);
Cells.Add(cell);
}
public void AddSingleValueCell(string headerTitle, double value)
{
var cell = new SingleValueCell(headerTitle, value);
Cells.Add(cell);
}
#endregion
}
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b93bf9f1eb7024c44a1c84b299dc9622
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -15,6 +15,13 @@ namespace YooAsset.Editor
/// </summary>
public class TableView : VisualElement
{
/// <summary>
/// Instantiates a TableView using data from a UXML file.
/// </summary>
public new class UxmlFactory : UxmlFactory<TableView, UxmlTraits>
{
}
private readonly Toolbar _toolbar;
private readonly ListView _listView;
@ -55,6 +62,11 @@ namespace YooAsset.Editor
/// </summary>
public Action<TableColumn> ClickTableHeadEvent;
/// <summary>
/// 单元视图变化事件
/// </summary>
public Action<ITableData> SelectionChangedEvent;
public TableView()
{
@ -65,6 +77,15 @@ namespace YooAsset.Editor
_listView.makeItem = MakeListViewElement;
_listView.bindItem = BindListViewElement;
_listView.RegisterCallback<PointerDownEvent>(OnClickListItem);
#if UNITY_2022_3_OR_NEWER
_listView.selectionChanged += OnSelectionChanged;
#elif UNITY_2020_1_OR_NEWER
_listView.onSelectionChange += OnSelectionChanged;
#else
_listView.onSelectionChanged += OnSelectionChanged;
#endif
this.Add(_listView);
}
@ -121,37 +142,28 @@ namespace YooAsset.Editor
_listView.Rebuild();
}
private bool CheckItemsSource(List<ITableData> itemsSource)
/// <summary>
/// 清空所有数据
/// </summary>
public void ClearAll(bool clearColumns, bool clearSource)
{
if (itemsSource == null || itemsSource.Count == 0)
if (clearColumns)
{
Debug.LogWarning("Items source is null or empty !");
return false;
_columns.Clear();
_toolbar.Clear();
}
int cellCount = itemsSource[0].Cells.Count;
for (int i = 0; i < itemsSource.Count; i++)
if (clearSource)
{
var tableData = itemsSource[i];
if (tableData == null)
{
Debug.LogWarning($"Items source has null instance !");
return false;
}
if (tableData.Cells == null || tableData.Cells.Count == 0)
{
Debug.LogWarning($"Items source data has empty cells !");
return false;
}
if (tableData.Cells.Count != cellCount)
{
Debug.LogWarning($"Items source data has inconsisten cells count ! Item index {i}");
return false;
}
if (_itemsSource != null)
_itemsSource.Clear();
if (_sortingDatas != null)
_sortingDatas.Clear();
_listView.Clear();
_listView.ClearSelection();
}
return true;
}
private void OnClickListItem(PointerDownEvent evt)
{
var selectData = _listView.selectedItem as ITableData;
@ -204,6 +216,47 @@ namespace YooAsset.Editor
// 刷新数据表
RebuildView();
}
private void OnSelectionChanged(IEnumerable<object> items)
{
foreach (var item in items)
{
var tableData = item as ITableData;
SelectionChangedEvent?.Invoke(tableData);
break;
}
}
private bool CheckItemsSource(List<ITableData> itemsSource)
{
if (itemsSource == null)
return false;
if (itemsSource.Count > 0)
{
int cellCount = itemsSource[0].Cells.Count;
for (int i = 0; i < itemsSource.Count; i++)
{
var tableData = itemsSource[i];
if (tableData == null)
{
Debug.LogWarning($"Items source has null instance !");
return false;
}
if (tableData.Cells == null || tableData.Cells.Count == 0)
{
Debug.LogWarning($"Items source data has empty cells !");
return false;
}
if (tableData.Cells.Count != cellCount)
{
Debug.LogWarning($"Items source data has inconsisten cells count ! Item index {i}");
return false;
}
}
}
return true;
}
private VisualElement MakeListViewElement()
{
VisualElement listViewElement = new VisualElement();