using System; using System.Collections.Generic; using UnityEngine; namespace XericUI.XTable.Core { /// /// 单个数据块——保存 BlockSizeX × BlockSizeY 个单元格数据。 /// 数据使用线性数组存储,索引 = localRow * BlockSizeX + localCol。 /// [Serializable] public class XTableBlock : ISerializationCallbackReceiver { /// 块在全局块坐标系中的行号 public int BlockRow; /// 块在全局块坐标系中的列号 public int BlockCol; /// 块内单元格列数 public int BlockSizeX; /// 块内单元格行数 public int BlockSizeY; /// /// 数据存储——线性数组。 /// 索引 = localRow * BlockSizeX + localCol。 /// public XTableCellData[] Cells; /// 合并单元格描述列表 public List MergeDescriptors; // 仅用于读取旧资产;新块不分配、运行时不使用这些逐格边框数组。 [HideInInspector] public string[] BorderRightNs; [HideInInspector] public string[] BorderBottomNs; /// /// 创建指定尺寸的块 /// public XTableBlock(int blockSizeX, int blockSizeY) { BlockSizeX = blockSizeX; BlockSizeY = blockSizeY; Cells = new XTableCellData[blockSizeX * blockSizeY]; MergeDescriptors = new List(); } /// /// 创建指定尺寸和位置的块 /// public XTableBlock(int blockSizeX, int blockSizeY, int blockRow, int blockCol) : this(blockSizeX, blockSizeY) { BlockRow = blockRow; BlockCol = blockCol; } /// /// 通过块内局部坐标获取单元格数据 /// public XTableCellData GetCell(int localRow, int localCol) { int index = localRow * BlockSizeX + localCol; if (index < 0 || index >= Cells.Length) return null; return Cells[index]; } /// /// 通过块内局部坐标设置单元格数据 /// public void SetCell(int localRow, int localCol, XTableCellData data) { int index = localRow * BlockSizeX + localCol; if (index < 0 || index >= Cells.Length) return; Cells[index] = data; } /// /// 检查是否有合并描述重定向此单元格 /// /// 合并描述,若无重定向则返回 null public XTableMergeDescriptor GetMergeRedirect(int localRow, int localCol) { if (MergeDescriptors == null || MergeDescriptors.Count == 0) return null; for (int i = 0; i < MergeDescriptors.Count; i++) { if (MergeDescriptors[i].Contains(localRow, localCol)) return MergeDescriptors[i]; } return null; } #region 序列化回调 public void OnBeforeSerialize() { } public void OnAfterDeserialize() { // 反序列化时确保 BlockSize 有效(构造函数被跳过) if (BlockSizeX <= 0) BlockSizeX = XTableData.DefaultBlockSize; if (BlockSizeY <= 0) BlockSizeY = XTableData.DefaultBlockSize; // 确保数组和列表非空 if (Cells == null) Cells = new XTableCellData[BlockSizeX * BlockSizeY]; if (MergeDescriptors == null) MergeDescriptors = new List(); } #endregion } }