修复表格的刷新工作流程,优化表格的操作,优化表格的序列化工作。

This commit is contained in:
2026-06-27 22:07:35 +08:00
parent 08df172165
commit 84b60c6835
6 changed files with 1083 additions and 919 deletions
+26 -79
View File
@@ -8,7 +8,7 @@ namespace XericUIEditor.XTable
{ {
/// <summary> /// <summary>
/// XericUIActionTable 的 Inspector 编辑器—— /// XericUIActionTable 的 Inspector 编辑器——
/// 折叠分组:[样式设定] [尺寸设定] [编辑测试] [数据浏览]。 /// 折叠分组:[样式设定] [尺寸设定] [调试] [数据浏览]。
/// 使用 EditorGUILayout.Foldout 避免与数组 PropertyField 内部的折叠冲突。 /// 使用 EditorGUILayout.Foldout 避免与数组 PropertyField 内部的折叠冲突。
/// </summary> /// </summary>
[CustomEditor(typeof(XericUIActionTable))] [CustomEditor(typeof(XericUIActionTable))]
@@ -24,23 +24,11 @@ namespace XericUIEditor.XTable
// 折叠状态 // 折叠状态
private static bool s_StyleFoldout = true; private static bool s_StyleFoldout = true;
private static bool s_SizeFoldout = true; private static bool s_SizeFoldout = true;
private static bool s_EditTestFoldout; private static bool s_DebugFoldout;
private static bool s_DataBrowseFoldout; private static bool s_DataBrowseFoldout;
// 编辑测试参数 // 调试参数
private int m_BatchRowCount = 10; private bool m_ShowChildren;
private int m_BatchColCount = 10;
private float m_BatchCellHeight = 40f;
private float m_BatchCellWidth = 100f;
private int m_MergeStartRow;
private int m_MergeStartCol;
private int m_MergeRowSpan = 2;
private int m_MergeColSpan = 2;
private int m_EditRow;
private int m_EditCol;
private string m_EditText = "";
private void OnEnable() private void OnEnable()
{ {
@@ -81,55 +69,35 @@ namespace XericUIEditor.XTable
EditorGUI.indentLevel--; EditorGUI.indentLevel--;
} }
// ===== 编辑测试 ===== // ===== 调试 =====
EditorGUILayout.Space(4); EditorGUILayout.Space(4);
s_EditTestFoldout = EditorGUILayout.Foldout(s_EditTestFoldout, "编辑测试", true); s_DebugFoldout = EditorGUILayout.Foldout(s_DebugFoldout, "调试", true);
if (s_EditTestFoldout) if (s_DebugFoldout)
{ {
EditorGUI.indentLevel++; EditorGUI.indentLevel++;
EditorGUILayout.LabelField("批量生成尺寸", EditorStyles.miniBoldLabel); if (GUILayout.Button("重新生成表格", GUILayout.Height(26)))
m_BatchRowCount = EditorGUILayout.IntField("行数", m_BatchRowCount); {
m_BatchColCount = EditorGUILayout.IntField("列数", m_BatchColCount); m_Table.FullRebuild();
m_BatchCellHeight = EditorGUILayout.FloatField("单元格高度", m_BatchCellHeight); m_Table.SetChildrenVisible(!m_ShowChildren);
m_BatchCellWidth = EditorGUILayout.FloatField("单元格宽度", m_BatchCellWidth); }
if (GUILayout.Button("应用批量尺寸", GUILayout.Height(22)))
ApplyBatchRowColSizes();
EditorGUILayout.Space(6); EditorGUILayout.Space(4);
EditorGUILayout.LabelField("合并单元格", EditorStyles.miniBoldLabel); bool newShow = EditorGUILayout.Toggle("显示子项", m_ShowChildren);
m_MergeStartRow = EditorGUILayout.IntField("起始行", m_MergeStartRow); if (newShow != m_ShowChildren)
m_MergeStartCol = EditorGUILayout.IntField("起始列", m_MergeStartCol); {
m_MergeRowSpan = EditorGUILayout.IntField("行跨度", m_MergeRowSpan); m_ShowChildren = newShow;
m_MergeColSpan = EditorGUILayout.IntField("列跨度", m_MergeColSpan); m_Table.SetChildrenVisible(m_ShowChildren);
EditorGUILayout.BeginHorizontal(); }
if (GUILayout.Button("合并", GUILayout.Height(22)))
EditorApplication.delayCall += () => m_Table.MergeCells(m_MergeStartRow, m_MergeStartCol, m_MergeRowSpan, m_MergeColSpan);
if (GUILayout.Button("取消合并", GUILayout.Height(22)))
EditorApplication.delayCall += () => m_Table.UnmergeCells(m_MergeStartRow, m_MergeStartCol);
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space(6); if (m_ShowChildren)
{
EditorGUILayout.LabelField("单元格编辑", EditorStyles.miniBoldLabel); EditorGUILayout.HelpBox(
m_EditRow = EditorGUILayout.IntField("行", m_EditRow); "子项已显示:所有动态生成的对象将在 Hierarchy 中可见(仍不保存到场景)。\n" +
m_EditCol = EditorGUILayout.IntField("列", m_EditCol); "用于排查表格元素堆积或渲染异常问题。",
MessageType.Info);
var existing = m_Table.GetCellData(m_EditRow, m_EditCol); }
if (existing != null && !string.IsNullOrEmpty(existing.Text) && string.IsNullOrEmpty(m_EditText))
m_EditText = existing.Text;
m_EditText = EditorGUILayout.TextField("文本内容", m_EditText);
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("应用文本", GUILayout.Height(22)))
EditorApplication.delayCall += () => m_Table.SetCellText(m_EditRow, m_EditCol, m_EditText);
if (GUILayout.Button("选中此格", GUILayout.Height(22)))
EditorApplication.delayCall += () => m_Table.SelectCell(m_EditRow, m_EditCol);
if (GUILayout.Button("清除选中", GUILayout.Height(22)))
EditorApplication.delayCall += () => m_Table.ClearSelection();
EditorGUILayout.EndHorizontal();
EditorGUI.indentLevel--; EditorGUI.indentLevel--;
} }
@@ -166,27 +134,6 @@ namespace XericUIEditor.XTable
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
} }
private void ApplyBatchRowColSizes()
{
serializedObject.Update();
if (m_RowHeightsProp != null)
{
m_RowHeightsProp.arraySize = m_BatchRowCount;
for (int i = 0; i < m_BatchRowCount; i++)
m_RowHeightsProp.GetArrayElementAtIndex(i).floatValue = m_BatchCellHeight;
}
if (m_ColWidthsProp != null)
{
m_ColWidthsProp.arraySize = m_BatchColCount;
for (int i = 0; i < m_BatchColCount; i++)
m_ColWidthsProp.GetArrayElementAtIndex(i).floatValue = m_BatchCellWidth;
}
serializedObject.ApplyModifiedProperties();
}
#region Hierarchy 创建菜单 #region Hierarchy 创建菜单
[MenuItem("GameObject/Xeric UI/Table/Xeric UI Action Table", false, 60)] [MenuItem("GameObject/Xeric UI/Table/Xeric UI Action Table", false, 60)]
+77 -19
View File
@@ -2,6 +2,7 @@ using UnityEditor;
using UnityEngine; using UnityEngine;
using XericLibrary.Runtime.SuperStyleSheet;
using XericUI.XTable.Mapping; using XericUI.XTable.Mapping;
using XericUI.XTable.Rendering.Component; using XericUI.XTable.Rendering.Component;
@@ -137,10 +138,23 @@ namespace XericUIEditor.XTable
// 底部内容输入框获得焦点:种子为当前单元格内容 // 底部内容输入框获得焦点:种子为当前单元格内容
else if (newFocus == "XTableSceneEditInput" && s_ActiveTable != null) else if (newFocus == "XTableSceneEditInput" && s_ActiveTable != null)
{ {
int sr = s_ActiveTable.SelectedRow; SyncEditText(s_ActiveTable);
int sc = s_ActiveTable.SelectedCol; }
}
/// <summary>将 s_EditText 同步为当前选中单元格的内容(选中变更时调用)</summary>
private static void SyncEditText(XericUIActionTable table)
{
int sr = table.SelectedRow;
int sc = table.SelectedCol;
if (sr >= 0 && sc >= 0) if (sr >= 0 && sc >= 0)
s_EditText = s_ActiveTable.GetCellData(sr, sc).Text ?? ""; {
var data = table.GetCellData(sr, sc);
s_EditText = (data != null) ? (data.Text ?? "") : "";
}
else
{
s_EditText = "";
} }
} }
@@ -338,6 +352,7 @@ namespace XericUIEditor.XTable
} }
table.RefreshView(); table.RefreshView();
SyncEditText(table);
Event.current.Use(); Event.current.Use();
SceneView.RepaintAll(); SceneView.RepaintAll();
} }
@@ -412,6 +427,7 @@ namespace XericUIEditor.XTable
} }
table.RefreshView(); table.RefreshView();
SyncEditText(table);
Event.current.Use(); Event.current.Use();
SceneView.RepaintAll(); SceneView.RepaintAll();
} }
@@ -719,7 +735,12 @@ namespace XericUIEditor.XTable
private static void DrawInputBar(XericUIActionTable table, float x, float y) private static void DrawInputBar(XericUIActionTable table, float x, float y)
{ {
Rect barRect = new Rect(x, y, 500, 24); const float barW = 450;
const float barH = 52;
const float rowH = 22;
const float pad = 2;
Rect barRect = new Rect(x, y, barW, barH);
EditorGUI.DrawRect(barRect, s_InputBg); EditorGUI.DrawRect(barRect, s_InputBg);
if (table.SelectedRow >= 0 && table.SelectedCol >= 0) if (table.SelectedRow >= 0 && table.SelectedCol >= 0)
@@ -730,17 +751,26 @@ namespace XericUIEditor.XTable
normal = { textColor = s_LabelText }, normal = { textColor = s_LabelText },
alignment = TextAnchor.MiddleCenter alignment = TextAnchor.MiddleCenter
}; };
string cellLabel = table.GetSelectionText();
GUI.Label(new Rect(barRect.x + 4, barRect.y, 75, barRect.height),
cellLabel, labelStyle);
var inputStyle = new GUIStyle(GUI.skin.textField) var inputStyle = new GUIStyle(GUI.skin.textField)
{ {
fontSize = 12, fontSize = 12,
margin = new RectOffset(2, 2, 2, 2) margin = new RectOffset(1, 1, 1, 1)
}; };
Rect inputRect = new Rect(barRect.x + 85, barRect.y + 2, 200, barRect.height - 4); var btnStyle = new GUIStyle(GUI.skin.button)
{
fontSize = 11,
margin = new RectOffset(1, 1, 1, 1),
padding = new RectOffset(4, 4, 2, 2)
};
// Row 1: 坐标标签 | 输入框 | 应用按钮
float btnW = 44;
float row1Y = barRect.y + pad;
float row2Y = barRect.y + rowH + pad + 4;
string cellLabel = table.GetSelectionText();
GUI.Label(new Rect(barRect.x + 4, row1Y, 60, rowH), cellLabel, labelStyle);
Rect inputRect = new Rect(barRect.x + 68, row1Y, barW - 68 - btnW - pad * 2, rowH);
// 回车提交(仅在 CellEditing 状态下) // 回车提交(仅在 CellEditing 状态下)
if (s_State == EditorState.CellEditing && if (s_State == EditorState.CellEditing &&
@@ -754,18 +784,40 @@ namespace XericUIEditor.XTable
GUI.SetNextControlName("XTableSceneEditInput"); GUI.SetNextControlName("XTableSceneEditInput");
s_EditText = GUI.TextField(inputRect, s_EditText, inputStyle); s_EditText = GUI.TextField(inputRect, s_EditText, inputStyle);
var btnStyle = new GUIStyle(GUI.skin.button) if (GUI.Button(new Rect(barRect.x + barW - btnW - pad, row1Y, btnW, rowH),
{
fontSize = 11,
margin = new RectOffset(2, 2, 2, 2),
padding = new RectOffset(4, 4, 2, 2)
};
if (GUI.Button(new Rect(barRect.x + 370, barRect.y + 2, 50, barRect.height - 4),
"应用", btnStyle)) "应用", btnStyle))
{ {
GUI.FocusControl(null); GUI.FocusControl(null);
ApplyCellEdit(table); ApplyCellEdit(table);
} }
// Row 2: 样式命名空间下拉框
var nsLabelStyle = new GUIStyle(GUI.skin.label)
{
fontSize = 10,
normal = { textColor = new Color(0.6f, 0.6f, 0.6f) },
alignment = TextAnchor.MiddleRight
};
GUI.Label(new Rect(barRect.x + 4, row2Y, 36, rowH), "样式:", nsLabelStyle);
// 获取当前命名空间
string currentNS = table.GetCellStyleNamespace(
table.SelectedRow, table.SelectedCol) ?? "";
// 构建下拉选项(StyleManager.GetAvailableNamespaces 已保证 "default" 在首位)
var namespaces = StyleManager.GetAvailableNamespaces();
int currentIdx = namespaces.IndexOf(currentNS);
if (currentIdx < 0) currentIdx = 0;
Rect dropRect = new Rect(barRect.x + 44, row2Y, barW - 44 - pad, rowH);
int newIdx = UnityEditor.EditorGUI.Popup(dropRect, currentIdx,
namespaces.ToArray(), UnityEditor.EditorStyles.popup);
if (newIdx != currentIdx && newIdx >= 0 && newIdx < namespaces.Count)
{
table.SetCellStyleNamespace(table.SelectedRow, table.SelectedCol,
namespaces[newIdx]);
}
} }
else else
{ {
@@ -775,7 +827,8 @@ namespace XericUIEditor.XTable
normal = { textColor = new Color(0.5f, 0.5f, 0.5f) }, normal = { textColor = new Color(0.5f, 0.5f, 0.5f) },
alignment = TextAnchor.MiddleCenter alignment = TextAnchor.MiddleCenter
}; };
GUI.Label(barRect, "点击左侧行号或上方列名选中单元格 | Shift+方向键多选", hintStyle); GUI.Label(new Rect(barRect.x, barRect.y, barW, barH / 2),
"点击左侧行号或上方列名选中单元格 | Shift+方向键多选", hintStyle);
} }
} }
@@ -823,6 +876,7 @@ namespace XericUIEditor.XTable
} }
table.RefreshView(); table.RefreshView();
SyncEditText(table);
e.Use(); e.Use();
SceneView.RepaintAll(); SceneView.RepaintAll();
break; break;
@@ -835,6 +889,7 @@ namespace XericUIEditor.XTable
table.ExtendMultiSelect(dragRow, dragCol); table.ExtendMultiSelect(dragRow, dragCol);
table.RefreshView(); table.RefreshView();
SyncEditText(table);
e.Use(); e.Use();
SceneView.RepaintAll(); SceneView.RepaintAll();
break; break;
@@ -870,6 +925,7 @@ namespace XericUIEditor.XTable
table.ClearSelection(); table.ClearSelection();
table.RefreshView(); table.RefreshView();
s_EditText = ""; s_EditText = "";
SyncEditText(table);
e.Use(); e.Use();
SceneView.RepaintAll(); SceneView.RepaintAll();
return; return;
@@ -899,6 +955,7 @@ namespace XericUIEditor.XTable
table.SelectCell(row, col); table.SelectCell(row, col);
table.RefreshView(); table.RefreshView();
SyncEditText(table);
e.Use(); e.Use();
SceneView.RepaintAll(); SceneView.RepaintAll();
return; return;
@@ -934,6 +991,7 @@ namespace XericUIEditor.XTable
} }
table.RefreshView(); table.RefreshView();
SyncEditText(table);
e.Use(); e.Use();
SceneView.RepaintAll(); SceneView.RepaintAll();
} }
+35 -5
View File
@@ -20,16 +20,23 @@ namespace XericUI.XTable.Core
#region 属性 #region 属性
/// <summary>块内列数(默认 <see cref="DefaultBlockSize"/>)</summary> /// <summary>块内列数(默认 <see cref="DefaultBlockSize"/>)</summary>
public int BlockSizeX { get; private set; } [field: SerializeField] public int BlockSizeX { get; private set; }
/// <summary>块内行数(默认 <see cref="DefaultBlockSize"/>)</summary> /// <summary>块内行数(默认 <see cref="DefaultBlockSize"/>)</summary>
public int BlockSizeY { get; private set; } [field: SerializeField] public int BlockSizeY { get; private set; }
/// <summary> /// <summary>
/// 块字典——key = Z 曲线索引(ulong),value = 数据块。 /// 块字典——key = Z 曲线索引(ulong),value = 数据块。
/// 具有哈希表性质,按需创建,键不连续(离散存储)。 /// 具有哈希表性质,按需创建,键不连续(离散存储)。
/// 注意:Dictionary 不被 Unity 序列化器原生支持,通过 <see cref="m_BlockKeys"/> / <see cref="m_BlockValues"/> 代理序列化。
/// </summary> /// </summary>
public Dictionary<ulong, XTableBlock> BlockMap { get; private set; } [NonSerialized] public Dictionary<ulong, XTableBlock> BlockMap;
// ── Dictionary 序列化代理 ──
// Unity 无法直接序列化 Dictionary<ulong, XTableBlock>,
// 使用两个平行列表在 OnBeforeSerialize/OnAfterDeserialize 中转换。
[SerializeField] private List<ulong> m_BlockKeys = new List<ulong>();
[SerializeField] private List<XTableBlock> m_BlockValues = new List<XTableBlock>();
/// <summary> /// <summary>
/// 获取或创建单元格——如果单元格不存在则自动创建空数据。 /// 获取或创建单元格——如果单元格不存在则自动创建空数据。
@@ -74,13 +81,36 @@ namespace XericUI.XTable.Core
#region 序列化回调 #region 序列化回调
public void OnBeforeSerialize() { } public void OnBeforeSerialize()
{
// Dictionary → 平行列表
m_BlockKeys.Clear();
m_BlockValues.Clear();
if (BlockMap != null)
{
foreach (var kv in BlockMap)
{
m_BlockKeys.Add(kv.Key);
m_BlockValues.Add(kv.Value);
}
}
}
public void OnAfterDeserialize() public void OnAfterDeserialize()
{ {
if (BlockSizeX <= 0) BlockSizeX = DefaultBlockSize; if (BlockSizeX <= 0) BlockSizeX = DefaultBlockSize;
if (BlockSizeY <= 0) BlockSizeY = DefaultBlockSize; if (BlockSizeY <= 0) BlockSizeY = DefaultBlockSize;
if (BlockMap == null) BlockMap = new Dictionary<ulong, XTableBlock>();
// 平行列表 → Dictionary
BlockMap = new Dictionary<ulong, XTableBlock>();
int count = System.Math.Min(
m_BlockKeys?.Count ?? 0,
m_BlockValues?.Count ?? 0);
for (int i = 0; i < count; i++)
{
if (m_BlockValues[i] != null)
BlockMap[m_BlockKeys[i]] = m_BlockValues[i];
}
} }
#endregion #endregion
+41 -3
View File
@@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine;
namespace XericUI.XTable.Core namespace XericUI.XTable.Core
{ {
/// <summary> /// <summary>
@@ -8,7 +10,7 @@ namespace XericUI.XTable.Core
/// 同一类型支持本地块内引用和跨块引用,通过 <see cref="IsCrossBlock"/> 区分。 /// 同一类型支持本地块内引用和跨块引用,通过 <see cref="IsCrossBlock"/> 区分。
/// </summary> /// </summary>
[Serializable] [Serializable]
public class XTableMergeDescriptor public class XTableMergeDescriptor : ISerializationCallbackReceiver
{ {
/// <summary>合并区域在本地块内的起始行</summary> /// <summary>合并区域在本地块内的起始行</summary>
public int LocalStartRow; public int LocalStartRow;
@@ -37,8 +39,12 @@ namespace XericUI.XTable.Core
/// <summary>源单元格在块内的列号(跨块时=源块的列、本地时=本块的列)</summary> /// <summary>源单元格在块内的列号(跨块时=源块的列、本地时=本块的列)</summary>
public int SourceLocalCol; public int SourceLocalCol;
/// <summary>本块内被合并的 (row, col) 集合,查询时快速判断是否需要重定向</summary> /// <summary>本块内被合并的 (row, col) 集合——不参与序列化,通过代理列表同步</summary>
public HashSet<(int row, int col)> MergedCellSet; [NonSerialized] public HashSet<(int row, int col)> MergedCellSet;
// ── HashSet 序列化代理 ──
[SerializeField] private List<int> m_MergedRowList = new List<int>();
[SerializeField] private List<int> m_MergedColList = new List<int>();
public XTableMergeDescriptor() public XTableMergeDescriptor()
{ {
@@ -63,5 +69,37 @@ namespace XericUI.XTable.Core
targetRow = SourceLocalRow; targetRow = SourceLocalRow;
targetCol = SourceLocalCol; targetCol = SourceLocalCol;
} }
#region 序列化回调
public void OnBeforeSerialize()
{
// HashSet<(int,int)> → 两个平行列表
m_MergedRowList.Clear();
m_MergedColList.Clear();
if (MergedCellSet != null)
{
foreach (var (row, col) in MergedCellSet)
{
m_MergedRowList.Add(row);
m_MergedColList.Add(col);
}
}
}
public void OnAfterDeserialize()
{
// 两个平行列表 → HashSet<(int,int)>
MergedCellSet = new HashSet<(int, int)>();
int count = System.Math.Min(
m_MergedRowList?.Count ?? 0,
m_MergedColList?.Count ?? 0);
for (int i = 0; i < count; i++)
{
MergedCellSet.Add((m_MergedRowList[i], m_MergedColList[i]));
}
}
#endregion
} }
} }
@@ -1,12 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
using UnityEngine.UI; using UnityEngine.UI;
using XericUI.Core.Base; using XericUI.Core.Base;
using XericUI.XTable.Core; using XericUI.XTable.Core;
using XericUI.XTable.Mapping; using XericUI.XTable.Mapping;
@@ -85,7 +82,11 @@ namespace XericUI.XTable.Rendering.Component
m_TableData = new XTableData(); m_TableData = new XTableData();
return m_TableData; return m_TableData;
} }
set { m_TableData = value; MarkDirty(TableDirtyType.DataChanged); } set
{
m_TableData = value;
MarkDirty(TableDirtyType.DataChanged);
}
} }
public float[] RowHeights => m_RowHeights; public float[] RowHeights => m_RowHeights;
@@ -120,6 +121,9 @@ namespace XericUI.XTable.Rendering.Component
{ {
base.Awake(); base.Awake();
// 域重载后旧子对象可能残留在场景中(DontSave 不自动销毁),先清空再重建
ClearAllChildren();
if (m_TableData == null) if (m_TableData == null)
m_TableData = new XTableData(); m_TableData = new XTableData();
@@ -229,6 +233,7 @@ namespace XericUI.XTable.Rendering.Component
UnsubscribeEditorUpdate(); UnsubscribeEditorUpdate();
return; return;
} }
if (m_DirtyFlag.IsDirty) if (m_DirtyFlag.IsDirty)
RefreshView(); RefreshView();
} }
@@ -254,6 +259,20 @@ namespace XericUI.XTable.Rendering.Component
public XTableCellData GetCellData(int row, int col) public XTableCellData GetCellData(int row, int col)
=> TableData.GetOrCreateCell(row, col); => TableData.GetOrCreateCell(row, col);
/// <summary>设置单元格的样式命名空间</summary>
public void SetCellStyleNamespace(int row, int col, string styleNamespace)
{
var data = TableData.GetOrCreateCell(row, col);
data.StyleNamespace = styleNamespace;
MarkDirty(TableDirtyType.StyleChanged);
}
/// <summary>获取单元格的样式命名空间</summary>
public string GetCellStyleNamespace(int row, int col)
{
return TableData.GetCell(row, col)?.StyleNamespace ?? m_DefaultStyleNamespace;
}
public void MergeCells(int startRow, int startCol, int rowSpan, int colSpan) public void MergeCells(int startRow, int startCol, int rowSpan, int colSpan)
{ {
TableData.MergeCells(startRow, startCol, rowSpan, colSpan); TableData.MergeCells(startRow, startCol, rowSpan, colSpan);
@@ -519,6 +538,7 @@ namespace XericUI.XTable.Rendering.Component
m_SelectStartRow, m_SelectStartCol, m_SelectStartRow, m_SelectStartCol,
m_SelectEndRow, m_SelectEndCol); m_SelectEndRow, m_SelectEndCol);
} }
return XTableCoordinateFormat.CellToText(m_SelectedRow, m_SelectedCol); return XTableCoordinateFormat.CellToText(m_SelectedRow, m_SelectedCol);
} }
@@ -555,6 +575,21 @@ namespace XericUI.XTable.Rendering.Component
// 回收池 // 回收池
m_Assembler.ReturnAll(); m_Assembler.ReturnAll();
// 额外清理孤立的直接子对象(防止域重载或异常流程产生的残留)
Transform poolRoot = m_Assembler.PoolRoot;
for (int i = rectTransform.childCount - 1; i >= 0; i--)
{
var child = rectTransform.GetChild(i);
if ((poolRoot != null && child == poolRoot)) continue;
if (m_BackgroundGo != null && child.gameObject == m_BackgroundGo) continue;
child.gameObject.hideFlags = HideFlags.None;
if (Application.isPlaying)
Destroy(child.gameObject);
else
DestroyImmediate(child.gameObject);
}
// 计算可见范围 // 计算可见范围
int sr, er, sc, ec; int sr, er, sc, ec;
CalcVisibleRange(out sr, out er, out sc, out ec); CalcVisibleRange(out sr, out er, out sc, out ec);
@@ -593,18 +628,53 @@ namespace XericUI.XTable.Rendering.Component
bgImg.raycastTarget = false; bgImg.raycastTarget = false;
} }
/// <summary>销毁 transform 下所有子对象(用于域重载后清理残留)</summary>
private void ClearAllChildren()
{
if (rectTransform == null) return;
#if UNITY_EDITOR
// 编辑器模式下先重置 HideFlags 再销毁,防止 DontSave 对象拒绝销毁
while (rectTransform.childCount > 0)
{
var child = rectTransform.GetChild(0);
child.gameObject.hideFlags = HideFlags.None;
if (Application.isPlaying)
Destroy(child.gameObject);
else
DestroyImmediate(child.gameObject);
}
#else
for (int i = rectTransform.childCount - 1; i >= 0; i--)
{
var child = rectTransform.GetChild(i);
Destroy(child.gameObject);
}
#endif
}
/// <summary>设置所有动态子对象的可见性(仅编辑器)</summary>
public void SetChildrenVisible(bool visible)
{
#if UNITY_EDITOR
if (rectTransform == null) return;
for (int i = rectTransform.childCount - 1; i >= 0; i--)
{
var child = rectTransform.GetChild(i);
child.gameObject.hideFlags = visible
? HideFlags.DontSave // 可见但不保存
: HideFlags.DontSave | HideFlags.HideInHierarchy; // 隐藏且不保存
}
#endif
}
/// <summary>完全重建表格——销毁所有动态生成的对象并重新初始化</summary> /// <summary>完全重建表格——销毁所有动态生成的对象并重新初始化</summary>
[ContextMenu("Xeric UI/完全重建表格")] [ContextMenu("Xeric UI/完全重建表格")]
public void FullRebuild() public void FullRebuild()
{ {
m_Assembler?.DestroyAll(); m_Assembler?.DestroyAll();
ClearAllChildren();
if (m_BackgroundGo != null)
{
if (Application.isPlaying) Destroy(m_BackgroundGo);
else DestroyImmediate(m_BackgroundGo);
m_BackgroundGo = null; m_BackgroundGo = null;
}
m_TableData = new XTableData(); m_TableData = new XTableData();
@@ -646,7 +716,8 @@ namespace XericUI.XTable.Rendering.Component
selImg.Rect.anchoredPosition = new Vector2(cellX, cellY); selImg.Rect.anchoredPosition = new Vector2(cellX, cellY);
selImg.Rect.sizeDelta = new Vector2(cellW, cellH); selImg.Rect.sizeDelta = new Vector2(cellW, cellH);
selImg.Image.color = (r == m_SelectedRow && c == m_SelectedCol) selImg.Image.color = (r == m_SelectedRow && c == m_SelectedCol)
? focusColor : multiSelectColor; ? focusColor
: multiSelectColor;
} }
// 单选高亮 // 单选高亮
else if (r == m_SelectedRow && c == m_SelectedCol) else if (r == m_SelectedRow && c == m_SelectedCol)
@@ -710,11 +781,13 @@ namespace XericUI.XTable.Rendering.Component
{ {
m_TotalWidth = 0; m_TotalWidth = 0;
if (m_ColWidths != null) if (m_ColWidths != null)
foreach (float w in m_ColWidths) m_TotalWidth += w; foreach (float w in m_ColWidths)
m_TotalWidth += w;
m_TotalHeight = 0; m_TotalHeight = 0;
if (m_RowHeights != null) if (m_RowHeights != null)
foreach (float h in m_RowHeights) m_TotalHeight += h; foreach (float h in m_RowHeights)
m_TotalHeight += h;
if (m_ScrollRect != null && m_ScrollRect.content != null) if (m_ScrollRect != null && m_ScrollRect.content != null)
m_ScrollRect.content.sizeDelta = new Vector2(m_TotalWidth, m_TotalHeight); m_ScrollRect.content.sizeDelta = new Vector2(m_TotalWidth, m_TotalHeight);
@@ -739,7 +812,10 @@ namespace XericUI.XTable.Rendering.Component
private void CalcVisibleRange(out int sr, out int er, out int sc, out int ec) private void CalcVisibleRange(out int sr, out int er, out int sc, out int ec)
{ {
sr = 0; er = 0; sc = 0; ec = 0; sr = 0;
er = 0;
sc = 0;
ec = 0;
float accY = 0; float accY = 0;
for (int r = 0; r < m_RowHeights.Length; r++) for (int r = 0; r < m_RowHeights.Length; r++)
@@ -750,6 +826,7 @@ namespace XericUI.XTable.Rendering.Component
if (er == 0 && sr == 0) sr = r; if (er == 0 && sr == 0) sr = r;
er = r + 1; er = r + 1;
} }
accY = rowBottom; accY = rowBottom;
if (accY > m_VisibleRect.yMax) break; if (accY > m_VisibleRect.yMax) break;
} }
@@ -763,6 +840,7 @@ namespace XericUI.XTable.Rendering.Component
if (ec == 0 && sc == 0) sc = c; if (ec == 0 && sc == 0) sc = c;
ec = c + 1; ec = c + 1;
} }
accX = colRight; accX = colRight;
if (accX > m_VisibleRect.xMax) break; if (accX > m_VisibleRect.xMax) break;
} }
@@ -830,7 +908,12 @@ namespace XericUI.XTable.Rendering.Component
float ax = 0; float ax = 0;
for (int c = 0; c < m_ColWidths.Length; c++) for (int c = 0; c < m_ColWidths.Length; c++)
{ {
if (tableX >= ax && tableX < ax + m_ColWidths[c]) { col = c; break; } if (tableX >= ax && tableX < ax + m_ColWidths[c])
{
col = c;
break;
}
ax += m_ColWidths[c]; ax += m_ColWidths[c];
} }
@@ -838,7 +921,12 @@ namespace XericUI.XTable.Rendering.Component
float ay = 0; float ay = 0;
for (int r = 0; r < m_RowHeights.Length; r++) for (int r = 0; r < m_RowHeights.Length; r++)
{ {
if (tableY >= ay && tableY < ay + m_RowHeights[r]) { row = r; break; } if (tableY >= ay && tableY < ay + m_RowHeights[r])
{
row = r;
break;
}
ay += m_RowHeights[r]; ay += m_RowHeights[r];
} }
@@ -39,6 +39,9 @@ namespace XericUI.XTable.Rendering.Elements
private Transform m_Parent; private Transform m_Parent;
private Transform m_PoolRoot; private Transform m_PoolRoot;
/// <summary>池根节点(所有回收对象均挂在此节点下,节点本身处于 inactive)</summary>
public Transform PoolRoot => m_PoolRoot;
private Stack<PooledImage> m_ImagePool = new Stack<PooledImage>(); private Stack<PooledImage> m_ImagePool = new Stack<PooledImage>();
private Stack<PooledText> m_TextPool = new Stack<PooledText>(); private Stack<PooledText> m_TextPool = new Stack<PooledText>();
private List<PooledImage> m_ActiveImages = new List<PooledImage>(); private List<PooledImage> m_ActiveImages = new List<PooledImage>();