更换表格对象池系统,新的对象池系统会尽可能减小对象的回收操作,同时引入基于曼哈顿点距的对象池索引逻辑,引入计数器,避免野对象或对象冲突。

This commit is contained in:
2026-07-28 13:52:24 +08:00
parent b6557db618
commit 550f588408
6 changed files with 351 additions and 170 deletions
@@ -1,5 +1,3 @@
using System.Collections.Generic;
using TMPro;
using UnityEngine;
@@ -8,7 +6,8 @@ using UnityEngine.UI;
namespace XericUI.XTable.Rendering.Elements
{
/// <summary>
/// 表格单元动态拼装器——通过对象池管理 Image + TMP_Text 的组合创建与回收。
/// 表格单元动态拼装器——通过 SlotCounterPool 管理 Image + TMP_Text 的组合创建与回收。
/// 每个单元格通过 cellId 映射到固定索引的计数器槽位,空闲槽位经 FrozenSlots 全局流转复用。
/// 所有元素统一使用 anchor=(0,1) pivot=(0,1) 左上角坐标系。
/// 所有动态生成对象标记 HideFlags.DontSave | HideFlags.HideInHierarchy,防止泄漏到场景。
/// </summary>
@@ -22,6 +21,8 @@ namespace XericUI.XTable.Rendering.Elements
public RectTransform Rect;
public Image Image;
public StyleBoundElement Style;
/// <summary>绑定的单元格 ID,用于 Release 定位槽位</summary>
public int CellId;
}
public class PooledText
@@ -30,6 +31,8 @@ namespace XericUI.XTable.Rendering.Elements
public RectTransform Rect;
public TMP_Text Text;
public StyleBoundElement Style;
/// <summary>绑定的单元格 ID,用于 Release 定位槽位</summary>
public int CellId;
}
#endregion
@@ -44,20 +47,14 @@ namespace XericUI.XTable.Rendering.Elements
/// <summary>所有已绑定的 StyleBoundElement 回调集合(用于样式变更时通知外部)</summary>
public event System.Action OnStyleChangeDetected;
private Queue<PooledImage> m_ImagePool = new Queue<PooledImage>();
private Queue<PooledText> m_TextPool = new Queue<PooledText>();
private List<PooledImage> m_ActiveImages = new List<PooledImage>();
private List<PooledText> m_ActiveTexts = new List<PooledText>();
/// <summary>基于计数器的 Image 槽位对象池</summary>
private SlotCounterPool<PooledImage> m_ImagePool;
/// <summary>基于计数器的 Text 槽位对象池</summary>
private SlotCounterPool<PooledText> m_TextPool;
/// <summary>HideFlags 应用于所有动态创建的对象(不保存、不在 Hierarchy 显示)</summary>
private const HideFlags POOL_FLAGS = HideFlags.DontSave | HideFlags.HideInHierarchy;
/// <summary>渲染项目总数上限,超出后 GetImage/GetText 返回 null 并发出警告</summary>
private const int MAX_POOL_ITEMS = 256;
private int m_ImageCreatedCount;
private int m_TextCreatedCount;
#endregion
#region 构造函数与析构
@@ -66,33 +63,15 @@ namespace XericUI.XTable.Rendering.Elements
{
m_Parent = parent;
m_DefaultStyleNamespace = defaultStyleNamespace;
m_ImagePool = new SlotCounterPool<PooledImage>(() => CreateImage("cell_img"));
m_TextPool = new SlotCounterPool<PooledText>(() => CreateText("cell_text"));
}
/// <summary>彻底销毁所有池对象(包括活跃和休眠的)</summary>
/// <summary>彻底销毁所有池对象(包括计数器内活跃槽位和冻结槽位)</summary>
public void DestroyAll()
{
// 杀死活跃对象
for (int i = m_ActiveImages.Count - 1; i >= 0; i--)
{
KillObject(m_ActiveImages[i].Root);
}
m_ActiveImages.Clear();
for (int i = m_ActiveTexts.Count - 1; i >= 0; i--)
{
KillObject(m_ActiveTexts[i].Root);
}
m_ActiveTexts.Clear();
// 杀死池中对象
while (m_ImagePool.Count > 0)
{
KillObject(m_ImagePool.Dequeue().Root);
}
while (m_TextPool.Count > 0)
{
KillObject(m_TextPool.Dequeue().Root);
}
m_ImagePool.DestroyAll(item => KillObject(item.Root));
m_TextPool.DestroyAll(item => KillObject(item.Root));
}
private static void KillObject(GameObject go)
@@ -116,49 +95,29 @@ namespace XericUI.XTable.Rendering.Elements
#region 池操作
public PooledImage GetImage(string name = "cell_img", string styleNamespace = null)
/// <summary>为指定 cellId 获取一个池化 Image</summary>
public PooledImage GetImage(int cellId, string name = "cell_img", string styleNamespace = null)
{
PooledImage item;
if (m_ImagePool.Count > 0)
{
item = m_ImagePool.Dequeue();
ResetImageDefaults(item);
}
else if (m_ImageCreatedCount < MAX_POOL_ITEMS)
{
item = CreateImage(name);
m_ImageCreatedCount++;
}
else
{
Debug.LogWarning($"[XericUIActionTable] Image 池已达上限 {MAX_POOL_ITEMS},无法分配新对象");
return null;
}
var item = m_ImagePool.Acquire(cellId);
if (item == null) return null;
item.CellId = cellId;
item.Root.name = name;
ResetImageDefaults(item);
BindStyle(item.Style, styleNamespace);
m_ActiveImages.Add(item);
return item;
}
public PooledText GetText(string name = "cell_text", string styleNamespace = null)
/// <summary>为指定 cellId 获取一个池化 TMP_Text</summary>
public PooledText GetText(int cellId, string name = "cell_text", string styleNamespace = null)
{
PooledText item;
if (m_TextPool.Count > 0)
{
item = m_TextPool.Dequeue();
ResetTextDefaults(item);
}
else if (m_TextCreatedCount < MAX_POOL_ITEMS)
{
item = CreateText(name);
m_TextCreatedCount++;
}
else
{
Debug.LogWarning($"[XericUIActionTable] Text 池已达上限 {MAX_POOL_ITEMS},无法分配新对象");
return null;
}
var item = m_TextPool.Acquire(cellId);
if (item == null) return null;
item.CellId = cellId;
item.Root.name = name;
ResetTextDefaults(item);
BindStyle(item.Style, styleNamespace);
m_ActiveTexts.Add(item);
return item;
}
@@ -183,41 +142,28 @@ namespace XericUI.XTable.Rendering.Elements
OnStyleChangeDetected?.Invoke();
}
/// <summary>归还所有槽位至 FrozenSlots(全量刷新时调用)</summary>
public void ReturnAll()
{
foreach (var img in m_ActiveImages) ReturnImage(img);
m_ActiveImages.Clear();
foreach (var txt in m_ActiveTexts) ReturnText(txt);
m_ActiveTexts.Clear();
// Unbind 所有活跃槽位的样式回调
m_ImagePool.ReturnAll();
m_TextPool.ReturnAll();
}
/// <summary>归还单个 Image 池对象(从活跃列表移除并推入休眠栈)</summary>
/// <summary>归还单个 Image 池对象(通过 CellId 定位槽位并移入 FrozenSlots)</summary>
public void ReleaseImage(PooledImage item)
{
if (item == null) return;
m_ActiveImages.Remove(item);
ReturnImage(item);
item.Style?.Unbind();
m_ImagePool.Release(item.CellId);
}
/// <summary>归还单个 Text 池对象(从活跃列表移除并推入休眠栈)</summary>
/// <summary>归还单个 Text 池对象(通过 CellId 定位槽位并移入 FrozenSlots)</summary>
public void ReleaseText(PooledText item)
{
if (item == null) return;
m_ActiveTexts.Remove(item);
ReturnText(item);
}
private void ReturnImage(PooledImage item)
{
item.Style?.Unbind();
m_ImagePool.Enqueue(item);
}
private void ReturnText(PooledText item)
{
item.Style?.Unbind();
m_TextPool.Enqueue(item);
m_TextPool.Release(item.CellId);
}
#endregion