修复表格再2021等低版本中交互编辑时,会选中背后元素的情况。

格式化
This commit is contained in:
2026-06-29 11:56:19 +08:00
parent b7963afe30
commit aca1c5a94e
5 changed files with 274 additions and 169 deletions
+2 -3
View File
@@ -80,14 +80,13 @@ namespace XericUIEditor.XTable
if (GUILayout.Button("重新生成表格", GUILayout.Height(26)))
{
m_Table.FullRebuild();
m_Table.FullRebuildPreservingData();
m_Table.SetChildrenVisible(m_ShowChildren);
}
if (GUILayout.Button("强制刷新样式", GUILayout.Height(26)))
{
XericLibrary.Runtime.SuperStyleSheet.StyleManager.ForceRefresh();
m_Table.FullRebuild();
m_Table.ForceStyleRefresh();
m_Table.SetChildrenVisible(m_ShowChildren);
}
@@ -217,6 +217,25 @@ namespace XericUIEditor.XTable
// ★ 状态机:必须最先执行,后续所有逻辑依赖 s_State
UpdateState();
// Unity 2021 兼容:阻止鼠标事件穿透 overlay 到背后 UI 对象
// 原理:在 overlay 区域注册默认控件,Scene View 拣选系统检测到控件后
// 不再选中背后 GameObject(Canvas、ScrollRect 等)
if (s_State == EditorState.TableNavigation)
{
// 覆盖区域:行标签 + 列表头 + 表格主体 + 底部工具栏
Rect overlayArea = new Rect(
tableRect.x - COL_W - MARGIN, // 包含行标签
tableRect.y - ROW_H - MARGIN, // 包含列表头
tableRect.width + COL_W + MARGIN + 4, // 包含行标签
tableRect.height + ROW_H + MARGIN + 56); // 包含列表头+工具栏
if (overlayArea.Contains(Event.current.mousePosition))
{
int blockId = GUIUtility.GetControlID(FocusType.Passive);
HandleUtility.AddDefaultControl(blockId);
}
}
DrawRowLabels(rh, tableRect, xScale, yScale, table);
DrawColumnLabels(cw, tableRect, xScale, yScale, table);
DrawGridLines(rh, cw, tableRect, xScale, yScale);
@@ -9,6 +9,9 @@ using XericUI.XTable.Core;
using XericUI.XTable.Mapping;
using XericUI.XTable.Rendering.Elements;
using XericLibrary.Runtime.SuperStyleSheet;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace XericUI.XTable.Rendering.Component
{
@@ -721,6 +724,48 @@ namespace XericUI.XTable.Rendering.Component
SetChildrenVisible(m_ChildrenVisible);
}
/// <summary>重新生成表格但保留已有文本数据——先保存,重建后再回填</summary>
public void FullRebuildPreservingData()
{
// 保存当前单元格文本
var savedTexts = new Dictionary<(int, int), string>();
if (m_TableData != null)
{
int rows = m_RowHeights?.Length ?? 0;
int cols = m_ColWidths?.Length ?? 0;
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
var cell = m_TableData.GetCell(r, c);
if (cell != null && !string.IsNullOrEmpty(cell.Text))
savedTexts[(r, c)] = cell.Text;
}
}
}
FullRebuild();
// 恢复文本数据
foreach (var kv in savedTexts)
{
int r = kv.Key.Item1, c = kv.Key.Item2;
if (r < (m_RowHeights?.Length ?? 0) && c < (m_ColWidths?.Length ?? 0))
m_TableData.GetOrCreateCell(r, c).Text = kv.Value;
}
RefreshView();
SetChildrenVisible(m_ChildrenVisible);
}
/// <summary>仅强制刷新样式,不重建数据结构</summary>
public void ForceStyleRefresh()
{
StyleManager.ForceRefresh();
MarkDirty(TableDirtyType.StyleChanged);
RefreshView();
}
#endregion
#region 单元格渲染
@@ -806,6 +851,7 @@ namespace XericUI.XTable.Rendering.Component
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;
}
}
}
@@ -884,6 +930,7 @@ namespace XericUI.XTable.Rendering.Component
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
}
@@ -1005,6 +1052,14 @@ namespace XericUI.XTable.Rendering.Component
return val != null ? (float)val : fallback;
}
private TMP_FontAsset GetStyleFontAsset(string ns, string path, TMP_FontAsset fallback = null)
{
StyleValue val = StyleManager.GetValue(ns, path);
if (val == null) return fallback;
try { return val.GetValueAs<TMP_FontAsset>(); }
catch { return fallback; }
}
private void EnsureDefaultStyleNamespace()
{
// 避免在样式表尚未加载时触发 HasStyle → GetValue 的命名空间不存在错误
@@ -1035,6 +1090,13 @@ namespace XericUI.XTable.Rendering.Component
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<TMP_FontAsset>(
"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();
@@ -1,183 +1,197 @@
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using XericLibrary.Runtime.SuperStyleSheet;
namespace XericUI.XTable.Rendering.Elements
{
/// <summary>
/// 样式绑定元素——将 Image 或 TMP_Text 挂勾到样式表路径。
/// 自动从 StyleManager 拉取值并应用,样式变更时自动刷新。
/// 通过对象池复用时调用 Bind/Unbind 切换绑定目标。
/// </summary>
public class StyleBoundElement
{
#region 字段
/// <summary>
/// 样式绑定元素——将 Image 或 TMP_Text 挂勾到样式表路径。
/// 自动从 StyleManager 拉取值并应用,样式变更时自动刷新。
/// 通过对象池复用时调用 Bind/Unbind 切换绑定目标。
/// </summary>
public class StyleBoundElement
{
#region 字段
/// <summary>绑定的 Image 组件(可能为 null)</summary>
public Image ImageComponent;
/// <summary>绑定的 Image 组件(可能为 null)</summary>
public Image ImageComponent;
/// <summary>绑定的 TMP_Text 组件(可能为 null)</summary>
public TMP_Text TextComponent;
/// <summary>绑定的 TMP_Text 组件(可能为 null)</summary>
public TMP_Text TextComponent;
/// <summary>样式命名空间</summary>
private string m_StyleNamespace;
/// <summary>样式命名空间</summary>
private string m_StyleNamespace;
/// <summary>各路径的 StyleField 缓存</summary>
private System.Collections.Generic.Dictionary<string, StyleField> m_Fields
= new System.Collections.Generic.Dictionary<string, StyleField>();
/// <summary>各路径的 StyleField 缓存</summary>
private System.Collections.Generic.Dictionary<string, StyleField> m_Fields
= new System.Collections.Generic.Dictionary<string, StyleField>();
/// <summary>是否已绑定</summary>
private bool m_IsBound;
/// <summary>是否已绑定</summary>
private bool m_IsBound;
/// <summary>样式变更时触发(通知表格重绘)</summary>
public event System.Action OnStyleChangedCallback;
/// <summary>样式变更时触发(通知表格重绘)</summary>
public event System.Action OnStyleChangedCallback;
/// <summary>当前绑定的命名空间(空字符串表示未绑定)</summary>
public string BoundNamespace => m_IsBound ? (m_StyleNamespace ?? "") : "";
/// <summary>当前绑定的命名空间(空字符串表示未绑定)</summary>
public string BoundNamespace => m_IsBound ? (m_StyleNamespace ?? "") : "";
#endregion
#endregion
#region 公开方法
#region 公开方法
/// <summary>
/// 绑定到指定命名空间并开始监听样式。
/// 运行时注册 StyleField 实现被动更新(push);编辑器下在渲染时直接 pull。
/// </summary>
public void Bind(string styleNamespace)
{
if (m_IsBound) Unbind();
m_StyleNamespace = styleNamespace;
/// <summary>
/// 绑定到指定命名空间并开始监听样式。
/// 运行时注册 StyleField 实现被动更新(push);编辑器下在渲染时直接 pull。
/// </summary>
public void Bind(string styleNamespace)
{
if (m_IsBound) Unbind();
m_StyleNamespace = styleNamespace;
if (string.IsNullOrEmpty(styleNamespace)) return;
if (string.IsNullOrEmpty(styleNamespace)) return;
// 检查命名空间是否存在,避免 Register → GetValue 报错
var namespaces = StyleManager.GetAvailableNamespaces();
if (namespaces == null || !namespaces.Contains(styleNamespace))
return;
// 检查命名空间是否存在,避免 Register → GetValue 报错
var namespaces = StyleManager.GetAvailableNamespaces();
if (namespaces == null || !namespaces.Contains(styleNamespace))
return;
// 预注册常用路径
RegisterField("/cell/bg/color", OnStyleChanged);
RegisterField("/cell/fg/color", OnStyleChanged);
RegisterField("/cell/border/top/color", OnStyleChanged);
RegisterField("/cell/border/bottom/color", OnStyleChanged);
RegisterField("/cell/border/left/color", OnStyleChanged);
RegisterField("/cell/border/right/color", OnStyleChanged);
RegisterField("/cell/font/size", OnStyleChanged);
RegisterField("/cell/font/alignment", OnStyleChanged);
RegisterField("/cell/font/richText", OnStyleChanged);
// 预注册常用路径
RegisterField("/cell/bg/color", OnStyleChanged);
RegisterField("/cell/fg/color", OnStyleChanged);
RegisterField("/cell/border/top/color", OnStyleChanged);
RegisterField("/cell/border/bottom/color", OnStyleChanged);
RegisterField("/cell/border/left/color", OnStyleChanged);
RegisterField("/cell/border/right/color", OnStyleChanged);
RegisterField("/cell/font/size", OnStyleChanged);
RegisterField("/cell/font/alignment", OnStyleChanged);
RegisterField("/cell/font/richText", OnStyleChanged);
RegisterField("/cell/font/fontAsset", OnStyleChanged);
m_IsBound = true;
RefreshAll();
}
m_IsBound = true;
RefreshAll();
}
/// <summary>
/// 解绑并停止监听样式。
/// </summary>
public void Unbind()
{
if (!m_IsBound) return;
/// <summary>
/// 解绑并停止监听样式。
/// </summary>
public void Unbind()
{
if (!m_IsBound) return;
foreach (var kvp in m_Fields)
{
if (kvp.Value != null)
{
kvp.Value.Member.OnValueChanged -= OnStyleChanged;
kvp.Value.Unregister();
}
}
m_Fields.Clear();
m_IsBound = false;
}
foreach (var kvp in m_Fields)
{
if (kvp.Value != null)
{
kvp.Value.Member.OnValueChanged -= OnStyleChanged;
kvp.Value.Unregister();
}
}
/// <summary>
/// 刷新全部样式值到组件。
/// </summary>
public void RefreshAll()
{
if (!m_IsBound || string.IsNullOrEmpty(m_StyleNamespace)) return;
m_Fields.Clear();
m_IsBound = false;
}
// 背景颜色 → Image.color
StyleValue bgVal = GetStyleValue("/cell/bg/color");
if (bgVal != null && ImageComponent != null)
ImageComponent.color = bgVal;
/// <summary>
/// 刷新全部样式值到组件。
/// </summary>
public void RefreshAll()
{
if (!m_IsBound || string.IsNullOrEmpty(m_StyleNamespace)) return;
// 前景颜色 → Text.color
StyleValue fgVal = GetStyleValue("/cell/fg/color");
if (fgVal != null && TextComponent != null)
TextComponent.color = fgVal;
// 背景颜色 → Image.color
StyleValue bgVal = GetStyleValue("/cell/bg/color");
if (bgVal != null && ImageComponent != null)
ImageComponent.color = bgVal;
// 字体大小
StyleValue sizeVal = GetStyleValue("/cell/font/size");
if (sizeVal != null && TextComponent != null)
TextComponent.fontSize = (int)sizeVal;
// 前景颜色 → Text.color
StyleValue fgVal = GetStyleValue("/cell/fg/color");
if (fgVal != null && TextComponent != null)
TextComponent.color = fgVal;
// 对齐
StyleValue alignVal = GetStyleValue("/cell/font/alignment");
if (alignVal != null && TextComponent != null)
TextComponent.alignment = alignVal.Convert<TextAlignmentOptions>();
// 字体大小
StyleValue sizeVal = GetStyleValue("/cell/font/size");
if (sizeVal != null && TextComponent != null)
TextComponent.fontSize = (int)sizeVal;
// 富文本
StyleValue richVal = GetStyleValue("/cell/font/richText");
if (richVal != null && TextComponent != null)
TextComponent.richText = (bool)richVal;
}
// 对齐
StyleValue alignVal = GetStyleValue("/cell/font/alignment");
if (alignVal != null && TextComponent != null)
TextComponent.alignment = alignVal.Convert<TextAlignmentOptions>();
#endregion
// 富文本
StyleValue richVal = GetStyleValue("/cell/font/richText");
if (richVal != null && TextComponent != null)
TextComponent.richText = (bool)richVal;
#region 私有方法
// 字体资源
StyleValue fontVal = GetStyleValue("/cell/font/fontAsset");
if (fontVal != null && TextComponent != null)
{
try
{
TextComponent.font = fontVal.GetValueAs<TMP_FontAsset>();
}
catch
{
/* 类型不匹配时忽略 */
}
}
}
private void RegisterField(string path, System.Action<StyleValue> callback)
{
var field = new StyleField
{
Namespace = m_StyleNamespace,
StylePath = path
};
field.Register();
field.Member.OnValueChanged += callback;
m_Fields[path] = field;
}
#endregion
private StyleValue GetStyleValue(string path)
{
return StyleManager.GetValue(m_StyleNamespace, path);
}
#region 私有方法
private void OnStyleChanged(StyleValue val)
{
RefreshAll();
OnStyleChangedCallback?.Invoke();
}
private void RegisterField(string path, System.Action<StyleValue> callback)
{
var field = new StyleField
{
Namespace = m_StyleNamespace,
StylePath = path
};
field.Register();
field.Member.OnValueChanged += callback;
m_Fields[path] = field;
}
#endregion
}
private StyleValue GetStyleValue(string path)
{
return StyleManager.GetValue(m_StyleNamespace, path);
}
/// <summary>
/// 用于 TextAnchor → TextAlignmentOptions 的转换扩展。
/// </summary>
internal static class StyleBoundElementExtensions
{
public static TextAlignmentOptions Convert<T>(this StyleValue val)
{
string s = val?.GetValueAs<string>();
if (string.IsNullOrEmpty(s)) return TextAlignmentOptions.Midline;
private void OnStyleChanged(StyleValue val)
{
RefreshAll();
OnStyleChangedCallback?.Invoke();
}
switch (s)
{
case "UpperLeft": return TextAlignmentOptions.TopLeft;
case "UpperCenter": return TextAlignmentOptions.Top;
case "UpperRight": return TextAlignmentOptions.TopRight;
case "MiddleLeft": return TextAlignmentOptions.MidlineLeft;
case "MiddleCenter": return TextAlignmentOptions.Midline;
case "MiddleRight": return TextAlignmentOptions.MidlineRight;
case "LowerLeft": return TextAlignmentOptions.BottomLeft;
case "LowerCenter": return TextAlignmentOptions.Bottom;
case "LowerRight": return TextAlignmentOptions.BottomRight;
default: return TextAlignmentOptions.Midline;
}
}
}
}
#endregion
}
/// <summary>
/// 用于 TextAnchor → TextAlignmentOptions 的转换扩展。
/// </summary>
internal static class StyleBoundElementExtensions
{
public static TextAlignmentOptions Convert<T>(this StyleValue val)
{
string s = val?.GetValueAs<string>();
if (string.IsNullOrEmpty(s)) return TextAlignmentOptions.Midline;
switch (s)
{
case "UpperLeft": return TextAlignmentOptions.TopLeft;
case "UpperCenter": return TextAlignmentOptions.Top;
case "UpperRight": return TextAlignmentOptions.TopRight;
case "MiddleLeft": return TextAlignmentOptions.MidlineLeft;
case "MiddleCenter": return TextAlignmentOptions.Midline;
case "MiddleRight": return TextAlignmentOptions.MidlineRight;
case "LowerLeft": return TextAlignmentOptions.BottomLeft;
case "LowerCenter": return TextAlignmentOptions.Bottom;
case "LowerRight": return TextAlignmentOptions.BottomRight;
default: return TextAlignmentOptions.Midline;
}
}
}
}
+31 -20
View File
@@ -3,36 +3,47 @@
// 表格默认样式定义
// ==========================================
namespace: xeric_table_default
// ---- 单元格背景 ----
cell/bg/color = (1,1,1,1):UnityEngine.Color @Desc:单元格背景颜色
// ---- 单元格前景 ----
cell/fg/color = (0.1,0.1,0.1,1):UnityEngine.Color @Desc:单元格前景文字颜色
// ---- 边框 ----
cell/border/top/color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:上边框颜色
cell/border/top/width = 1:System.Single @Desc:上边框宽度
cell/border/bottom/color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:下边框颜色
cell/border/bottom/width = 1:System.Single @Desc:下边框宽度
cell/border/left/color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:左边框颜色
cell/border/left/width = 1:System.Single @Desc:左边框宽度
cell/border/right/color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:右边框颜色
cell/border/right/width = 1:System.Single @Desc:右边框宽度
cell/border/top/ {
color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:上边框颜色
width = 1:System.Single @Desc:上边框宽度
}
cell/border/bottom/ {
color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:下边框颜色
width = 1:System.Single @Desc:下边框宽度
}
cell/border/left/ {
color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:左边框颜色
width = 1:System.Single @Desc:左边框宽度
}
cell/border/right/ {
color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:右边框颜色
width = 1:System.Single @Desc:右边框宽度
}
// ---- 选中高亮 ----
cell/selection/bg/color = (0.2,0.5,1,0.3):UnityEngine.Color @Desc:选中单元格高亮颜色
// ---- 表头样式 ----
header/bg/color = (0.9,0.9,0.9,1):UnityEngine.Color @Desc:表头背景颜色
header/fg/color = (0,0,0,1):UnityEngine.Color @Desc:表头文字颜色
// ---- 字体 ----
cell/font/size = 14:System.Int32 @Desc:默认字体大小
cell/font/alignment = MiddleCenter:UnityEngine.TextAnchor @Desc:默认文本对齐
cell/font/richText = true:System.Boolean @Desc:是否启用富文本
cell/font/ {
size = 14:System.Int32 @Desc:默认字体大小
alignment = MiddleCenter:UnityEngine.TextAnchor @Desc:默认文本对齐
richText = true:System.Boolean @Desc:是否启用富文本
fontAsset = guid:68dace674a028544187e7ffe55cdae8b:TMPro.TMP_FontAsset:TMPro.TMP_FontAsset @Desc:字体资源 @ResourceSoftReference:Packages/com.lrss3.xericuiactionvessel/Runtime/Font/SourceHanSansSC-Regular-2 SDF.asset
}
// ---- 行高 / 列宽(缺省值) ----
cell/default/height = 40:System.Single @Desc:默认行高
cell/default/width = 100:System.Single @Desc:默认列宽
cell/default/ {
height = 40:System.Single @Desc:默认行高
width = 100:System.Single @Desc:默认列宽
}