表格:Awake 不再死循环;预制体实例里不清子物体;重建前补齐 ScrollRect 骨架
- Play 下 Destroy 是延迟销毁,while (childCount > 0) 同帧永远不减少 ⇒ Awake 出不来。 改成倒序 for + Destroy;编辑模式仍用 DestroyImmediate。 - 预制体实例里的子物体归预制体所有,编辑模式 DestroyImmediate 会抛异常(Awake 与每帧的 孤立清理都会撞)。两处都跳过实例内子物体;后者逐个子物体判归属,且不写 hideFlags。 - ScrollRect 的 content/viewport 为空时 ContentTransform 回落到表自己:内容尺寸被写给表本身 (保存预制体就是那个"奇怪尺寸"),清空还会把表自己的子物体一起清掉。新增 EnsureScrollStructure():补齐 Viewport(RectMask2D)/Content,按名字认领、不重复建; 接在 Awake 与 FullRebuild/FullRebuildPreservingData/ClearTable 之前,另加右键菜单。
This commit is contained in:
@@ -662,6 +662,7 @@ namespace XericUI.XTable.Rendering.Component
|
||||
|
||||
m_Assembler?.DestroyAll();
|
||||
m_PrefabPool?.DestroyAll();
|
||||
EnsureScrollStructure();
|
||||
ClearAllChildren();
|
||||
m_BackgroundRenderer = null;
|
||||
m_BorderRenderer = null;
|
||||
@@ -722,6 +723,7 @@ namespace XericUI.XTable.Rendering.Component
|
||||
|
||||
m_Assembler?.DestroyAll();
|
||||
m_PrefabPool?.DestroyAll();
|
||||
EnsureScrollStructure();
|
||||
ClearAllChildren();
|
||||
m_BackgroundRenderer = null;
|
||||
m_BorderRenderer = null;
|
||||
@@ -809,6 +811,7 @@ namespace XericUI.XTable.Rendering.Component
|
||||
|
||||
m_Assembler?.DestroyAll();
|
||||
m_PrefabPool?.DestroyAll();
|
||||
EnsureScrollStructure();
|
||||
ClearAllChildren();
|
||||
m_BackgroundRenderer = null;
|
||||
m_BorderRenderer = null;
|
||||
|
||||
@@ -57,16 +57,43 @@ namespace XericUI.XTable.Rendering.Component
|
||||
Transform t = ContentTransform;
|
||||
if (t == null) return;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// 编辑器模式下先重置 HideFlags 再销毁,防止 DontSave 对象拒绝销毁
|
||||
while (t.childCount > 0)
|
||||
// ScrollRect 的 Content 为空时 ContentTransform 回落到表自己的 rectTransform:
|
||||
// 这时清空清的是表自己的子物体(Viewport/Content/手摆的装饰),越重建越空。
|
||||
if (m_ScrollRect != null && m_ScrollRect.content == null && ReferenceEquals(t, rectTransform))
|
||||
{
|
||||
var child = t.GetChild(0);
|
||||
child.gameObject.hideFlags = HideFlags.None;
|
||||
if (Application.isPlaying)
|
||||
Debug.LogWarning("[XTable] ScrollRect 的 Content 为空,跳过清空(否则会清掉表自己的子物体):" + name);
|
||||
return;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// 编辑器模式下先重置 HideFlags 再销毁,防止 DontSave 对象拒绝销毁。
|
||||
// Play 下 Destroy 是延迟销毁、同帧 childCount 不会减少——原来这里用 while 会死循环(Awake 出不来)。
|
||||
if (Application.isPlaying)
|
||||
{
|
||||
for (int i = t.childCount - 1; i >= 0; i--)
|
||||
{
|
||||
var child = t.GetChild(i);
|
||||
child.gameObject.hideFlags = HideFlags.None;
|
||||
Destroy(child.gameObject);
|
||||
else
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 预制体自己的子物体(场景实例里的 prefab-owned、以及 Prefab 模式下资产自己的子节点)
|
||||
// 一律不碰:对它们 DestroyImmediate 会抛 InvalidOperationException;而以前只判了
|
||||
// IsPartOfPrefabInstance —— 在 Prefab 模式里该判定为 false,于是会把资产自己的子节点删掉,
|
||||
// 表现为“进预制体一个样、出场景一个样”。这里改成逐个判定(DontSave 的是我们自己生成的,照删),
|
||||
// 两种上下文行为一致。
|
||||
for (int i = t.childCount - 1; i >= 0; i--)
|
||||
{
|
||||
var child = t.GetChild(i);
|
||||
bool ours = (child.hideFlags & HideFlags.DontSave) != 0;
|
||||
if (!ours && (PrefabUtility.IsPartOfPrefabInstance(child.gameObject)
|
||||
|| PrefabUtility.IsPartOfPrefabAsset(child.gameObject)))
|
||||
continue;
|
||||
child.gameObject.hideFlags = HideFlags.None;
|
||||
DestroyImmediate(child.gameObject);
|
||||
}
|
||||
}
|
||||
#else
|
||||
for (int i = t.childCount - 1; i >= 0; i--)
|
||||
|
||||
@@ -309,8 +309,12 @@ namespace XericUI.XTable.Rendering.Component
|
||||
RecalculateTotalSize();
|
||||
UpdateVisibleRect();
|
||||
|
||||
// 确保 ScrollRect Content 尺寸正确
|
||||
RectTransform ctRt = ContentTransform as RectTransform;
|
||||
// 确保 ScrollRect Content 尺寸正确。
|
||||
// 有 ScrollRect 但 Content 为空时 ContentTransform 是表自己:这时写尺寸=把表本身改成
|
||||
// 内容尺寸(预制体一保存就是那个"奇怪的尺寸"),所以先不写。
|
||||
RectTransform ctRt = m_ScrollRect != null && m_ScrollRect.content == null
|
||||
? null
|
||||
: ContentTransform as RectTransform;
|
||||
if (ctRt != null)
|
||||
{
|
||||
float pad2 = m_ContentOffset * 2f;
|
||||
@@ -412,15 +416,40 @@ namespace XericUI.XTable.Rendering.Component
|
||||
private void CleanOrphanChildren()
|
||||
{
|
||||
Transform contentT = ContentTransform;
|
||||
Transform borderT = contentT?.Find("_TableBorders");
|
||||
if (contentT == null) return;
|
||||
// 骨架缺失时 ContentTransform 回落到表自己:这时"清理孤立子物体"清的是
|
||||
// 表自己的子物体(Viewport/Content),越清越空 ⇒ 一个都不清。
|
||||
if (m_ScrollRect != null && m_ScrollRect.content == null && ReferenceEquals(contentT, rectTransform))
|
||||
return;
|
||||
Transform borderT = contentT.Find("_TableBorders");
|
||||
|
||||
for (int i = contentT.childCount - 1; i >= 0; i--)
|
||||
{
|
||||
var child = contentT.GetChild(i);
|
||||
// 跳过池管理的对象(标记有 POOL_FLAGS = DontSave | HideInHierarchy)
|
||||
if ((child.hideFlags & HideFlags.DontSave) != 0) continue;
|
||||
// 池管理的对象(POOL_FLAGS = DontSave | HideInHierarchy)照旧跳过;
|
||||
// 但历史上叠出来的重复背景要清掉:背景缓存是 [NonSerialized],域重载后必丢,
|
||||
// 而 DontSave 的 _TableBackground 还活在场景里,不清理就会一层层叠加
|
||||
// (旧层按旧尺寸留在原位、且 HideInHierarchy 看不见,表现为边框错位/内容被盖)。
|
||||
if ((child.hideFlags & HideFlags.DontSave) != 0)
|
||||
{
|
||||
if (child.name == "_TableBackground" &&
|
||||
(m_BackgroundRenderer == null || child.gameObject != m_BackgroundRenderer.gameObject))
|
||||
{
|
||||
child.gameObject.hideFlags = HideFlags.None;
|
||||
if (Application.isPlaying) Destroy(child.gameObject);
|
||||
else DestroyImmediate(child.gameObject);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (m_BackgroundRenderer != null && child.gameObject == m_BackgroundRenderer.gameObject) continue;
|
||||
if (borderT != null && child == borderT) continue;
|
||||
// 预制体自己的子物体归预制体所有,编辑模式 DestroyImmediate 会抛 InvalidOperationException。
|
||||
// 实例和 Prefab 模式(资产)两种上下文都要跳过,否则“进去一个样、出来一个样”。
|
||||
#if UNITY_EDITOR
|
||||
if (!Application.isPlaying &&
|
||||
(UnityEditor.PrefabUtility.IsPartOfPrefabInstance(child.gameObject)
|
||||
|| UnityEditor.PrefabUtility.IsPartOfPrefabAsset(child.gameObject))) continue;
|
||||
#endif
|
||||
|
||||
child.gameObject.hideFlags = HideFlags.None;
|
||||
if (Application.isPlaying)
|
||||
@@ -434,14 +463,38 @@ namespace XericUI.XTable.Rendering.Component
|
||||
|
||||
#region 背景创建
|
||||
|
||||
/// <summary>运行时渲染子物体的统一标记:不存盘、隐藏,且在编辑器里不可编辑(避免被记成预制体改动)。</summary>
|
||||
private const HideFlags RuntimeRendererFlags = HideFlags.DontSave | HideFlags.HideInHierarchy | HideFlags.NotEditable;
|
||||
|
||||
private void CreateBackground()
|
||||
{
|
||||
if (m_BackgroundRenderer != null) return;
|
||||
Transform contentT = ContentTransform;
|
||||
if (m_BackgroundRenderer == null && contentT != null)
|
||||
{
|
||||
// 域重载会清空 [NonSerialized] 缓存,但 DontSave 的 _TableBackground 还活在场景里。
|
||||
// 先收养它,否则每次重载都会再建一层,旧层按旧尺寸留在原位并盖住内容。
|
||||
var existing = contentT.Find("_TableBackground");
|
||||
if (existing != null)
|
||||
{
|
||||
m_BackgroundRenderer = existing.GetComponent<XTableBackgroundRenderer>();
|
||||
if (m_BackgroundRenderer != null)
|
||||
{
|
||||
existing.gameObject.hideFlags = RuntimeRendererFlags;
|
||||
var adoptedSelection = existing.GetComponent<XTableSelectionInput>();
|
||||
if (adoptedSelection != null) adoptedSelection.Configure(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (m_BackgroundRenderer != null)
|
||||
{
|
||||
UpdateBackground();
|
||||
return;
|
||||
}
|
||||
|
||||
var backgroundGo = new GameObject("_TableBackground",
|
||||
typeof(RectTransform), typeof(CanvasRenderer), typeof(XTableBackgroundRenderer),
|
||||
typeof(XTableSelectionInput));
|
||||
backgroundGo.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy;
|
||||
backgroundGo.hideFlags = RuntimeRendererFlags;
|
||||
backgroundGo.transform.SetParent(ContentTransform, false);
|
||||
backgroundGo.transform.SetAsFirstSibling();
|
||||
|
||||
|
||||
@@ -89,6 +89,13 @@ namespace XericUI.XTable.Rendering.Component
|
||||
|
||||
[System.NonSerialized] private XTableBackgroundRenderer m_BackgroundRenderer;
|
||||
|
||||
/// <summary>
|
||||
/// content / 冻结容器这些“预制体自己的 RectTransform”由本组件驱动。
|
||||
/// 走 DrivenRectTransformTracker 写入的几何不会落序列化、也不会被记成预制体修改;
|
||||
/// 直接赋值则编辑模式每次刷新都会把表尺寸写进预制体(表现为“预制体坐标尺寸自己变了”)。
|
||||
/// </summary>
|
||||
[System.NonSerialized] private DrivenRectTransformTracker m_LayoutDriven;
|
||||
|
||||
/// <summary>预制体对象池——按原型分组管理</summary>
|
||||
[System.NonSerialized] private PrefabPrototypePool m_PrefabPool;
|
||||
|
||||
@@ -205,6 +212,85 @@ 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
|
||||
{
|
||||
@@ -267,6 +353,10 @@ namespace XericUI.XTable.Rendering.Component
|
||||
// 预加载 Resources 中所有 .xsss 样式表到 StyleManager(仅首次)
|
||||
PreloadAllStyleSheets();
|
||||
|
||||
// ScrollRect 的 content/viewport 丢了就先补骨架:否则下面的清空会把表自己的子物体
|
||||
// 当成残留清掉(越重建越空),尺寸也会写到表自己身上。
|
||||
EnsureScrollStructure();
|
||||
|
||||
// 域重载后旧子对象可能残留在场景中(DontSave 不自动销毁),先清空再重建
|
||||
ClearAllChildren();
|
||||
|
||||
@@ -333,6 +423,8 @@ namespace XericUI.XTable.Rendering.Component
|
||||
protected override void OnDisable()
|
||||
{
|
||||
base.OnDisable();
|
||||
// 驱动属性必须在停用时显式清掉,否则 Unity 会报 tracker 里残留已销毁对象。
|
||||
m_LayoutDriven.Clear();
|
||||
}
|
||||
|
||||
protected override void OnDestroy()
|
||||
|
||||
Reference in New Issue
Block a user