存量缺陷,不是新功能带来的:XChartTextSlotFactory.Acquire() 只给**新建**那件设默认
(text/换行/对齐/anchor/pivot),从 m_Free 里 Pop 出来的那件只 SetActive(true)。
而元素每帧往槽位上写的远不止这些——轴线就写六样:
label.text / fontSize / color / rectTransform.sizeDelta / anchoredPosition / localEulerAngles
(ChartToolAxis.cs:469-474)
⇒ 一件被上一个元素改成 14 号字、蓝色、矩形 90×24、还转了 30°,还回池子再发给别的元素时,
**这些值会带着走**:新元素只写它自己关心的那几样,剩下的沿用上一轮的。症状是"标签换个顺序就变大/歪了",
而画面上完全看不出是谁写的——典型的"同一件事两处存"(上一轮的残留也是"两处")。
落法:工厂里抽 public static ResetToDefaults(TMP_Text),**默认值只在这里定义一次**,
新建与复用**两条路径都调它**(复用路径那句是关键)。清单按"被写了什么"来定,不靠记忆:
新门 .ximage-gate/slot_reset_gate.py 两条主张——
A 所有元素往槽位上写的属性 ⊆ 恢复清单(读数:files scanned=53 slot writes=14 distinct=7 reset lines=16);
B 恢复函数确实在**两条获取路径**上被调用(只在"新建"分支调=一条绿谎,门直接判 FAIL)。
三支变异各红在自己点名的那条:S1 从清单里删掉 fontSize、S2 让轴线多写一个 overflowMode(门当场点名)、
S3 把复用路径那句调用删掉(B 主张两个分支同报 False)。正本 PASS、三支全红。
顺带记一条 TMP 版本事实(离线编译门先替我撞的):这版 TextMeshProUGUI 的 outlineWidth 是 **float**、
并且没有 enableOutline 开关 ⇒ 归零宽度就是不描边。我按新版文档写了 Vector2.zero 和 tmp.enableOutline,
CS0029 + CS1061 两条在门里就红了,没进他的编辑器(这条正是"写编辑器侧代码前先核 API 存在"的又一次兑现)。
覆盖不到的:这一句不改"上不上屏"(那是 P23/P25 的填充跳板),也不证明元素写得对——
它只保证"没写的属性不会带着上一轮的值"。真上屏与观感照旧归他现场判。
174 lines
7.3 KiB
C#
174 lines
7.3 KiB
C#
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
|
||
namespace XericUI.XChart.Pools
|
||
{
|
||
/// <summary>
|
||
/// 文本槽位工厂:运行时里<b>唯一</b>造 TMP 标签的地方(规范 §5)。
|
||
///
|
||
/// <para>空闲的槽位停在池根(chart 根下的 <c>GeneratedTextPool</c> 节点)上并停用;
|
||
/// 发落出去时挂到该 owner 自己的宿主节点。"父节点必须等于宿主"是有效性判据的一部分——
|
||
/// 域重载后宿主是新实例,旧标签还挂在旧父节点上,这种必须丢弃重补,
|
||
/// 不能让它带着上一轮的父子关系继续画。</para>
|
||
///
|
||
/// <para>池根整块没了(chart 被销毁又重建)不抛异常:<see cref="EnsurePoolRoot"/> 直接重建。</para>
|
||
/// </summary>
|
||
public sealed class XChartTextSlotFactory : IXChartSlotFactory<TMP_Text>
|
||
{
|
||
/// <summary>池根节点名。chart 的回收校验和调试报表都按这个名字找它,所以只此一份。</summary>
|
||
public const string PoolRootName = "GeneratedTextPool";
|
||
|
||
private readonly RectTransform m_ChartRoot;
|
||
private readonly Stack<TMP_Text> m_Free = new Stack<TMP_Text>();
|
||
private RectTransform m_PoolRoot;
|
||
private int m_SlotIndex;
|
||
|
||
public XChartTextSlotFactory(RectTransform chartRoot)
|
||
{
|
||
m_ChartRoot = chartRoot;
|
||
}
|
||
|
||
/// <summary>池里闲置的槽位数(调试判据用:调度收敛后应当等于"曾经发落过又还回来的")</summary>
|
||
public int FreeCount => m_Free.Count;
|
||
|
||
/// <summary>
|
||
/// 槽位的<b>默认状态</b>只在这里定义一次。新建那件走它,<b>从空闲表里拿回来的那件也必须走它</b>——
|
||
/// 只在新建分支设默认的话,复用件会带着上一轮的字号、颜色、矩形和旋转走(轴线每帧写
|
||
/// <c>text/fontSize/color/sizeDelta/anchoredPosition/localEulerAngles</c> 这六样,别的需求也一样),
|
||
/// 症状是"标签的元素换了个顺序显示,字就莫名变大/歪了",而这种错在画面上完全看不出来是谁写的。
|
||
/// </summary>
|
||
public static void ResetToDefaults(TMP_Text slot)
|
||
{
|
||
if (!IsAlive(slot)) return;
|
||
|
||
var tmp = slot as TextMeshProUGUI;
|
||
if (tmp != null)
|
||
{
|
||
tmp.text = string.Empty;
|
||
tmp.fontSize = k_DefaultFontSize;
|
||
tmp.color = k_DefaultColor;
|
||
tmp.fontStyle = FontStyles.Normal;
|
||
tmp.enableWordWrapping = false;
|
||
tmp.characterSpacing = 0f;
|
||
tmp.lineSpacing = 0f;
|
||
// 这版 TMP 的 outlineWidth 是 float(不是 Vector2),也没有 enableOutline 开关:
|
||
// 宽度归零就是不描边
|
||
tmp.outlineWidth = 0f;
|
||
tmp.alignment = TextAlignmentOptions.Center;
|
||
}
|
||
|
||
var rect = slot.rectTransform;
|
||
rect.anchorMin = k_DefaultAnchor;
|
||
rect.anchorMax = k_DefaultAnchor;
|
||
rect.pivot = k_DefaultPivot;
|
||
rect.sizeDelta = k_DefaultSize;
|
||
rect.anchoredPosition = Vector2.zero;
|
||
rect.localEulerAngles = Vector3.zero;
|
||
}
|
||
|
||
private const float k_DefaultFontSize = 14f;
|
||
private static readonly Color k_DefaultColor = Color.white;
|
||
private static readonly Vector2 k_DefaultAnchor = new Vector2(0.5f, 0.5f);
|
||
private static readonly Vector2 k_DefaultPivot = new Vector2(0.5f, 0.5f);
|
||
private static readonly Vector2 k_DefaultSize = new Vector2(0f, 0f);
|
||
|
||
public TMP_Text Acquire()
|
||
{
|
||
var poolRoot = EnsurePoolRoot();
|
||
if (poolRoot == null) return null;
|
||
|
||
while (m_Free.Count > 0)
|
||
{
|
||
TMP_Text slot = m_Free.Pop();
|
||
// 空闲表里也可能有域重载留下的僵尸件:验不过就当场销毁,别发出去
|
||
if (!IsAlive(slot)) { DestroySlot(slot); continue; }
|
||
// 复用的件先擦回默认——这一句是这条路径上唯一的保证,别指望各元素"自己都会写全"
|
||
ResetToDefaults(slot);
|
||
slot.gameObject.SetActive(true);
|
||
return slot;
|
||
}
|
||
|
||
var go = new GameObject("TextSlot_" + m_SlotIndex, typeof(RectTransform));
|
||
m_SlotIndex++;
|
||
go.transform.SetParent(poolRoot, false);
|
||
go.hideFlags = XChartBase.CurrentFlags;
|
||
|
||
var tmp = go.AddComponent<TextMeshProUGUI>();
|
||
ResetToDefaults(tmp);
|
||
return tmp;
|
||
}
|
||
|
||
public void Release(TMP_Text slot)
|
||
{
|
||
if (!IsAlive(slot)) return;
|
||
slot.text = string.Empty;
|
||
slot.gameObject.SetActive(false);
|
||
var poolRoot = EnsurePoolRoot();
|
||
if (poolRoot != null) slot.transform.SetParent(poolRoot, false);
|
||
m_Free.Push(slot);
|
||
}
|
||
|
||
public void Assign(TMP_Text slot, object host)
|
||
{
|
||
if (!IsAlive(slot)) return;
|
||
var parent = host as RectTransform;
|
||
if (parent == null) return;
|
||
slot.transform.SetParent(parent, false);
|
||
slot.gameObject.SetActive(true);
|
||
}
|
||
|
||
public bool IsUsable(TMP_Text slot, object host)
|
||
{
|
||
var parent = host as RectTransform;
|
||
if (parent == null) return false;
|
||
if (!IsAlive(slot)) return false;
|
||
return slot.transform.parent == parent && slot.gameObject.activeSelf;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 存在性合取判据(§5.3 的前半):件还在、GameObject 还在、保存位仍然带 DontSave。
|
||
/// 父节点与激活状态因 owner 而异,交给 <see cref="IsUsable"/>。
|
||
/// </summary>
|
||
private static bool IsAlive(TMP_Text slot)
|
||
{
|
||
if (slot == null) return false;
|
||
if (slot.gameObject == null) return false;
|
||
return (slot.hideFlags & HideFlags.DontSave) != 0;
|
||
}
|
||
|
||
private static void DestroySlot(TMP_Text slot)
|
||
{
|
||
if (slot == null) return;
|
||
if (slot.gameObject == null) return;
|
||
UnityEngine.Object.DestroyImmediate(slot.gameObject);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 拆池:把池根连同池内件一起硬销毁。<b>必须有这一步</b>——池根挂在 chart 自己下面而
|
||
/// 工具清理只拆 <c>[XChartTools]</c>,不拆它就等于每回收重建一次漏一个池根在场景里。
|
||
/// 已经发落出去的件不归这里:它们在各工具的宿主下,随工具根一起销毁。
|
||
/// </summary>
|
||
public void DestroyAll()
|
||
{
|
||
m_Free.Clear();
|
||
if (m_PoolRoot == null) return;
|
||
var rootObject = m_PoolRoot.gameObject;
|
||
m_PoolRoot = null;
|
||
if (rootObject != null) UnityEngine.Object.DestroyImmediate(rootObject);
|
||
}
|
||
|
||
private RectTransform EnsurePoolRoot()
|
||
{
|
||
if (m_PoolRoot != null) return m_PoolRoot;
|
||
if (m_ChartRoot == null) return null;
|
||
|
||
var go = new GameObject(PoolRootName, typeof(RectTransform));
|
||
go.transform.SetParent(m_ChartRoot, false);
|
||
go.hideFlags = XChartBase.CurrentFlags;
|
||
m_PoolRoot = go.GetComponent<RectTransform>();
|
||
return m_PoolRoot;
|
||
}
|
||
}
|
||
}
|