表格: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:
2026-11-13 15:19:45 +08:00
parent 624c5ca009
commit d861ea60fc
4 changed files with 189 additions and 14 deletions
@@ -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--)