using System.Collections.Generic;
using UnityEngine;
namespace XericUI.XTable.Rendering.Component
{
///
/// 单元格对象池——复用表格单元格 GameObject,避免频繁 Instantiate/Destroy。
///
public class XTableObjectPool
{
/// 回收池
private readonly Stack m_Pool = new Stack();
/// 当前活跃对象(已借出)
private readonly HashSet m_Active = new HashSet();
/// 预制体模板
private readonly GameObject m_Prefab;
/// 父级 Transform
private readonly Transform m_Parent;
/// 初始池容量
private readonly int m_InitialCapacity;
public XTableObjectPool(GameObject prefab, Transform parent, int initialCapacity = 64)
{
m_Prefab = prefab;
m_Parent = parent;
m_InitialCapacity = initialCapacity;
// 预创建对象
for (int i = 0; i < initialCapacity; i++)
{
GameObject obj = CreateNew();
obj.SetActive(false);
m_Pool.Push(obj);
}
}
///
/// 从池中获取一个对象
///
public GameObject Get()
{
GameObject obj;
if (m_Pool.Count > 0)
{
obj = m_Pool.Pop();
}
else
{
obj = CreateNew();
}
obj.SetActive(true);
m_Active.Add(obj);
return obj;
}
///
/// 将对象归还到池中
///
public void Return(GameObject obj)
{
if (obj == null) return;
obj.SetActive(false);
m_Active.Remove(obj);
m_Pool.Push(obj);
}
///
/// 归还所有活跃对象
///
public void ReturnAll()
{
foreach (var obj in m_Active)
{
if (obj != null)
{
obj.SetActive(false);
m_Pool.Push(obj);
}
}
m_Active.Clear();
}
///
/// 清理所有对象
///
public void Clear()
{
ReturnAll();
while (m_Pool.Count > 0)
{
var obj = m_Pool.Pop();
if (obj != null)
Object.Destroy(obj);
}
}
/// 当前活跃对象数
public int ActiveCount => m_Active.Count;
/// 池中空闲对象数
public int PoolCount => m_Pool.Count;
private GameObject CreateNew()
{
GameObject obj = m_Prefab != null
? Object.Instantiate(m_Prefab, m_Parent)
: new GameObject("TableCell", typeof(RectTransform));
obj.transform.SetParent(m_Parent, false);
return obj;
}
}
}