表格: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:
@@ -662,7 +662,7 @@ namespace XericUI.XTable.Rendering.Component
|
||||
|
||||
m_Assembler?.DestroyAll();
|
||||
m_PrefabPool?.DestroyAll();
|
||||
EnsureScrollStructure();
|
||||
EnsureScrollTemplate();
|
||||
ClearAllChildren();
|
||||
m_BackgroundRenderer = null;
|
||||
m_BorderRenderer = null;
|
||||
@@ -723,7 +723,7 @@ namespace XericUI.XTable.Rendering.Component
|
||||
|
||||
m_Assembler?.DestroyAll();
|
||||
m_PrefabPool?.DestroyAll();
|
||||
EnsureScrollStructure();
|
||||
EnsureScrollTemplate();
|
||||
ClearAllChildren();
|
||||
m_BackgroundRenderer = null;
|
||||
m_BorderRenderer = null;
|
||||
@@ -811,7 +811,7 @@ namespace XericUI.XTable.Rendering.Component
|
||||
|
||||
m_Assembler?.DestroyAll();
|
||||
m_PrefabPool?.DestroyAll();
|
||||
EnsureScrollStructure();
|
||||
EnsureScrollTemplate();
|
||||
ClearAllChildren();
|
||||
m_BackgroundRenderer = null;
|
||||
m_BorderRenderer = null;
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using XericUI.Core;
|
||||
|
||||
namespace XericUI.XTable.Rendering.Component
|
||||
{
|
||||
/// <summary>
|
||||
/// ScrollRect 模板:Viewport / Content / 横竖滚动条全部由表格自己生成,统一标记
|
||||
/// <c>DontSave | NotEditable</c>。结构照右键菜单「生成表格」(<c>CreateTable</c>)那一套。
|
||||
///
|
||||
/// <para><b>为什么整块都要管</b>(2026-11-13 现场):这几个节点以前是手摆进预制体的。
|
||||
/// 保存预制体时它们把运行时的尺寸/布局一起存进去(尺寸被写歪);把表格组件从预制体里
|
||||
/// 删掉之后,content 连同里面的渲染器还留在场景里。改成生成物之后:预制体里不再有它们的
|
||||
/// 副本,组件销毁时由 <see cref="DestroyScrollTemplate"/> 一并收掉。</para>
|
||||
/// </summary>
|
||||
public partial class XericUIActionTable
|
||||
{
|
||||
/// <summary>模板生成物的统一标记:不保存、不进编辑。</summary>
|
||||
private const HideFlags TemplateFlags = HideFlags.DontSave | HideFlags.NotEditable;
|
||||
|
||||
[NonSerialized] private RectTransform m_TemplateViewport;
|
||||
[NonSerialized] private RectTransform m_TemplateContent;
|
||||
[NonSerialized] private Scrollbar m_TemplateHBar;
|
||||
[NonSerialized] private Scrollbar m_TemplateVBar;
|
||||
|
||||
/// <summary>
|
||||
/// 补齐 ScrollRect 模板。缺什么补什么;手摆的旧节点按名字认领后就地改成生成物
|
||||
/// (下一次保存预制体时它们会从文件里消失,之后每次加载由这里重建)。
|
||||
/// </summary>
|
||||
[ContextMenu(XericRegistrationPaths.Table + "/补齐 ScrollRect 骨架")]
|
||||
public void EnsureScrollTemplate()
|
||||
{
|
||||
if (m_ScrollRect == null) return;
|
||||
var self = rectTransform;
|
||||
if (self == null) return;
|
||||
|
||||
RectTransform viewport = EnsureViewport(self);
|
||||
RectTransform content = EnsureContent(viewport);
|
||||
Scrollbar hBar = EnsureScrollbar(self, true);
|
||||
Scrollbar vBar = EnsureScrollbar(self, false);
|
||||
|
||||
if (m_ScrollRect.viewport != viewport) m_ScrollRect.viewport = viewport;
|
||||
if (m_ScrollRect.content != content) m_ScrollRect.content = content;
|
||||
if (m_ScrollRect.horizontalScrollbar != hBar) m_ScrollRect.horizontalScrollbar = hBar;
|
||||
if (m_ScrollRect.verticalScrollbar != vBar) m_ScrollRect.verticalScrollbar = vBar;
|
||||
m_ScrollRect.horizontalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHide;
|
||||
m_ScrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHide;
|
||||
m_ScrollRect.movementType = ScrollRect.MovementType.Clamped;
|
||||
|
||||
m_TemplateViewport = viewport;
|
||||
m_TemplateContent = content;
|
||||
m_TemplateHBar = hBar;
|
||||
m_TemplateVBar = vBar;
|
||||
}
|
||||
|
||||
/// <summary>销毁模板(组件被销毁时调用;只收自己生成的这一份)。</summary>
|
||||
private void DestroyScrollTemplate()
|
||||
{
|
||||
m_TemplateContent = null;
|
||||
DestroyGenerated(m_TemplateViewport);
|
||||
m_TemplateViewport = null;
|
||||
DestroyGenerated(m_TemplateHBar != null ? m_TemplateHBar.gameObject : null);
|
||||
m_TemplateHBar = null;
|
||||
DestroyGenerated(m_TemplateVBar != null ? m_TemplateVBar.gameObject : null);
|
||||
m_TemplateVBar = null;
|
||||
|
||||
if (m_ScrollRect != null)
|
||||
{
|
||||
m_ScrollRect.content = null;
|
||||
m_ScrollRect.viewport = null;
|
||||
m_ScrollRect.horizontalScrollbar = null;
|
||||
m_ScrollRect.verticalScrollbar = null;
|
||||
}
|
||||
}
|
||||
|
||||
private RectTransform EnsureViewport(Transform self)
|
||||
{
|
||||
RectTransform vrt = m_ScrollRect.viewport;
|
||||
if (vrt == null)
|
||||
{
|
||||
Transform found = self.Find("Viewport");
|
||||
if (found != null) vrt = found as RectTransform;
|
||||
}
|
||||
if (vrt == null)
|
||||
{
|
||||
var go = new GameObject("Viewport", typeof(RectTransform), typeof(Image), typeof(Mask));
|
||||
vrt = go.GetComponent<RectTransform>();
|
||||
vrt.SetParent(self, false);
|
||||
go.GetComponent<Image>().color = new Color(1f, 1f, 1f, 0.01f);
|
||||
Debug.Log("[XTable] 补 Viewport:" + name);
|
||||
}
|
||||
|
||||
vrt.anchorMin = Vector2.zero;
|
||||
vrt.anchorMax = Vector2.one;
|
||||
vrt.offsetMin = Vector2.zero;
|
||||
vrt.offsetMax = Vector2.zero;
|
||||
MarkGenerated(vrt.gameObject);
|
||||
return vrt;
|
||||
}
|
||||
|
||||
private RectTransform EnsureContent(RectTransform viewport)
|
||||
{
|
||||
RectTransform crt = m_ScrollRect.content as RectTransform;
|
||||
if (crt == null)
|
||||
{
|
||||
Transform found = viewport != null ? viewport.Find("Content") : transform.Find("Content");
|
||||
if (found != null) crt = found as RectTransform;
|
||||
}
|
||||
if (crt == null)
|
||||
{
|
||||
var go = new GameObject("Content", typeof(RectTransform));
|
||||
crt = go.GetComponent<RectTransform>();
|
||||
crt.SetParent(viewport != null ? (Transform)viewport : transform, false);
|
||||
crt.sizeDelta = new Vector2(800f, 600f);
|
||||
Debug.Log("[XTable] 补 Content:" + name);
|
||||
}
|
||||
|
||||
crt.anchorMin = new Vector2(0f, 1f);
|
||||
crt.anchorMax = new Vector2(0f, 1f);
|
||||
crt.pivot = new Vector2(0f, 1f);
|
||||
crt.anchoredPosition = Vector2.zero;
|
||||
MarkGenerated(crt.gameObject);
|
||||
return crt;
|
||||
}
|
||||
|
||||
private Scrollbar EnsureScrollbar(Transform self, bool horizontal)
|
||||
{
|
||||
string barName = horizontal ? "Scrollbar Horizontal" : "Scrollbar Vertical";
|
||||
Scrollbar bar = horizontal ? m_ScrollRect.horizontalScrollbar : m_ScrollRect.verticalScrollbar;
|
||||
if (bar == null)
|
||||
{
|
||||
Transform found = self.Find(barName);
|
||||
if (found != null) bar = found.GetComponent<Scrollbar>();
|
||||
}
|
||||
|
||||
if (bar == null)
|
||||
{
|
||||
var go = new GameObject(barName, typeof(RectTransform), typeof(Scrollbar), typeof(Image));
|
||||
bar = go.GetComponent<Scrollbar>();
|
||||
go.GetComponent<Image>().color = new Color(0.15f, 0.15f, 0.15f, 0.5f);
|
||||
|
||||
var sliding = new GameObject("Sliding Area", typeof(RectTransform));
|
||||
sliding.transform.SetParent(go.transform, false);
|
||||
var handle = new GameObject("Handle", typeof(RectTransform), typeof(Image));
|
||||
handle.transform.SetParent(sliding.transform, false);
|
||||
handle.GetComponent<Image>().color = new Color(0.4f, 0.4f, 0.4f, 0.8f);
|
||||
|
||||
RectTransform slidingRt = (RectTransform)sliding.transform;
|
||||
slidingRt.anchorMin = Vector2.zero;
|
||||
slidingRt.anchorMax = Vector2.one;
|
||||
slidingRt.sizeDelta = horizontal ? new Vector2(-20f, 0f) : new Vector2(0f, -20f);
|
||||
|
||||
RectTransform handleRt = (RectTransform)handle.transform;
|
||||
handleRt.anchorMin = Vector2.zero;
|
||||
handleRt.anchorMax = Vector2.one;
|
||||
handleRt.sizeDelta = new Vector2(-10f, -10f);
|
||||
bar.handleRect = handleRt;
|
||||
|
||||
bar.transform.SetParent(self, false);
|
||||
Debug.Log("[XTable] 补 " + barName + ":" + name);
|
||||
}
|
||||
|
||||
RectTransform rt = (RectTransform)bar.transform;
|
||||
if (horizontal)
|
||||
{
|
||||
rt.anchorMin = new Vector2(0f, 0f);
|
||||
rt.anchorMax = new Vector2(1f, 0f);
|
||||
rt.pivot = new Vector2(0.5f, 0f);
|
||||
rt.anchoredPosition = new Vector2(0f, 4f);
|
||||
rt.sizeDelta = new Vector2(-16f, 20f);
|
||||
bar.direction = Scrollbar.Direction.LeftToRight;
|
||||
}
|
||||
else
|
||||
{
|
||||
rt.anchorMin = new Vector2(1f, 0f);
|
||||
rt.anchorMax = new Vector2(1f, 1f);
|
||||
rt.pivot = new Vector2(1f, 0.5f);
|
||||
rt.anchoredPosition = new Vector2(-4f, 0f);
|
||||
rt.sizeDelta = new Vector2(20f, -16f);
|
||||
bar.direction = Scrollbar.Direction.BottomToTop;
|
||||
}
|
||||
|
||||
MarkGenerated(bar.gameObject);
|
||||
return bar;
|
||||
}
|
||||
|
||||
/// <summary>把生成物及其子物体统一标记为不保存、不进编辑。</summary>
|
||||
private static void MarkGenerated(GameObject go)
|
||||
{
|
||||
if (go == null) return;
|
||||
go.hideFlags = TemplateFlags;
|
||||
Transform t = go.transform;
|
||||
for (int i = 0; i < t.childCount; i++)
|
||||
MarkGenerated(t.GetChild(i).gameObject);
|
||||
}
|
||||
|
||||
private static void DestroyGenerated(UnityEngine.Object target)
|
||||
{
|
||||
if (target == null) return;
|
||||
#if UNITY_EDITOR
|
||||
// 预制体实例里的对象不许立即销毁(同 Awake 那个坑);它们本来就不保存,重载后自然不在。
|
||||
if (!Application.isPlaying && UnityEditor.PrefabUtility.IsPartOfPrefabInstance(target))
|
||||
return;
|
||||
if (!Application.isPlaying)
|
||||
{
|
||||
UnityEngine.Object.DestroyImmediate(target);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
UnityEngine.Object.Destroy(target);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bf00467a8a0e4a398c67f80b4b343e42
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user