diff --git a/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/AssetPathCell.cs b/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/AssetPathCell.cs index 68d929be..0c23b737 100644 --- a/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/AssetPathCell.cs +++ b/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/AssetPathCell.cs @@ -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) { } + + /// + /// 检视资源对象 + /// Ping an asset object in the Scene like clicking it in an inspector. + /// + public bool PingAssetObject() + { + var assetPath = StringValue; + var assetGUID = AssetDatabase.AssetPathToGUID(assetPath); + if (string.IsNullOrEmpty(assetGUID)) + return false; + + UnityEngine.Object asset = AssetDatabase.LoadAssetAtPath(assetPath); + if (asset == null) + return false; + + Selection.activeObject = asset; + EditorGUIUtility.PingObject(asset); + return true; + } } } #endif \ No newline at end of file diff --git a/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/IntegerValueCell.cs b/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/IntegerValueCell.cs index 2dd9fd86..72565aa6 100644 --- a/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/IntegerValueCell.cs +++ b/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/IntegerValueCell.cs @@ -15,9 +15,10 @@ namespace YooAsset.Editor } } - public IntegerValueCell(string headerTitle) + public IntegerValueCell(string headerTitle, object cellValue) { HeaderTitle = headerTitle; + CellValue = cellValue; } public object GetDisplayObject() { diff --git a/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/SingleValueCell.cs b/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/SingleValueCell.cs index 94508bc6..222e2cc9 100644 --- a/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/SingleValueCell.cs +++ b/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/SingleValueCell.cs @@ -15,9 +15,10 @@ namespace YooAsset.Editor } } - public SingleValueCell(string headerTitle) + public SingleValueCell(string headerTitle, object cellValue) { HeaderTitle = headerTitle; + CellValue = cellValue; } public object GetDisplayObject() { diff --git a/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/StringValueCell.cs b/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/StringValueCell.cs index eb6d4fbd..bb4dfc94 100644 --- a/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/StringValueCell.cs +++ b/Assets/YooAsset/Editor/UIElements/TableView/DefaultCells/StringValueCell.cs @@ -15,9 +15,10 @@ namespace YooAsset.Editor } } - public StringValueCell(string headerTitle) + public StringValueCell(string headerTitle, object cellValue) { HeaderTitle = headerTitle; + CellValue = cellValue; } public object GetDisplayObject() { diff --git a/Assets/YooAsset/Editor/UIElements/TableView/DefaultTableData.meta b/Assets/YooAsset/Editor/UIElements/TableView/DefaultTableData.meta new file mode 100644 index 00000000..ede3a8e7 --- /dev/null +++ b/Assets/YooAsset/Editor/UIElements/TableView/DefaultTableData.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: f347ad29484952842a01a1489fbbef5d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Editor/UIElements/TableView/DefaultTableData/DefaultTableData.cs b/Assets/YooAsset/Editor/UIElements/TableView/DefaultTableData/DefaultTableData.cs new file mode 100644 index 00000000..f2c14f32 --- /dev/null +++ b/Assets/YooAsset/Editor/UIElements/TableView/DefaultTableData/DefaultTableData.cs @@ -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 + { + /// + /// 是否可见 + /// + public bool Visible { set; get; } = true; + + /// + /// 单元格集合 + /// + public IList Cells { set; get; } = new List(); + + #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 \ No newline at end of file diff --git a/Assets/YooAsset/Editor/UIElements/TableView/DefaultTableData/DefaultTableData.cs.meta b/Assets/YooAsset/Editor/UIElements/TableView/DefaultTableData/DefaultTableData.cs.meta new file mode 100644 index 00000000..573ef636 --- /dev/null +++ b/Assets/YooAsset/Editor/UIElements/TableView/DefaultTableData/DefaultTableData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b93bf9f1eb7024c44a1c84b299dc9622 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Editor/UIElements/TableView/TableView.cs b/Assets/YooAsset/Editor/UIElements/TableView/TableView.cs index 96f9bdda..d44ac79f 100644 --- a/Assets/YooAsset/Editor/UIElements/TableView/TableView.cs +++ b/Assets/YooAsset/Editor/UIElements/TableView/TableView.cs @@ -15,6 +15,13 @@ namespace YooAsset.Editor /// public class TableView : VisualElement { + /// + /// Instantiates a TableView using data from a UXML file. + /// + public new class UxmlFactory : UxmlFactory + { + } + private readonly Toolbar _toolbar; private readonly ListView _listView; @@ -55,6 +62,11 @@ namespace YooAsset.Editor /// public Action ClickTableHeadEvent; + /// + /// 单元视图变化事件 + /// + public Action SelectionChangedEvent; + public TableView() { @@ -65,6 +77,15 @@ namespace YooAsset.Editor _listView.makeItem = MakeListViewElement; _listView.bindItem = BindListViewElement; _listView.RegisterCallback(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 itemsSource) + /// + /// 清空所有数据 + /// + 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 items) + { + foreach (var item in items) + { + var tableData = item as ITableData; + SelectionChangedEvent?.Invoke(tableData); + break; + } + } + + private bool CheckItemsSource(List 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();