表格:ScrollRect 模板(Viewport/Content/滚动条)改成生成物

结构照右键菜单「生成表格」:Viewport(RectTransform+Image+Mask) → Content(anchor/pivot 0,1)
→ 横/竖滚动条(Sliding Area + Handle)→ 接线(AutoHide + Clamped)。区别是全部节点标
DontSave | NotEditable:
- 预制体里不再存它们的副本,保存时不会把运行时的尺寸/布局一起存进去;
- 组件删掉时 OnDestroy 调 DestroyScrollTemplate 一并收掉,content 不会带着渲染器留在场景里;
- 旧的手摆节点按名字认领后就地改成生成物,下一次保存预制体时从文件里消失。

Awake 与三个重建入口(FullRebuild / FullRebuildPreservingData / ClearTable)都先补齐模板
再清空子物体。
This commit is contained in:
2026-11-14 10:28:05 +08:00
parent d8ada6fdc1
commit 9efd0aebd9
4 changed files with 233 additions and 83 deletions
@@ -212,85 +212,6 @@ namespace XericUI.XTable.Rendering.Component
}
}
/// <summary>
/// 补齐 ScrollRect 的骨架(Viewport + Content)。
///
/// <para><b>为什么要在重建之前补</b>(2026-11-13 现场):ScrollRect 还在、但它的
/// content/viewport 引用空了的时候,<see cref="ContentTransform"/> 会回落到表自己的
/// rectTransform ⇒ ① 表格把自己的尺寸写成内容尺寸(预制体一保存就是那个"奇怪的尺寸");
/// ② 重建时的清空清的是表自己的子物体,把原来的 Viewport/Content 一起清掉,越修越空。
/// 骨架回来了,这两件事都不会再发生。</para>
///
/// <para>只补缺的那一段;按名字找得到就认领它(手摆的 Viewport/Content 不会被换成新的)。</para>
/// </summary>
[ContextMenu(XericRegistrationPaths.Table + "/补齐 ScrollRect 骨架")]
public void EnsureScrollStructure()
{
if (m_ScrollRect == null) return;
var self = rectTransform;
if (self == null) return;
bool changed = false;
if (m_ScrollRect.viewport == null)
{
Transform viewportT = self.Find("Viewport");
if (viewportT == null)
{
viewportT = new GameObject("Viewport", typeof(RectTransform)).transform;
viewportT.SetParent(self, false);
}
var vrt = viewportT as RectTransform;
if (vrt != null)
{
vrt.anchorMin = Vector2.zero;
vrt.anchorMax = Vector2.one;
vrt.offsetMin = Vector2.zero;
vrt.offsetMax = Vector2.zero;
}
if (viewportT.GetComponent<RectMask2D>() == null && viewportT.GetComponent<Mask>() == null)
viewportT.gameObject.AddComponent<RectMask2D>();
m_ScrollRect.viewport = vrt;
changed = true;
Debug.Log("[XTable] ScrollRect 缺 Viewport,已补齐:" + name);
}
if (m_ScrollRect.content == null)
{
Transform parent = m_ScrollRect.viewport != null ? (Transform)m_ScrollRect.viewport : self;
Transform contentT = parent.Find("Content");
if (contentT == null && parent != self)
contentT = self.Find("Content");
if (contentT == null)
{
contentT = new GameObject("Content", typeof(RectTransform)).transform;
contentT.SetParent(parent, false);
}
var crt = contentT as RectTransform;
if (crt != null)
{
crt.anchorMin = new Vector2(0f, 1f);
crt.anchorMax = new Vector2(0f, 1f);
crt.pivot = new Vector2(0f, 1f);
crt.anchoredPosition = Vector2.zero;
crt.sizeDelta = Vector2.zero;
}
m_ScrollRect.content = crt;
changed = true;
Debug.Log("[XTable] ScrollRect 缺 Content,已补齐:" + name);
}
#if UNITY_EDITOR
if (changed && !Application.isPlaying)
EditorUtility.SetDirty(m_ScrollRect);
#endif
}
/// <summary>表格全局配置资源</summary>
public XTableConfig Config
{
@@ -355,7 +276,7 @@ namespace XericUI.XTable.Rendering.Component
// ScrollRect 的 content/viewport 丢了就先补骨架:否则下面的清空会把表自己的子物体
// 当成残留清掉(越重建越空),尺寸也会写到表自己身上。
EnsureScrollStructure();
EnsureScrollTemplate();
// 域重载后旧子对象可能残留在场景中(DontSave 不自动销毁),先清空再重建
ClearAllChildren();
@@ -447,6 +368,10 @@ namespace XericUI.XTable.Rendering.Component
DestroyFrozenRenderers();
DestroySelectionOverlay(ref m_NormalSelectionOverlay);
// ScrollRect 模板是我们生成的:组件删掉时连同 Viewport/Content/滚动条一起收掉,
// 否则 content 会带着里面的渲染器留在场景里(2026-11-13 现场)。
DestroyScrollTemplate();
base.OnDestroy();
}