坐标错位的真正来源在图元渲染器的上游:PrimitiveCacheRendererBase / CacheLineRendererBase 把 子块建成"铺满本渲染器、轴心 0.5"的对象,顶点直接放在子块本地空间 ⇒ 图元坐标的原点是 rect 的<b>中心</b>,不是左上角。表格喂的是左上角坐标,于是背景与边框(两者都是图元渲染器) 一起偏了半个尺寸、彼此严丝合缝,却和文本(UGUI 左上角语义)对不上。 - 新增 PinPrimitiveRect:锚点 (0,1)、轴心 0.5、锚位放在左上角 ⇒ rect 中心(即网格原点) 与左上角重合;背景、边框、冻结边框都改走它,绘图代码保持左上角语义不变。 - 撤回上一版按 rectTransform.rect 换算原点的做法(那个假设是错的)。 - SetChildrenVisible 改为遍历整棵生成层级(模板/背景/边框/冻结层/池化单元格), 显示时只去掉 HideInHierarchy、保留 NotEditable:调试视图能看到每一个生成物。
59 lines
2.3 KiB
C#
59 lines
2.3 KiB
C#
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using XericLibrary.Runtime.UIGraph;
|
||
using PrimitiveType = XericLibrary.Runtime.UIGraph.PrimitiveType;
|
||
|
||
namespace XericUI.XTable.Rendering.Elements
|
||
{
|
||
/// <summary>用于批量绘制 XTable 背景区域的图元渲染器。</summary>
|
||
public sealed class XTableBackgroundRenderer : PrimitiveCacheRendererBase
|
||
{
|
||
/// <summary>以左上角为原点的背景区域及其填充颜色。</summary>
|
||
public struct RectData
|
||
{
|
||
public Rect Rect;
|
||
public Color Color;
|
||
|
||
public RectData(Rect rect, Color color)
|
||
{
|
||
Rect = rect;
|
||
Color = color;
|
||
}
|
||
}
|
||
|
||
/// <summary>以左上角坐标系替换全部背景矩形。</summary>
|
||
public void ReplaceRects(IReadOnlyList<RectData> rects)
|
||
{
|
||
ClearAll();
|
||
if (rects == null) return;
|
||
|
||
// 图元坐标是"以矩形左上角为原点"。基类把顶点放在子块本地空间里,而子块是铺满本渲染器
|
||
// 且轴心 0.5 的 —— 子块原点是<b>本渲染器 rect 的中心</b>。所以本渲染器的 rect 必须
|
||
// 以"中心 = 左上角"摆放(PinPrimitiveRect:pivot 0.5 + 锚位在左上角),
|
||
// 这里的坐标才和文本(UGUI 左上角语义)对得上。
|
||
for (int i = 0; i < rects.Count; i++)
|
||
{
|
||
RectData rectData = rects[i];
|
||
Rect rect = rectData.Rect;
|
||
AddPrimitive(new PrimitiveCacheEntry
|
||
{
|
||
type = PrimitiveType.Rectangle,
|
||
sizeMode = SizeMode.InscribedEllipse,
|
||
center = new Vector2(rect.x + rect.width * 0.5f, -(rect.y + rect.height * 0.5f)),
|
||
size = rect.size,
|
||
@params = new PrimitiveParams
|
||
{
|
||
axisScaleX = 1f,
|
||
axisScaleY = 1f,
|
||
chamferSegments = 1,
|
||
bgColor = rectData.Color,
|
||
centerColor = rectData.Color,
|
||
borderColor = Color.clear,
|
||
borderThickness = 0f,
|
||
},
|
||
});
|
||
}
|
||
}
|
||
}
|
||
}
|