调整表格渲染对象复用逻辑

This commit is contained in:
2026-08-18 13:43:49 +08:00
parent 1453dc6634
commit b132e8e167
9 changed files with 155 additions and 48 deletions
@@ -50,14 +50,14 @@ namespace XericUI.XTable.Rendering.Elements
#region 构造函数与析构
public CellAssembler(Transform parent, string defaultStyleNamespace = null, int textPoolMaxCapacity = 256,
int imagePoolMaxCapacity = 256)
int imagePoolMaxCapacity = 256, int textPoolRouteBucketCount = 256, int imagePoolRouteBucketCount = 256)
{
m_Parent = parent;
m_DefaultStyleNamespace = defaultStyleNamespace;
m_ImagePool = new SlotCounterPool<PooledImage>(() => CreateImage("cell_img"), imagePoolMaxCapacity,
SetImageCounterActive, SetImageCounterInactive);
SetImageCounterActive, SetImageCounterInactive, imagePoolRouteBucketCount);
m_TextPool = new SlotCounterPool<PooledText>(() => CreateText("cell_text"), textPoolMaxCapacity,
SetTextCounterActive, SetTextCounterInactive);
SetTextCounterActive, SetTextCounterInactive, textPoolRouteBucketCount);
}
/// <summary>彻底销毁所有池对象。</summary>
@@ -175,13 +175,13 @@ namespace XericUI.XTable.Rendering.Elements
/// <summary>解除指定 Image 的槽位绑定;显隐仅在所属 Counter 完整归还时变更。</summary>
public void ReleaseImage(PooledImage item)
{
if (item != null) m_ImagePool.ReleaseCell(item.CellId);
if (item != null) m_ImagePool.ReleaseByObject(item);
}
/// <summary>解除指定 Text 的槽位绑定;显隐仅在所属 Counter 完整归还时变更。</summary>
public void ReleaseText(PooledText item)
{
if (item != null) m_TextPool.ReleaseCell(item.CellId);
if (item != null) m_TextPool.ReleaseByObject(item);
}
private void SetImageCounterActive(IReadOnlyList<PooledImage> items)
@@ -59,6 +59,13 @@ namespace XericUI.XTable.Rendering.Elements
}
}
public bool IsInstanceOfPrefab(GameObject instance, GameObject prefab)
{
if (instance == null || prefab == null) return false;
return m_Pools.TryGetValue(prefab, out PrefabPool pool)
&& pool.ActiveSlots.ContainsValue(instance);
}
public bool Release(GameObject instance, Action<GameObject> beforeRelease = null)
{
if (instance == null) return false;
@@ -30,10 +30,9 @@ namespace XericUI.XTable.Rendering.Elements
#region 字段
/// <summary>cellId 路由桶数量。</summary>
private const int ROUTE_BUCKET_COUNT = 256;
private const int DEFAULT_ROUTE_BUCKET_COUNT = 256;
private readonly SlotCounter[] m_Counters = new SlotCounter[ROUTE_BUCKET_COUNT];
private readonly SlotCounter[] m_Counters;
private readonly int m_MaxCreatedCount;
private readonly Stack<SlotCounter> m_StandbyCounters = new Stack<SlotCounter>();
private readonly Func<T> m_Factory;
@@ -43,16 +42,31 @@ namespace XericUI.XTable.Rendering.Elements
/// <summary>已创建对象总数(只读)</summary>
public int CreatedCount => m_CreatedCount;
public int MaxCreatedCount => m_MaxCreatedCount;
public int RouteBucketCount => m_Counters.Length;
public int ActiveCounterCount
{
get
{
int count = 0;
for (int i = 0; i < m_Counters.Length; i++)
if (m_Counters[i] != null) count++;
return count;
}
}
public bool IsPoolFull => m_CreatedCount >= m_MaxCreatedCount;
#endregion
#region 构造
public SlotCounterPool(Func<T> factory, int maxCreatedCount = 256,
Action<IReadOnlyList<T>> onCounterBorrowed = null, Action<IReadOnlyList<T>> onCounterReturned = null)
Action<IReadOnlyList<T>> onCounterBorrowed = null, Action<IReadOnlyList<T>> onCounterReturned = null,
int routeBucketCount = DEFAULT_ROUTE_BUCKET_COUNT)
{
m_Factory = factory ?? throw new ArgumentNullException(nameof(factory));
m_MaxCreatedCount = Mathf.Max(1, maxCreatedCount);
m_Counters = new SlotCounter[Mathf.Max(1, routeBucketCount)];
m_OnCounterBorrowed = onCounterBorrowed;
m_OnCounterReturned = onCounterReturned;
}
@@ -159,7 +173,7 @@ namespace XericUI.XTable.Rendering.Elements
{
if (pooledObject == null) return false;
for (int ci = 0; ci < ROUTE_BUCKET_COUNT; ci++)
for (int ci = 0; ci < m_Counters.Length; ci++)
{
var counter = m_Counters[ci];
if (counter == null) continue;
@@ -176,7 +190,7 @@ namespace XericUI.XTable.Rendering.Elements
/// <summary>解除全部槽位绑定,并将所有计数器整体归还待命栈。</summary>
public void ReleaseAllBindings()
{
for (int ci = 0; ci < ROUTE_BUCKET_COUNT; ci++)
for (int ci = 0; ci < m_Counters.Length; ci++)
{
var counter = m_Counters[ci];
if (counter == null) continue;
@@ -192,7 +206,7 @@ namespace XericUI.XTable.Rendering.Elements
{
if (beforeRelease != null)
{
for (int ci = 0; ci < ROUTE_BUCKET_COUNT; ci++)
for (int ci = 0; ci < m_Counters.Length; ci++)
{
var counter = m_Counters[ci];
if (counter == null) continue;
@@ -208,7 +222,7 @@ namespace XericUI.XTable.Rendering.Elements
{
if (destroyFunc == null) return;
var destroyedCounters = new HashSet<SlotCounter>();
for (int ci = 0; ci < ROUTE_BUCKET_COUNT; ci++)
for (int ci = 0; ci < m_Counters.Length; ci++)
{
var counter = m_Counters[ci];
if (counter != null) destroyedCounters.Add(counter);
@@ -221,10 +235,10 @@ namespace XericUI.XTable.Rendering.Elements
m_CreatedCount = 0;
}
private static int GetRouteIndex(int cellId)
private int GetRouteIndex(int cellId)
{
int index = cellId % ROUTE_BUCKET_COUNT;
return index < 0 ? index + ROUTE_BUCKET_COUNT : index;
int index = cellId % m_Counters.Length;
return index < 0 ? index + m_Counters.Length : index;
}
private void ReturnCounter(int index, SlotCounter counter)