表格:背景/边框的图元原点按"rect 中心"摆放;调试开关覆盖整棵生成层级
坐标错位的真正来源在图元渲染器的上游:PrimitiveCacheRendererBase / CacheLineRendererBase 把 子块建成"铺满本渲染器、轴心 0.5"的对象,顶点直接放在子块本地空间 ⇒ 图元坐标的原点是 rect 的<b>中心</b>,不是左上角。表格喂的是左上角坐标,于是背景与边框(两者都是图元渲染器) 一起偏了半个尺寸、彼此严丝合缝,却和文本(UGUI 左上角语义)对不上。 - 新增 PinPrimitiveRect:锚点 (0,1)、轴心 0.5、锚位放在左上角 ⇒ rect 中心(即网格原点) 与左上角重合;背景、边框、冻结边框都改走它,绘图代码保持左上角语义不变。 - 撤回上一版按 rectTransform.rect 换算原点的做法(那个假设是错的)。 - SetChildrenVisible 改为遍历整棵生成层级(模板/背景/边框/冻结层/池化单元格), 显示时只去掉 HideInHierarchy、保留 NotEditable:调试视图能看到每一个生成物。
This commit is contained in:
@@ -39,9 +39,9 @@ namespace XericUI.XTable.Rendering.Component
|
|||||||
{
|
{
|
||||||
// 顺带自愈旧对象的标记:运行期渲染物一律不可编辑,编辑期改动才不会被记成预制体覆盖。
|
// 顺带自愈旧对象的标记:运行期渲染物一律不可编辑,编辑期改动才不会被记成预制体覆盖。
|
||||||
existing.gameObject.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy | HideFlags.NotEditable;
|
existing.gameObject.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy | HideFlags.NotEditable;
|
||||||
// 认领回来的旧对象可能还带着上一版的中心锚点,先钉回左上角。
|
// 图元渲染器的顶点原点是 rect 中心:把中心摆到左上角(pivot 0.5 + 锚位 0)。
|
||||||
PinTopLeftRect(existing.transform as RectTransform,
|
PinPrimitiveRect(existing.transform as RectTransform,
|
||||||
new Vector2(m_ContentOffset, -m_ContentOffset));
|
new Vector2(m_ContentOffset, -m_ContentOffset), m_TotalWidth, m_TotalHeight);
|
||||||
return m_BorderRenderer;
|
return m_BorderRenderer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -52,12 +52,10 @@ namespace XericUI.XTable.Rendering.Component
|
|||||||
go.transform.SetParent(contentT, false);
|
go.transform.SetParent(contentT, false);
|
||||||
|
|
||||||
RectTransform rt = go.GetComponent<RectTransform>();
|
RectTransform rt = go.GetComponent<RectTransform>();
|
||||||
rt.anchorMin = new Vector2(0, 1);
|
|
||||||
rt.anchorMax = new Vector2(0, 1);
|
|
||||||
rt.pivot = new Vector2(0, 1);
|
|
||||||
float pad = m_ContentOffset * 2f;
|
float pad = m_ContentOffset * 2f;
|
||||||
rt.anchoredPosition = new Vector2(m_ContentOffset, -m_ContentOffset);
|
// 图元渲染器的顶点原点是 rect 中心:中心摆到左上角,边框坐标就与文本同一套语义。
|
||||||
rt.sizeDelta = new Vector2(m_TotalWidth + pad, m_TotalHeight + pad);
|
PinPrimitiveRect(rt, new Vector2(m_ContentOffset, -m_ContentOffset),
|
||||||
|
m_TotalWidth + pad, m_TotalHeight + pad);
|
||||||
|
|
||||||
m_BorderRenderer = go.GetComponent<UILineRendererV2>();
|
m_BorderRenderer = go.GetComponent<UILineRendererV2>();
|
||||||
m_BorderRenderer.raycastTarget = false;
|
m_BorderRenderer.raycastTarget = false;
|
||||||
@@ -85,7 +83,7 @@ namespace XericUI.XTable.Rendering.Component
|
|||||||
|
|
||||||
var renderer = GetOrCreateBorderRenderer();
|
var renderer = GetOrCreateBorderRenderer();
|
||||||
renderer.ClearAll();
|
renderer.ClearAll();
|
||||||
PinTopLeftRect(renderer.rectTransform, new Vector2(m_ContentOffset, -m_ContentOffset),
|
PinPrimitiveRect(renderer.rectTransform, new Vector2(m_ContentOffset, -m_ContentOffset),
|
||||||
m_TotalWidth, m_TotalHeight);
|
m_TotalWidth, m_TotalHeight);
|
||||||
|
|
||||||
int rows = m_RowHeights.Length;
|
int rows = m_RowHeights.Length;
|
||||||
@@ -100,7 +98,7 @@ namespace XericUI.XTable.Rendering.Component
|
|||||||
{
|
{
|
||||||
if (renderer == null || rowStart >= rowEnd || colStart >= colEnd) return;
|
if (renderer == null || rowStart >= rowEnd || colStart >= colEnd) return;
|
||||||
renderer.ClearAll();
|
renderer.ClearAll();
|
||||||
PinTopLeftRect(renderer.rectTransform, new Vector2(m_ContentOffset, -m_ContentOffset),
|
PinPrimitiveRect(renderer.rectTransform, new Vector2(m_ContentOffset, -m_ContentOffset),
|
||||||
m_TotalWidth, m_TotalHeight);
|
m_TotalWidth, m_TotalHeight);
|
||||||
for (int boundary = rowStart; boundary <= rowEnd; boundary++)
|
for (int boundary = rowStart; boundary <= rowEnd; boundary++)
|
||||||
SubmitHorizontalBoundary(renderer, boundary, colStart, colEnd);
|
SubmitHorizontalBoundary(renderer, boundary, colStart, colEnd);
|
||||||
@@ -185,9 +183,8 @@ namespace XericUI.XTable.Rendering.Component
|
|||||||
go.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy;
|
go.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy;
|
||||||
go.transform.SetParent(parent, false);
|
go.transform.SetParent(parent, false);
|
||||||
var rt = go.GetComponent<RectTransform>();
|
var rt = go.GetComponent<RectTransform>();
|
||||||
rt.anchorMin = new Vector2(0, 1);
|
// 同上:中心落在容器的左上角,冻结边框与冻结区的文本对得上。
|
||||||
rt.anchorMax = new Vector2(0, 1);
|
PinPrimitiveRect(rt, Vector2.zero, 0f, 0f);
|
||||||
rt.pivot = new Vector2(0, 1);
|
|
||||||
renderer = go.GetComponent<UILineRendererV2>();
|
renderer = go.GetComponent<UILineRendererV2>();
|
||||||
renderer.raycastTarget = false;
|
renderer.raycastTarget = false;
|
||||||
return renderer;
|
return renderer;
|
||||||
|
|||||||
@@ -104,21 +104,34 @@ namespace XericUI.XTable.Rendering.Component
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>设置所有动态子对象的可见性(仅编辑器)</summary>
|
/// <summary>
|
||||||
|
/// 设置所有动态子对象的可见性(仅编辑器)。覆盖整棵生成层级:模板(Viewport/Content/
|
||||||
|
/// 滚动条)、背景、边框、冻结层、池化单元格都挂 DontSave,这里统一把 HideInHierarchy 去掉。
|
||||||
|
/// </summary>
|
||||||
public void SetChildrenVisible(bool visible)
|
public void SetChildrenVisible(bool visible)
|
||||||
{
|
{
|
||||||
m_ChildrenVisible = visible;
|
m_ChildrenVisible = visible;
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
Transform t = ContentTransform;
|
ApplyGeneratedVisibility(transform, visible);
|
||||||
if (t == null) return;
|
|
||||||
for (int i = t.childCount - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
var child = t.GetChild(i);
|
|
||||||
child.gameObject.hideFlags = visible
|
|
||||||
? HideFlags.DontSave
|
|
||||||
: HideFlags.DontSave | HideFlags.HideInHierarchy;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
private static void ApplyGeneratedVisibility(Transform parent, bool visible)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < parent.childCount; i++)
|
||||||
|
{
|
||||||
|
var child = parent.GetChild(i);
|
||||||
|
if ((child.gameObject.hideFlags & HideFlags.DontSave) != 0)
|
||||||
|
{
|
||||||
|
// NotEditable 保留:看得见、不误改(编辑期改动不会被记成预制体覆盖)。
|
||||||
|
child.gameObject.hideFlags = visible
|
||||||
|
? HideFlags.DontSave | HideFlags.NotEditable
|
||||||
|
: HideFlags.DontSave | HideFlags.NotEditable | HideFlags.HideInHierarchy;
|
||||||
|
}
|
||||||
|
ApplyGeneratedVisibility(child, visible);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -537,7 +537,7 @@ namespace XericUI.XTable.Rendering.Component
|
|||||||
|
|
||||||
// 每次更新都按尺寸钉死:认领回来的旧对象可能还带着上一版的中心锚点,
|
// 每次更新都按尺寸钉死:认领回来的旧对象可能还带着上一版的中心锚点,
|
||||||
// 只改尺寸就会画成"以父组件中心为左上角"。
|
// 只改尺寸就会画成"以父组件中心为左上角"。
|
||||||
PinTopLeftRect(m_BackgroundRenderer.rectTransform,
|
PinPrimitiveRect(m_BackgroundRenderer.rectTransform,
|
||||||
new Vector2(m_ContentOffset, -m_ContentOffset), m_TotalWidth, m_TotalHeight);
|
new Vector2(m_ContentOffset, -m_ContentOffset), m_TotalWidth, m_TotalHeight);
|
||||||
string baseNamespace = GetSingleStyleNamespace();
|
string baseNamespace = GetSingleStyleNamespace();
|
||||||
var rects = new List<XTableBackgroundRenderer.RectData>
|
var rects = new List<XTableBackgroundRenderer.RectData>
|
||||||
|
|||||||
@@ -222,6 +222,24 @@ namespace XericUI.XTable.Rendering.Component
|
|||||||
if (rt.anchoredPosition != position) rt.anchoredPosition = position;
|
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>
|
/// <summary>同上,顺带把尺寸一起钉死。</summary>
|
||||||
private static void PinTopLeftRect(RectTransform rt, Vector2 position, float width, float height)
|
private static void PinTopLeftRect(RectTransform rt, Vector2 position, float width, float height)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -27,13 +27,10 @@ namespace XericUI.XTable.Rendering.Elements
|
|||||||
ClearAll();
|
ClearAll();
|
||||||
if (rects == null) return;
|
if (rects == null) return;
|
||||||
|
|
||||||
// 图元坐标是"以矩形左上角为原点",而基类把顶点直接放在渲染器的<b>本地空间</b>
|
// 图元坐标是"以矩形左上角为原点"。基类把顶点放在子块本地空间里,而子块是铺满本渲染器
|
||||||
// (本地原点是轴心,不是左上角)。这里按 rectTransform.rect 把两套原点对上:
|
// 且轴心 0.5 的 —— 子块原点是<b>本渲染器 rect 的中心</b>。所以本渲染器的 rect 必须
|
||||||
// 轴心是 (0,1) 还是 (0.5,0.5) 结果都一样——不再依赖调用方先把 pivot 摆对。
|
// 以"中心 = 左上角"摆放(PinPrimitiveRect:pivot 0.5 + 锚位在左上角),
|
||||||
Rect local = rectTransform.rect;
|
// 这里的坐标才和文本(UGUI 左上角语义)对得上。
|
||||||
float originX = local.x;
|
|
||||||
float originY = local.y + local.height;
|
|
||||||
|
|
||||||
for (int i = 0; i < rects.Count; i++)
|
for (int i = 0; i < rects.Count; i++)
|
||||||
{
|
{
|
||||||
RectData rectData = rects[i];
|
RectData rectData = rects[i];
|
||||||
@@ -42,8 +39,7 @@ namespace XericUI.XTable.Rendering.Elements
|
|||||||
{
|
{
|
||||||
type = PrimitiveType.Rectangle,
|
type = PrimitiveType.Rectangle,
|
||||||
sizeMode = SizeMode.InscribedEllipse,
|
sizeMode = SizeMode.InscribedEllipse,
|
||||||
center = new Vector2(originX + rect.x + rect.width * 0.5f,
|
center = new Vector2(rect.x + rect.width * 0.5f, -(rect.y + rect.height * 0.5f)),
|
||||||
originY - (rect.y + rect.height * 0.5f)),
|
|
||||||
size = rect.size,
|
size = rect.size,
|
||||||
@params = new PrimitiveParams
|
@params = new PrimitiveParams
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user