表格: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:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user