Files
XericUIActionVessel/Runtime/XTable/Rendering/Component/XericUIActionTable.ScrollTemplate.cs
T
lrss3 2cb96324c1 表格:自动生成开着时滚动条一律接管(灰色不可编辑),关掉时解锁成可编辑
之前只给"新建或本来就 DontSave"的滚动条打标记,预制体里存下来的旧副本被当成
用户自己的对象,所以 Hierarchy 里一直是白色可编辑。现在:
- 自动生成开:两根条一律 MarkGenerated(DontSave|NotEditable,名字变灰);
- 自动生成关:Unlock 去掉标记、保留 ScrollRect 绑定,直接交给用户改样式/换绑定。
2026-12-03 13:48:35 +08:00

237 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using UnityEngine;
using UnityEngine.UI;
using XericUI.Core;
namespace XericUI.XTable.Rendering.Component
{
/// <summary>
/// ScrollRect 模板:Viewport / Content 永远维护;横竖滚动条由「自动生成滚动条」开关控制,
/// 几何与外观全部来自样式表(XTableScrollbarFactory)。结构照右键菜单「生成表格」那一套。
/// 生成物统一标 <c>DontSave | NotEditable</c>(Hierarchy 里名字是灰的)。
///
/// <para><b>为什么整块都要管</b>(2026-11-13 现场):这几个节点以前是手摆进预制体的。
/// 保存预制体时它们把运行时的尺寸/布局一起存进去(尺寸被写歪);把表格组件从预制体里
/// 删掉之后,content 连同里面的渲染器还留在场景里。改成生成物之后:预制体里不再有它们的
/// 副本,组件销毁时由 <see cref="DestroyScrollTemplate"/> 一并收掉。</para>
/// </summary>
public partial class XericUIActionTable
{
/// <summary>模板生成物:不保存 + 不可编辑(Hierarchy 里名字是灰的,一眼看出它不是资产)。</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>内容区左上角是否已按尺寸钉过一次;补齐模板/重建时清零,让下一次刷新重新钉。</summary>
[NonSerialized] private bool m_ContentPinned;
[NonSerialized] private XTableScrollbarFactory.Style m_LastScrollbarStyle;
[NonSerialized] private bool m_HasScrollbarStyle;
/// <summary>
/// 补齐 ScrollRect 模板。缺什么补什么;手摆的旧节点按名字认领后就地改成生成物
/// (下一次保存预制体时它们会从文件里消失,之后每次加载由这里重建)。
/// </summary>
[ContextMenu(XericRegistrationPaths.Table + "/补齐 ScrollRect 骨架")]
public void EnsureScrollTemplate()
{
if (m_ScrollRect == null) return;
var self = rectTransform;
if (self == null) return;
// ScrollRect 的内容模板永远维护(Viewport/Content);唯一开放给用户的是滚动条。
RectTransform viewport = EnsureViewport(self);
RectTransform content = EnsureContent(viewport);
if (m_ScrollRect.viewport != viewport) m_ScrollRect.viewport = viewport;
if (m_ScrollRect.content != content) m_ScrollRect.content = content;
m_ScrollRect.movementType = ScrollRect.MovementType.Clamped;
Scrollbar hBar = null, vBar = null;
if (m_AutoScrollbars)
{
var style = ResolveScrollbarStyle();
hBar = XTableScrollbarFactory.Ensure(self, m_ScrollRect, true, style);
vBar = XTableScrollbarFactory.Ensure(self, m_ScrollRect, false, style);
m_LastScrollbarStyle = style;
m_HasScrollbarStyle = true;
m_ScrollRect.horizontalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHide;
m_ScrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHide;
}
else
{
// 关掉自动生成:把两根条解锁成可编辑/可保存(绑定保留),从这里交给用户。
XTableScrollbarFactory.Unlock(m_ScrollRect.horizontalScrollbar);
XTableScrollbarFactory.Unlock(m_ScrollRect.verticalScrollbar);
m_HasScrollbarStyle = false;
}
m_TemplateViewport = viewport;
m_TemplateContent = content;
m_TemplateHBar = hBar;
m_TemplateVBar = vBar;
// 下一次 RefreshView 把左上角按尺寸重新钉一次(生成的内容先小后大,ScrollRect 会夹走位置)。
m_ContentPinned = false;
}
/// <summary>样式热更:值变了才把几何/配色写进两根滚动条(不重建对象)。</summary>
private void SyncScrollbarStyle()
{
if (!m_AutoScrollbars || m_ScrollRect == null) return;
var style = ResolveScrollbarStyle();
if (m_HasScrollbarStyle && SameScrollbarStyle(m_LastScrollbarStyle, style)) return;
m_LastScrollbarStyle = style;
m_HasScrollbarStyle = true;
if (m_ScrollRect.horizontalScrollbar != null)
XTableScrollbarFactory.Apply(m_ScrollRect.horizontalScrollbar, true, style);
if (m_ScrollRect.verticalScrollbar != null)
XTableScrollbarFactory.Apply(m_ScrollRect.verticalScrollbar, false, style);
}
private static bool SameScrollbarStyle(in XTableScrollbarFactory.Style a, in XTableScrollbarFactory.Style b)
{
return Mathf.Approximately(a.Thickness, b.Thickness)
&& Mathf.Approximately(a.EdgeStart, b.EdgeStart)
&& Mathf.Approximately(a.EdgeEnd, b.EdgeEnd)
&& Mathf.Approximately(a.Side, b.Side)
&& Mathf.Approximately(a.HandlePadding, b.HandlePadding)
&& a.BarColor == b.BarColor
&& a.HandleColor == b.HandleColor
&& a.BarSprite == b.BarSprite
&& a.HandleSprite == b.HandleSprite;
}
/// <summary>
/// 逐项对齐生成物:<b>存在就复用</b>(各自会重置 rect 与内容),不存在才创建。
///
/// <para>替代"先清空再重建":编辑模式下预制体实例里的对象不能 DestroyImmediate,
/// 手工删过之后池里还会留下已销毁的引用(MissingReferenceException)。逐项对齐不会
/// 碰任何不该碰的东西,重复调用也是幂等的。</para>
/// </summary>
private void ReconcileGeneratedHierarchy()
{
EnsureScrollTemplate();
CreateBackground();
GetOrCreateBorderRenderer();
CreateViewportRenderer();
}
/// <summary>销毁模板(组件被销毁时调用;只收自己生成的这一份)。</summary>
private void DestroyScrollTemplate()
{
m_TemplateContent = null;
XTableScrollbarFactory.DestroyGenerated(m_TemplateViewport);
m_TemplateViewport = null;
XTableScrollbarFactory.DestroyGenerated(m_TemplateHBar != null ? m_TemplateHBar.gameObject : null);
m_TemplateHBar = null;
XTableScrollbarFactory.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;
XTableScrollbarFactory.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;
XTableScrollbarFactory.MarkGenerated(crt.gameObject);
return crt;
}
/// <summary>
/// 把生成物的 rect 钉成"锚点左上 + 指定锚位",只在值不同的时候写。
///
/// <para>生成物可能是域重载后认领回来的旧对象(<c>_TableBackground</c> / <c>_TableBorders</c>),
/// 它们的锚点/锚位还是上一版留下的——只改 sizeDelta 就会画出"以父组件中心为左上角"的错位。</para>
/// </summary>
private static void PinTopLeftRect(RectTransform rt, Vector2 position)
{
if (rt == null) return;
Vector2 topLeft = new Vector2(0f, 1f);
if (rt.anchorMin != topLeft) rt.anchorMin = topLeft;
if (rt.anchorMax != topLeft) rt.anchorMax = topLeft;
if (rt.pivot != topLeft) rt.pivot = topLeft;
if (rt.anchoredPosition != position) rt.anchoredPosition = position;
}
/// <summary>
/// 图元渲染器(背景/边框)专用:它们的子块铺满本渲染器且轴心 0.5,顶点原点=rect 的<b>中心</b>。
/// 所以让这个 rect 的"中心"落在左上角(pivot 0.5 + 锚点 (0,1) + 锚位 0),
/// 绘图代码就能继续用左上角坐标,和文本(UGUI 左上角语义)对得上。
/// </summary>
private static void PinPrimitiveRect(RectTransform rt, Vector2 position, float width, float height)
{
if (rt == null) return;
Vector2 topLeft = new Vector2(0f, 1f);
Vector2 center = new Vector2(0.5f, 0.5f);
if (rt.anchorMin != topLeft) rt.anchorMin = topLeft;
if (rt.anchorMax != topLeft) rt.anchorMax = topLeft;
if (rt.pivot != center) rt.pivot = center;
if (rt.anchoredPosition != position) rt.anchoredPosition = position;
var size = new Vector2(width, height);
if (rt.sizeDelta != size) rt.sizeDelta = size;
}
/// <summary>同上,顺带把尺寸一起钉死。</summary>
private static void PinTopLeftRect(RectTransform rt, Vector2 position, float width, float height)
{
if (rt == null) return;
PinTopLeftRect(rt, position);
var size = new Vector2(width, height);
if (rt.sizeDelta != size) rt.sizeDelta = size;
}
}
}