表格:编辑器模式不再把运行时布局写成预制体覆盖

- 生成物 hideFlags 增加 NotEditable:编辑期的自动改动不会被记成预制体覆盖。
- 域重载后按名字收养已存在的 _TableBackground/_TableBorders(普通字段为 null,DontSave 的
  对象还活着),否则每次重载都会再叠一层。
- content 与冻结容器的尺寸/位置改用 DrivenRectTransformTracker 写入,值每次重算、不落序列化。
This commit is contained in:
2026-11-13 15:19:50 +08:00
parent d861ea60fc
commit e23150bfda
4 changed files with 32 additions and 5 deletions
@@ -62,18 +62,32 @@ namespace XericUI.XTable.Rendering.Component
public void CreateBackground(float width, float height, string styleNamespace, Vector2 position = default)
{
if (m_BackgroundGo == null && m_Parent != null)
{
// 背景缓存是普通字段,域重载后为 null;容器里 DontSave 的 _TableBackground 还活着。
// 三个冻结渲染器各有自己的容器,按名字收养是安全的;不收养每次重载都会多叠一层。
var existing = m_Parent.Find("_TableBackground");
if (existing != null) m_BackgroundGo = existing.gameObject;
}
if (m_BackgroundGo == null)
{
m_BackgroundGo = new GameObject("_TableBackground", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image), typeof(XTableSelectionInput));
m_BackgroundGo.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy;
// NotEditable:编辑期改它不会被记成预制体覆盖。
m_BackgroundGo.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy | HideFlags.NotEditable;
m_BackgroundGo.transform.SetParent(m_Parent, false);
m_BackgroundGo.transform.SetAsFirstSibling();
var image = m_BackgroundGo.GetComponent<Image>();
}
// 收养来的对象也要重新配置一次,颜色/选择输入不能沿用上一次的。
var image = m_BackgroundGo.GetComponent<Image>();
if (image != null)
{
image.raycastTarget = true;
m_BackgroundGo.GetComponent<XTableSelectionInput>().Configure(SelectionTable);
image.color = GetStyleColor?.Invoke(styleNamespace, XericUIActionTable.STYLE_CELL_BG_COLOR,
XericUIActionTable.BG_COLOR_FALLBACK) ?? XericUIActionTable.BG_COLOR_FALLBACK;
}
var selectionInput = m_BackgroundGo.GetComponent<XTableSelectionInput>();
if (selectionInput != null) selectionInput.Configure(SelectionTable);
var rt = m_BackgroundGo.GetComponent<RectTransform>();
rt.anchorMin = new Vector2(0, 1);
rt.anchorMax = new Vector2(0, 1);
@@ -35,12 +35,17 @@ namespace XericUI.XTable.Rendering.Component
if (existing != null)
{
m_BorderRenderer = existing.GetComponent<UILineRendererV2>();
if (m_BorderRenderer != null) return m_BorderRenderer;
if (m_BorderRenderer != null)
{
// 顺带自愈旧对象的标记:运行期渲染物一律不可编辑,编辑期改动才不会被记成预制体覆盖。
existing.gameObject.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy | HideFlags.NotEditable;
return m_BorderRenderer;
}
}
var go = new GameObject("_TableBorders",
typeof(RectTransform), typeof(CanvasRenderer), typeof(UILineRendererV2));
go.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy;
go.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy | HideFlags.NotEditable;
go.transform.SetParent(contentT, false);
RectTransform rt = go.GetComponent<RectTransform>();
@@ -96,8 +96,13 @@ namespace XericUI.XTable.Rendering.Component
float pad = m_ContentOffset * 2f;
if (m_ScrollRect != null && m_ScrollRect.content != null)
{
// content 属于预制体:用驱动属性写入,值每次刷新重算、不落序列化,
// 编辑模式也就不会把表尺寸记成预制体修改。
m_LayoutDriven.Add(this, m_ScrollRect.content, DrivenTransformProperties.SizeDelta);
m_ScrollRect.content.sizeDelta = new Vector2(
m_TotalWidth + pad, m_TotalHeight + pad);
}
}
private void UpdateVisibleRect()
@@ -215,6 +215,9 @@ namespace XericUI.XTable.Rendering.Component
// 偏移 m_ContentOffset(边框半宽)以对齐 Content 中单元格的偏移,
// 避免冻结区域左侧/顶部的 1px 边框因 0.5px 坐标差被 viewport 裁切。
float pad = m_ContentOffset * 2f;
// 冻结容器属于预制体:用驱动属性写入,编辑期不会把布局记成预制体修改。
m_LayoutDriven.Add(this, container,
DrivenTransformProperties.AnchoredPosition | DrivenTransformProperties.SizeDelta);
container.anchoredPosition = position + new Vector2(m_ContentOffset, -m_ContentOffset);
container.sizeDelta = new Vector2(m_TotalWidth + pad, m_TotalHeight + pad);
}