From cac9bb196d5370c6df211cbcf70941b6c4a20f4f Mon Sep 17 00:00:00 2001 From: lrc <571244399@qq.com> Date: Mon, 29 Jun 2026 22:38:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=A1=A8=E6=A0=BC?= =?UTF-8?q?=E4=B8=8A=E7=9A=84=E5=AF=BC=E5=85=A5=E5=AF=BC=E5=87=BA=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/XTable/XericUIActionTableEditor.cs | 29 +++ .../Core/StyleSheetUI/StyleRefreshManager.cs | 2 +- Runtime/XTable/Core/XTableClipboardData.cs | 177 ++++++++++++++++++ .../XTable/Core/XTableClipboardData.cs.meta | 11 ++ .../Rendering/Component/XericUIActionTable.cs | 28 +++ 5 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 Runtime/XTable/Core/XTableClipboardData.cs create mode 100644 Runtime/XTable/Core/XTableClipboardData.cs.meta diff --git a/Editor/XTable/XericUIActionTableEditor.cs b/Editor/XTable/XericUIActionTableEditor.cs index a6c6533..c686f88 100644 --- a/Editor/XTable/XericUIActionTableEditor.cs +++ b/Editor/XTable/XericUIActionTableEditor.cs @@ -1,6 +1,8 @@ using UnityEditor; using UnityEngine; using UnityEngine.UI; +using XericLibrary.Runtime.MacroLibrary; +using XericUI.XTable.Core; using XericUI.XTable.Rendering.Component; namespace XericUIEditor.XTable @@ -92,6 +94,33 @@ namespace XericUIEditor.XTable EditorGUILayout.Space(4); + EditorGUILayout.BeginHorizontal(); + if (GUILayout.Button("导出到剪贴板", GUILayout.Height(26))) + { + var data = m_Table.BuildClipboardData(); + MacroFile.SetClipboardObject(data, MacroFile.JsonSerializerFormatter.formatterWithZip); + Debug.Log($"[XTable] 表格数据已导出到剪贴板 ({data.Cells?.Count ?? 0} 个单元格, {data.Merges?.Count ?? 0} 个合并)"); + } + if (GUILayout.Button("从剪贴板导入", GUILayout.Height(26))) + { + var data = MacroFile.GetClipboardObject(MacroFile.JsonSerializerFormatter.formatterWithZip); + if (data != null) + { + Undo.RecordObject(m_Table, "Import Table from Clipboard"); + m_Table.ApplyClipboardData(data); + serializedObject.Update(); + Repaint(); + Debug.Log($"[XTable] 已从剪贴板导入表格数据 ({data.Cells?.Count ?? 0} 个单元格, {data.Merges?.Count ?? 0} 个合并)"); + } + else + { + Debug.LogWarning("[XTable] 剪贴板中没有有效的表格数据"); + } + } + EditorGUILayout.EndHorizontal(); + + EditorGUILayout.Space(4); + bool newShow = EditorGUILayout.Toggle("显示子项", m_ShowChildren); if (newShow != m_ShowChildren) { diff --git a/Runtime/Core/StyleSheetUI/StyleRefreshManager.cs b/Runtime/Core/StyleSheetUI/StyleRefreshManager.cs index a8048d8..87d95e1 100644 --- a/Runtime/Core/StyleSheetUI/StyleRefreshManager.cs +++ b/Runtime/Core/StyleSheetUI/StyleRefreshManager.cs @@ -43,7 +43,7 @@ namespace XericUI.Core.StyleSheetUI /// public static void ForceRefreshAll() { -#if XERIC_STYLESHEET_ENABLED +#if XericLibrary XericLibrary.Runtime.SuperStyleSheet.StyleManager.ForceRefresh(); #endif diff --git a/Runtime/XTable/Core/XTableClipboardData.cs b/Runtime/XTable/Core/XTableClipboardData.cs new file mode 100644 index 0000000..ab5466c --- /dev/null +++ b/Runtime/XTable/Core/XTableClipboardData.cs @@ -0,0 +1,177 @@ +using System; +using System.Collections.Generic; + +using XericUI.XTable.Mapping; +using XericUI.XTable.Rendering.Component; + +namespace XericUI.XTable.Core +{ + /// + /// 表格粘贴复制传输对象——将表格的尺寸、单元格文本、合并信息打包为可序列化结构, + /// 配合 MacroFile.SetClipboardObject / GetClipboardObject 实现表格内容的导入导出。 + /// + [Serializable] + public class XTableClipboardData + { + [Serializable] + public struct CellEntry + { + public int Row; + public int Col; + public string Text; + public string StyleNamespace; + + public CellEntry(int row, int col, string text, string styleNamespace = null) + { + Row = row; + Col = col; + Text = text; + StyleNamespace = styleNamespace; + } + } + + [Serializable] + public struct MergeEntry + { + /// 合并起始行(全局坐标) + public int StartRow; + + /// 合并起始列(全局坐标) + public int StartCol; + + /// 合并行数 + public int RowSpan; + + /// 合并列数 + public int ColSpan; + + public MergeEntry(int startRow, int startCol, int rowSpan, int colSpan) + { + StartRow = startRow; + StartCol = startCol; + RowSpan = rowSpan; + ColSpan = colSpan; + } + } + + /// 行高数组 + public float[] RowHeights; + + /// 列宽数组 + public float[] ColWidths; + + /// 默认样式命名空间 + public string DefaultStyleNamespace; + + /// 单元格数据列表(仅序列化有文本内容的单元格) + public List Cells; + + /// 合并单元格描述列表 + public List Merges; + + public XTableClipboardData() + { + Cells = new List(); + Merges = new List(); + } + + /// + /// 从表格收集所有数据——尺寸、单元格文本、合并信息。 + /// 调用方可将返回的 DTO 传给 MacroFile.SetClipboardObject 写入剪贴板。 + /// + public static XTableClipboardData FromTable(XericUIActionTable table) + { + var data = new XTableClipboardData + { + RowHeights = table.RowHeights, + ColWidths = table.ColWidths, + DefaultStyleNamespace = table.DefaultStyleNamespace, + }; + + int rows = table.RowHeights?.Length ?? 0; + int cols = table.ColWidths?.Length ?? 0; + var tableData = table.TableData; + for (int r = 0; r < rows; r++) + { + for (int c = 0; c < cols; c++) + { + var cell = tableData.GetCell(r, c); + if (cell == null) continue; + + bool hasContent = !string.IsNullOrEmpty(cell.Text) + || !string.IsNullOrEmpty(cell.StyleNamespace); + if (!hasContent) continue; + + data.Cells.Add(new CellEntry(r, c, cell.Text, cell.StyleNamespace)); + } + } + + // 收集合并信息(全局坐标) + if (tableData != null) + { + foreach (var kv in tableData.BlockMap) + { + var block = kv.Value; + if (block.MergeDescriptors == null || block.MergeDescriptors.Count == 0) continue; + + XTableBlockMapping.ZIndexToBlockCoord(kv.Key, out int blockRow, out int blockCol); + + foreach (var desc in block.MergeDescriptors) + { + if (desc.IsCrossBlock) continue; + + int globalStartRow = blockRow * tableData.BlockSizeY + desc.LocalStartRow; + int globalStartCol = blockCol * tableData.BlockSizeX + desc.LocalStartCol; + + data.Merges.Add(new MergeEntry(globalStartRow, globalStartCol, + desc.MergeRowSpan, desc.MergeColSpan)); + } + } + } + + return data; + } + + /// + /// 将 DTO 中的单元格文本和合并信息写回表格数据层。 + /// 尺寸与命名空间的设置由表格组件在 ApplyClipboardData 中处理。 + /// + public void PopulateToTable(XericUIActionTable table) + { + var td = table.TableData; + if (td == null) return; + + int rows = table.RowHeights?.Length ?? 0; + int cols = table.ColWidths?.Length ?? 0; + + // 单元格文本 + if (Cells != null) + { + foreach (var entry in Cells) + { + if (entry.Row < 0 || entry.Row >= rows || entry.Col < 0 || entry.Col >= cols) + continue; + + var cell = td.GetOrCreateCell(entry.Row, entry.Col); + cell.Text = entry.Text; + cell.StyleNamespace = entry.StyleNamespace; + } + } + + // 合并信息 + if (Merges != null) + { + foreach (var merge in Merges) + { + if (merge.RowSpan > 1 || merge.ColSpan > 1) + { + int endRow = merge.StartRow + merge.RowSpan - 1; + int endCol = merge.StartCol + merge.ColSpan - 1; + if (endRow < rows && endCol < cols) + td.MergeCells(merge.StartRow, merge.StartCol, endRow, endCol); + } + } + } + } + } +} diff --git a/Runtime/XTable/Core/XTableClipboardData.cs.meta b/Runtime/XTable/Core/XTableClipboardData.cs.meta new file mode 100644 index 0000000..1cc04e9 --- /dev/null +++ b/Runtime/XTable/Core/XTableClipboardData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 31b3bc5cc85539b4db6ac19bbb4a1888 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/XTable/Rendering/Component/XericUIActionTable.cs b/Runtime/XTable/Rendering/Component/XericUIActionTable.cs index 407f5ad..b030bec 100644 --- a/Runtime/XTable/Rendering/Component/XericUIActionTable.cs +++ b/Runtime/XTable/Rendering/Component/XericUIActionTable.cs @@ -766,6 +766,34 @@ namespace XericUI.XTable.Rendering.Component RefreshView(); } + /// 构建剪贴板数据对象(尺寸 + 单元格文本 + 合并信息) + public XTableClipboardData BuildClipboardData() + { + return XTableClipboardData.FromTable(this); + } + + /// 将剪贴板数据应用到表格——覆盖尺寸、命名空间、单元格文本、合并信息并刷新 + public void ApplyClipboardData(XTableClipboardData data) + { + if (data == null) return; + + // 应用尺寸与默认命名空间 + if (data.RowHeights != null && data.RowHeights.Length > 0) + m_RowHeights = data.RowHeights; + if (data.ColWidths != null && data.ColWidths.Length > 0) + m_ColWidths = data.ColWidths; + if (!string.IsNullOrEmpty(data.DefaultStyleNamespace)) + m_DefaultStyleNamespace = data.DefaultStyleNamespace; + + // 写回单元格文本与合并信息 + data.PopulateToTable(this); + + MarkDirty(TableDirtyType.DataChanged); + RecalculateTotalSize(); + RefreshView(); + SetChildrenVisible(m_ChildrenVisible); + } + #endregion #region 单元格渲染 From b871e947b106993e4c96820fb191fbc5c64bf905 Mon Sep 17 00:00:00 2001 From: lrc <571244399@qq.com> Date: Tue, 30 Jun 2026 09:13:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=95=B4=E5=90=88=E8=A1=A8=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../XericUIActionTable.StyleParams.cs | 99 +++++++++++++++++ .../XericUIActionTable.StyleParams.cs.meta | 11 ++ .../Rendering/Component/XericUIActionTable.cs | 103 +++++++----------- 3 files changed, 150 insertions(+), 63 deletions(-) create mode 100644 Runtime/XTable/Rendering/Component/XericUIActionTable.StyleParams.cs create mode 100644 Runtime/XTable/Rendering/Component/XericUIActionTable.StyleParams.cs.meta diff --git a/Runtime/XTable/Rendering/Component/XericUIActionTable.StyleParams.cs b/Runtime/XTable/Rendering/Component/XericUIActionTable.StyleParams.cs new file mode 100644 index 0000000..19ed9b6 --- /dev/null +++ b/Runtime/XTable/Rendering/Component/XericUIActionTable.StyleParams.cs @@ -0,0 +1,99 @@ +using TMPro; +using UnityEngine; +using XericLibrary.Runtime.SuperStyleSheet; + +namespace XericUI.XTable.Rendering.Component +{ + /// + /// 表格样式参数——统一管理所有样式路径与回退值。 + /// 修改此处的常量即可全局调整表格外观,无需在各方法中查找硬编码字符串。 + /// + public partial class XericUIActionTable + { + #region 样式路径常量 + + public const string STYLE_CELL_BG_COLOR = "/cell/bg/color"; + public const string STYLE_CELL_FG_COLOR = "/cell/fg/color"; + public const string STYLE_CELL_SELECTION_BG_COLOR = "/cell/selection/bg/color"; + + public const string STYLE_CELL_BORDER_TOP_COLOR = "/cell/border/top/color"; + public const string STYLE_CELL_BORDER_TOP_WIDTH = "/cell/border/top/width"; + public const string STYLE_CELL_BORDER_BOTTOM_COLOR = "/cell/border/bottom/color"; + public const string STYLE_CELL_BORDER_BOTTOM_WIDTH = "/cell/border/bottom/width"; + public const string STYLE_CELL_BORDER_LEFT_COLOR = "/cell/border/left/color"; + public const string STYLE_CELL_BORDER_LEFT_WIDTH = "/cell/border/left/width"; + public const string STYLE_CELL_BORDER_RIGHT_COLOR = "/cell/border/right/color"; + public const string STYLE_CELL_BORDER_RIGHT_WIDTH = "/cell/border/right/width"; + + public const string STYLE_CELL_FONT_SIZE = "/cell/font/size"; + public const string STYLE_CELL_FONT_ALIGNMENT = "/cell/font/alignment"; + public const string STYLE_CELL_FONT_RICH_TEXT = "/cell/font/richText"; + public const string STYLE_CELL_FONT_ASSET = "/cell/font/fontAsset"; + + public const string STYLE_CELL_DEFAULT_HEIGHT = "/cell/default/height"; + public const string STYLE_CELL_DEFAULT_WIDTH = "/cell/default/width"; + + #endregion + + #region 回退值(GetStyleColor/GetStyleFloat/GetStyleInt 兜底参数) + + public static readonly Color BG_COLOR_FALLBACK = new Color(0.18f, 0.18f, 0.18f, 1f); + public static readonly Color FG_COLOR_FALLBACK = new Color(0.05f, 0.05f, 0.05f); + public static readonly Color SELECTION_COLOR_FALLBACK = new Color(0.2f, 0.5f, 1f, 0.3f); + public static readonly Color BORDER_COLOR_FALLBACK = new Color(0.35f, 0.35f, 0.35f, 0.5f); + public static readonly float BORDER_WIDTH_FALLBACK = 1f; + public static readonly int FONT_SIZE_FALLBACK = 14; + + #endregion + + #region 动态默认值(EnsureDefaultStyleNamespace 中 AddDynamicStyle 的初始值,匹配 DefaultTableStyles.xsss) + + public static readonly Color DEFAULT_BG_COLOR = Color.white; + public static readonly Color DEFAULT_FG_COLOR = new Color(0.1f, 0.1f, 0.1f); + public static readonly Color DEFAULT_BORDER_COLOR = new Color(0.8f, 0.8f, 0.8f); + public static readonly float DEFAULT_BORDER_WIDTH = 1f; + public static readonly Color DEFAULT_SELECTION_COLOR = new Color(0.2f, 0.5f, 1f, 0.3f); + public static readonly int DEFAULT_FONT_SIZE = 14; + public const string DEFAULT_FONT_ALIGNMENT = "MiddleCenter"; + public const bool DEFAULT_FONT_RICH_TEXT = true; + public const float DEFAULT_CELL_HEIGHT = 40f; + public const float DEFAULT_CELL_WIDTH = 100f; + + #endregion + + #region 运行时硬编码色值 + + public static readonly Color RT_SELECTION_COLOR = new Color(0.18f, 0.42f, 0.82f, 0.25f); + public static readonly Color RT_MULTI_SELECT_COLOR = new Color(0.18f, 0.42f, 0.82f, 0.12f); + public static readonly Color RT_FOCUS_COLOR = new Color(0.22f, 0.55f, 0.95f, 0.35f); + public static readonly Color RT_BORDER_COLOR = new Color(0.35f, 0.35f, 0.35f, 0.5f); + public const float RT_BORDER_WIDTH = 1f; + + #endregion + + #region 便捷取值方法(封装路径 + 回退值) + + private Color GetCellBgColor(string ns) => + GetStyleColor(ns, STYLE_CELL_BG_COLOR, BG_COLOR_FALLBACK); + + private Color GetCellFgColor(string ns) => + GetStyleColor(ns, STYLE_CELL_FG_COLOR, FG_COLOR_FALLBACK); + + private Color GetCellSelectionBgColor(string ns) => + GetStyleColor(ns, STYLE_CELL_SELECTION_BG_COLOR, SELECTION_COLOR_FALLBACK); + + private Color GetCellBorderColor(string ns) => + GetStyleColor(ns, STYLE_CELL_BORDER_BOTTOM_COLOR, BORDER_COLOR_FALLBACK); + + private float GetCellBorderWidth(string ns) => + GetStyleFloat(ns, STYLE_CELL_BORDER_BOTTOM_WIDTH, BORDER_WIDTH_FALLBACK); + + private int GetCellFontSize(string ns) => + GetStyleInt(ns, STYLE_CELL_FONT_SIZE, FONT_SIZE_FALLBACK); + + private TMP_FontAsset GetCellFontAsset(string ns) => + GetStyleFontAsset(ns, STYLE_CELL_FONT_ASSET); + + #endregion + } +} diff --git a/Runtime/XTable/Rendering/Component/XericUIActionTable.StyleParams.cs.meta b/Runtime/XTable/Rendering/Component/XericUIActionTable.StyleParams.cs.meta new file mode 100644 index 0000000..dc8e1c4 --- /dev/null +++ b/Runtime/XTable/Rendering/Component/XericUIActionTable.StyleParams.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0521a59402d81494a9874fcda645ed81 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/XTable/Rendering/Component/XericUIActionTable.cs b/Runtime/XTable/Rendering/Component/XericUIActionTable.cs index b030bec..36f78c6 100644 --- a/Runtime/XTable/Rendering/Component/XericUIActionTable.cs +++ b/Runtime/XTable/Rendering/Component/XericUIActionTable.cs @@ -660,7 +660,7 @@ namespace XericUI.XTable.Rendering.Component bgRt.sizeDelta = new Vector2(m_TotalWidth, m_TotalHeight); var bgImg = m_BackgroundGo.GetComponent(); - bgImg.color = GetStyleColor(m_DefaultStyleNamespace, "/cell/bg/color", new Color(0.18f, 0.18f, 0.18f, 1f)); + bgImg.color = GetCellBgColor(m_DefaultStyleNamespace); bgImg.raycastTarget = false; } @@ -800,16 +800,15 @@ namespace XericUI.XTable.Rendering.Component private void RenderCells(int sr, int er, int sc, int ec) { - Color fgFallback = new Color(0.05f, 0.05f, 0.05f); string ns = m_DefaultStyleNamespace; // 编辑器模式:从样式表 pull 所有颜色值 #if UNITY_EDITOR if (!Application.isPlaying) { - Color selectionColor = GetStyleColor(ns, "/cell/selection/bg/color", new Color(0.2f, 0.5f, 1f, 0.3f)); - Color borderColor = GetStyleColor(ns, "/cell/border/bottom/color", new Color(0.35f, 0.35f, 0.35f, 0.5f)); - float borderWidth = GetStyleFloat(ns, "/cell/border/bottom/width", 1f); + Color selectionColor = GetCellSelectionBgColor(ns); + Color borderColor = GetCellBorderColor(ns); + float borderWidth = GetCellBorderWidth(ns); Color multiSelectColor = new Color(selectionColor.r, selectionColor.g, selectionColor.b, selectionColor.a * 0.4f); Color focusColor = new Color(selectionColor.r + 0.04f, selectionColor.g + 0.13f, selectionColor.b + 0.13f, selectionColor.a * 1.4f); @@ -876,10 +875,10 @@ namespace XericUI.XTable.Rendering.Component txt.Rect.anchoredPosition = new Vector2(cellX + 4, cellY); txt.Rect.sizeDelta = new Vector2(cellW - 8, cellH); txt.Text.text = data.Text; - txt.Text.color = GetStyleColor(cellNS, "/cell/fg/color", fgFallback); - txt.Text.fontSize = GetStyleInt(cellNS, "/cell/font/size", 14); + txt.Text.color = GetCellFgColor(cellNS); + txt.Text.fontSize = GetCellFontSize(cellNS); txt.Text.alignment = TextAlignmentOptions.Midline; - txt.Text.font = GetStyleFontAsset(cellNS, "/cell/font/fontAsset") ?? txt.Text.font; + txt.Text.font = GetCellFontAsset(cellNS) ?? txt.Text.font; } } } @@ -888,9 +887,9 @@ namespace XericUI.XTable.Rendering.Component #endif // 运行时模式 - Color rtSelectionColor = new Color(0.18f, 0.42f, 0.82f, 0.25f); - Color rtMultiSelectColor = new Color(0.18f, 0.42f, 0.82f, 0.12f); - Color rtFocusColor = new Color(0.22f, 0.55f, 0.95f, 0.35f); + Color rtSelectionColor = RT_SELECTION_COLOR; + Color rtMultiSelectColor = RT_MULTI_SELECT_COLOR; + Color rtFocusColor = RT_FOCUS_COLOR; for (int r = sr; r < er && r < m_RowHeights.Length; r++) { @@ -935,13 +934,13 @@ namespace XericUI.XTable.Rendering.Component var hLine = m_Assembler.GetImage("cell_hline"); hLine.Rect.anchoredPosition = new Vector2(cellX, cellY - cellH); - hLine.Rect.sizeDelta = new Vector2(cellW, 1f); - hLine.Image.color = new Color(0.35f, 0.35f, 0.35f, 0.5f); + hLine.Rect.sizeDelta = new Vector2(cellW, RT_BORDER_WIDTH); + hLine.Image.color = RT_BORDER_COLOR; var vLine = m_Assembler.GetImage("cell_vline"); - vLine.Rect.anchoredPosition = new Vector2(cellX + cellW - 1, cellY); - vLine.Rect.sizeDelta = new Vector2(1f, cellH); - vLine.Image.color = new Color(0.35f, 0.35f, 0.35f, 0.5f); + vLine.Rect.anchoredPosition = new Vector2(cellX + cellW - RT_BORDER_WIDTH, cellY); + vLine.Rect.sizeDelta = new Vector2(RT_BORDER_WIDTH, cellH); + vLine.Image.color = RT_BORDER_COLOR; if (data == null) continue; @@ -951,16 +950,7 @@ namespace XericUI.XTable.Rendering.Component txt.Rect.anchoredPosition = new Vector2(cellX + 4, cellY); txt.Rect.sizeDelta = new Vector2(cellW - 8, cellH); txt.Text.text = data.Text; - -#if UNITY_EDITOR - if (!Application.isPlaying) - { - txt.Text.color = GetStyleColor(cellNS, "/cell/fg/color", fgFallback); - txt.Text.fontSize = GetStyleInt(cellNS, "/cell/font/size", 14); - txt.Text.alignment = TextAlignmentOptions.Midline; - txt.Text.font = GetStyleFontAsset(cellNS, "/cell/font/fontAsset") ?? txt.Text.font; - } -#endif + // 运行时样式由 StyleBoundElement 被动更新,此处仅设置文本内容 } } } @@ -1090,45 +1080,32 @@ namespace XericUI.XTable.Rendering.Component private void EnsureDefaultStyleNamespace() { - // 避免在样式表尚未加载时触发 HasStyle → GetValue 的命名空间不存在错误 - var namespaces = StyleManager.GetAvailableNamespaces(); - if (namespaces == null || !namespaces.Contains(m_DefaultStyleNamespace)) + // 检查命名空间是否已存在,避免 HasStyle → GetValue 对不存在的命名空间报错 + bool nsExists = StyleManager.GetAvailableNamespaces()?.Contains(m_DefaultStyleNamespace) == true; + + // 命名空间已存在且样式已加载(来自 .xsss),无需添加回退值 + if (nsExists && StyleManager.HasStyle(m_DefaultStyleNamespace, STYLE_CELL_BG_COLOR)) return; - if (!StyleManager.HasStyle(m_DefaultStyleNamespace, "/cell/bg/color")) - { - // 回退默认值匹配 DefaultTableStyles.xsss - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/bg/color", Color.white); - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/fg/color", - new Color(0.1f, 0.1f, 0.1f)); - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/border/top/color", - new Color(0.8f, 0.8f, 0.8f)); - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/border/top/width", 1f); - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/border/bottom/color", - new Color(0.8f, 0.8f, 0.8f)); - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/border/bottom/width", 1f); - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/border/left/color", - new Color(0.8f, 0.8f, 0.8f)); - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/border/left/width", 1f); - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/border/right/color", - new Color(0.8f, 0.8f, 0.8f)); - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/border/right/width", 1f); - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/selection/bg/color", - new Color(0.2f, 0.5f, 1f, 0.3f)); - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/font/size", 14); - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/font/alignment", "MiddleCenter"); - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/font/richText", true); -#if UNITY_EDITOR - // 编辑器下尝试加载默认字体资源 - var defaultFont = UnityEditor.AssetDatabase.LoadAssetAtPath( - "Packages/com.lrss3.xericuiactionvessel/Runtime/Font/SourceHanSansSC-Regular-2 SDF.asset"); - if (defaultFont != null) - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/font/fontAsset", defaultFont); -#endif - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/default/height", 40f); - StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/default/width", 100f); - StyleManager.ForceRefresh(); - } + // 回退默认值匹配 DefaultTableStyles.xsss + // AddDynamicStyle 自动创建命名空间(如果尚不存在) + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_BG_COLOR, DEFAULT_BG_COLOR); + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_FG_COLOR, DEFAULT_FG_COLOR); + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_BORDER_TOP_COLOR, DEFAULT_BORDER_COLOR); + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_BORDER_TOP_WIDTH, DEFAULT_BORDER_WIDTH); + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_BORDER_BOTTOM_COLOR, DEFAULT_BORDER_COLOR); + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_BORDER_BOTTOM_WIDTH, DEFAULT_BORDER_WIDTH); + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_BORDER_LEFT_COLOR, DEFAULT_BORDER_COLOR); + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_BORDER_LEFT_WIDTH, DEFAULT_BORDER_WIDTH); + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_BORDER_RIGHT_COLOR, DEFAULT_BORDER_COLOR); + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_BORDER_RIGHT_WIDTH, DEFAULT_BORDER_WIDTH); + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_SELECTION_BG_COLOR, DEFAULT_SELECTION_COLOR); + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_FONT_SIZE, DEFAULT_FONT_SIZE); + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_FONT_ALIGNMENT, DEFAULT_FONT_ALIGNMENT); + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_FONT_RICH_TEXT, DEFAULT_FONT_RICH_TEXT); + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_DEFAULT_HEIGHT, DEFAULT_CELL_HEIGHT); + StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, STYLE_CELL_DEFAULT_WIDTH, DEFAULT_CELL_WIDTH); + StyleManager.ForceRefresh(); } #endregion