换成不触发active的对象池队列模式

This commit is contained in:
2026-07-28 10:27:45 +08:00
parent 28a850e342
commit b6557db618
4 changed files with 168 additions and 139 deletions
@@ -234,28 +234,40 @@ namespace XericUI.XTable.Rendering.Component
if (IsInSelectionRange != null && IsInSelectionRange(r, c)) if (IsInSelectionRange != null && IsInSelectionRange(r, c))
{ {
var selImg = m_Assembler.GetImage("cell_multiselect"); var selImg = m_Assembler.GetImage("cell_multiselect");
selImg.Rect.anchoredPosition = new Vector2(cellX, cellY); if (selImg != null)
selImg.Rect.sizeDelta = new Vector2(cellW, cellH); {
selImg.Image.color = (r == selRow && c == selCol) ? focusColor : multiSelectColor; selImg.Rect.anchoredPosition = new Vector2(cellX, cellY);
selImg.Rect.sizeDelta = new Vector2(cellW, cellH);
selImg.Image.color = (r == selRow && c == selCol) ? focusColor : multiSelectColor;
}
} }
else if (r == selRow && c == selCol) else if (r == selRow && c == selCol)
{ {
var selImg = m_Assembler.GetImage("cell_select"); var selImg = m_Assembler.GetImage("cell_select");
selImg.Rect.anchoredPosition = new Vector2(cellX, cellY); if (selImg != null)
selImg.Rect.sizeDelta = new Vector2(cellW, cellH); {
selImg.Image.color = selectionColor; selImg.Rect.anchoredPosition = new Vector2(cellX, cellY);
selImg.Rect.sizeDelta = new Vector2(cellW, cellH);
selImg.Image.color = selectionColor;
}
} }
// 网格线 // 网格线
var hLine = m_Assembler.GetImage("cell_hline"); var hLine = m_Assembler.GetImage("cell_hline");
hLine.Rect.anchoredPosition = new Vector2(cellX, cellY - cellH); if (hLine != null)
hLine.Rect.sizeDelta = new Vector2(cellW, borderWidth); {
hLine.Image.color = borderColor; hLine.Rect.anchoredPosition = new Vector2(cellX, cellY - cellH);
hLine.Rect.sizeDelta = new Vector2(cellW, borderWidth);
hLine.Image.color = borderColor;
}
var vLine = m_Assembler.GetImage("cell_vline"); var vLine = m_Assembler.GetImage("cell_vline");
vLine.Rect.anchoredPosition = new Vector2(cellX + cellW - borderWidth, cellY); if (vLine != null)
vLine.Rect.sizeDelta = new Vector2(borderWidth, cellH); {
vLine.Image.color = borderColor; vLine.Rect.anchoredPosition = new Vector2(cellX + cellW - borderWidth, cellY);
vLine.Rect.sizeDelta = new Vector2(borderWidth, cellH);
vLine.Image.color = borderColor;
}
if (data == null) continue; if (data == null) continue;
@@ -265,16 +277,19 @@ namespace XericUI.XTable.Rendering.Component
if (!string.IsNullOrEmpty(data.Text)) if (!string.IsNullOrEmpty(data.Text))
{ {
var txt = m_Assembler.GetText("cell_text", cellNS); var txt = m_Assembler.GetText("cell_text", cellNS);
txt.Rect.anchoredPosition = new Vector2(cellX + CellPadding, cellY); if (txt != null)
txt.Rect.sizeDelta = new Vector2(cellW - CellPadding2, cellH); {
txt.Text.text = data.Text; txt.Rect.anchoredPosition = new Vector2(cellX + CellPadding, cellY);
txt.Text.color = GetStyleColor(cellNS, XericUIActionTable.STYLE_CELL_FG_COLOR, txt.Rect.sizeDelta = new Vector2(cellW - CellPadding2, cellH);
XericUIActionTable.FG_COLOR_FALLBACK); txt.Text.SetText(data.Text);
txt.Text.fontSize = GetStyleInt(cellNS, XericUIActionTable.STYLE_CELL_FONT_SIZE, txt.Text.color = GetStyleColor(cellNS, XericUIActionTable.STYLE_CELL_FG_COLOR,
XericUIActionTable.FONT_SIZE_FALLBACK); XericUIActionTable.FG_COLOR_FALLBACK);
txt.Text.alignment = TextAlignmentOptions.Midline; txt.Text.fontSize = GetStyleInt(cellNS, XericUIActionTable.STYLE_CELL_FONT_SIZE,
txt.Text.font = GetStyleFontAsset?.Invoke(cellNS, XericUIActionTable.FONT_SIZE_FALLBACK);
XericUIActionTable.STYLE_CELL_FONT_ASSET) ?? txt.Text.font; txt.Text.alignment = TextAlignmentOptions.Midline;
txt.Text.font = GetStyleFontAsset?.Invoke(cellNS,
XericUIActionTable.STYLE_CELL_FONT_ASSET) ?? txt.Text.font;
}
} }
break; break;
case CellContentType.Image: case CellContentType.Image:
@@ -282,21 +297,27 @@ namespace XericUI.XTable.Rendering.Component
{ {
Sprite sprite = GetOrCreateSprite(data.Image); Sprite sprite = GetOrCreateSprite(data.Image);
var img = m_Assembler.GetImage("cell_image", cellNS); var img = m_Assembler.GetImage("cell_image", cellNS);
img.Rect.anchoredPosition = new Vector2(cellX + CellPadding, cellY); if (img != null)
img.Rect.sizeDelta = new Vector2(cellW - CellPadding2, cellH); {
img.Image.sprite = sprite; img.Rect.anchoredPosition = new Vector2(cellX + CellPadding, cellY);
img.Rect.sizeDelta = new Vector2(cellW - CellPadding2, cellH);
img.Image.sprite = sprite;
}
} }
break; break;
case CellContentType.Prefab: case CellContentType.Prefab:
if (data.Prefab != null && PrefabPool != null) if (data.Prefab != null && PrefabPool != null)
{ {
GameObject instance = PrefabPool.Acquire(data.Prefab, r, c); GameObject instance = PrefabPool.Acquire(data.Prefab, r, c);
RectTransform prt = instance.GetComponent<RectTransform>(); if (instance != null)
prt.anchoredPosition = new Vector2(cellX, cellY); {
prt.sizeDelta = new Vector2(cellW, cellH); RectTransform prt = instance.GetComponent<RectTransform>();
if (ActivePrefabMap != null) prt.anchoredPosition = new Vector2(cellX, cellY);
ActivePrefabMap[(r, c)] = instance; prt.sizeDelta = new Vector2(cellW, cellH);
data.OnPrefabAcquire?.Invoke(instance); if (ActivePrefabMap != null)
ActivePrefabMap[(r, c)] = instance;
data.OnPrefabAcquire?.Invoke(instance);
}
} }
break; break;
} }
@@ -340,27 +361,39 @@ namespace XericUI.XTable.Rendering.Component
if (IsInSelectionRange != null && IsInSelectionRange(r, c)) if (IsInSelectionRange != null && IsInSelectionRange(r, c))
{ {
var selImg = m_Assembler.GetImage("cell_multiselect"); var selImg = m_Assembler.GetImage("cell_multiselect");
selImg.Rect.anchoredPosition = new Vector2(cellX, cellY); if (selImg != null)
selImg.Rect.sizeDelta = new Vector2(cellW, cellH); {
selImg.Image.color = (r == selRow && c == selCol) ? rtFocusColor : rtMultiSelectColor; selImg.Rect.anchoredPosition = new Vector2(cellX, cellY);
selImg.Rect.sizeDelta = new Vector2(cellW, cellH);
selImg.Image.color = (r == selRow && c == selCol) ? rtFocusColor : rtMultiSelectColor;
}
} }
else if (r == selRow && c == selCol) else if (r == selRow && c == selCol)
{ {
var selImg = m_Assembler.GetImage("cell_select"); var selImg = m_Assembler.GetImage("cell_select");
selImg.Rect.anchoredPosition = new Vector2(cellX, cellY); if (selImg != null)
selImg.Rect.sizeDelta = new Vector2(cellW, cellH); {
selImg.Image.color = rtSelectionColor; selImg.Rect.anchoredPosition = new Vector2(cellX, cellY);
selImg.Rect.sizeDelta = new Vector2(cellW, cellH);
selImg.Image.color = rtSelectionColor;
}
} }
var hLine = m_Assembler.GetImage("cell_hline"); var hLine = m_Assembler.GetImage("cell_hline");
hLine.Rect.anchoredPosition = new Vector2(cellX, cellY - cellH); if (hLine != null)
hLine.Rect.sizeDelta = new Vector2(cellW, Config.RuntimeBorderWidth); {
hLine.Image.color = Config.RuntimeBorderColor; hLine.Rect.anchoredPosition = new Vector2(cellX, cellY - cellH);
hLine.Rect.sizeDelta = new Vector2(cellW, Config.RuntimeBorderWidth);
hLine.Image.color = Config.RuntimeBorderColor;
}
var vLine = m_Assembler.GetImage("cell_vline"); var vLine = m_Assembler.GetImage("cell_vline");
vLine.Rect.anchoredPosition = new Vector2(cellX + cellW - Config.RuntimeBorderWidth, cellY); if (vLine != null)
vLine.Rect.sizeDelta = new Vector2(Config.RuntimeBorderWidth, cellH); {
vLine.Image.color = Config.RuntimeBorderColor; vLine.Rect.anchoredPosition = new Vector2(cellX + cellW - Config.RuntimeBorderWidth, cellY);
vLine.Rect.sizeDelta = new Vector2(Config.RuntimeBorderWidth, cellH);
vLine.Image.color = Config.RuntimeBorderColor;
}
if (data == null) continue; if (data == null) continue;
@@ -370,9 +403,12 @@ namespace XericUI.XTable.Rendering.Component
if (!string.IsNullOrEmpty(data.Text)) if (!string.IsNullOrEmpty(data.Text))
{ {
var txt = m_Assembler.GetText("cell_text", cellNS); var txt = m_Assembler.GetText("cell_text", cellNS);
txt.Rect.anchoredPosition = new Vector2(cellX + CellPadding, cellY); if (txt != null)
txt.Rect.sizeDelta = new Vector2(cellW - CellPadding2, cellH); {
txt.Text.text = data.Text; txt.Rect.anchoredPosition = new Vector2(cellX + CellPadding, cellY);
txt.Rect.sizeDelta = new Vector2(cellW - CellPadding2, cellH);
txt.Text.SetText(data.Text);
}
} }
break; break;
case CellContentType.Image: case CellContentType.Image:
@@ -380,21 +416,27 @@ namespace XericUI.XTable.Rendering.Component
{ {
Sprite sprite = GetOrCreateSprite(data.Image); Sprite sprite = GetOrCreateSprite(data.Image);
var img = m_Assembler.GetImage("cell_image", cellNS); var img = m_Assembler.GetImage("cell_image", cellNS);
img.Rect.anchoredPosition = new Vector2(cellX + CellPadding, cellY); if (img != null)
img.Rect.sizeDelta = new Vector2(cellW - CellPadding2, cellH); {
img.Image.sprite = sprite; img.Rect.anchoredPosition = new Vector2(cellX + CellPadding, cellY);
img.Rect.sizeDelta = new Vector2(cellW - CellPadding2, cellH);
img.Image.sprite = sprite;
}
} }
break; break;
case CellContentType.Prefab: case CellContentType.Prefab:
if (data.Prefab != null && PrefabPool != null) if (data.Prefab != null && PrefabPool != null)
{ {
GameObject instance = PrefabPool.Acquire(data.Prefab, r, c); GameObject instance = PrefabPool.Acquire(data.Prefab, r, c);
RectTransform prt = instance.GetComponent<RectTransform>(); if (instance != null)
prt.anchoredPosition = new Vector2(cellX, cellY); {
prt.sizeDelta = new Vector2(cellW, cellH); RectTransform prt = instance.GetComponent<RectTransform>();
if (ActivePrefabMap != null) prt.anchoredPosition = new Vector2(cellX, cellY);
ActivePrefabMap[(r, c)] = instance; prt.sizeDelta = new Vector2(cellW, cellH);
data.OnPrefabAcquire?.Invoke(instance); if (ActivePrefabMap != null)
ActivePrefabMap[(r, c)] = instance;
data.OnPrefabAcquire?.Invoke(instance);
}
} }
break; break;
} }
@@ -195,6 +195,7 @@ namespace XericUI.XTable.Rendering.Component
string key = inRange ? "cell_multiselect" : "cell_select"; string key = inRange ? "cell_multiselect" : "cell_select";
var img = m_Assembler.GetImage(key, cellNS); var img = m_Assembler.GetImage(key, cellNS);
if (img == null) return;
img.Rect.anchoredPosition = new Vector2(cellX, cellY); img.Rect.anchoredPosition = new Vector2(cellX, cellY);
img.Rect.sizeDelta = new Vector2(cellW, cellH); img.Rect.sizeDelta = new Vector2(cellW, cellH);
@@ -240,17 +241,23 @@ namespace XericUI.XTable.Rendering.Component
// 水平网格线(底部) // 水平网格线(底部)
var hLine = m_Assembler.GetImage("cell_hline", cellNS); var hLine = m_Assembler.GetImage("cell_hline", cellNS);
hLine.Rect.anchoredPosition = new Vector2(cellX, cellY - cellH); if (hLine != null)
hLine.Rect.sizeDelta = new Vector2(cellW, borderWidth); {
hLine.Image.color = borderColor; hLine.Rect.anchoredPosition = new Vector2(cellX, cellY - cellH);
objs.HLine = hLine; hLine.Rect.sizeDelta = new Vector2(cellW, borderWidth);
hLine.Image.color = borderColor;
objs.HLine = hLine;
}
// 垂直网格线(右侧) // 垂直网格线(右侧)
var vLine = m_Assembler.GetImage("cell_vline", cellNS); var vLine = m_Assembler.GetImage("cell_vline", cellNS);
vLine.Rect.anchoredPosition = new Vector2(cellX + cellW - borderWidth, cellY); if (vLine != null)
vLine.Rect.sizeDelta = new Vector2(borderWidth, cellH); {
vLine.Image.color = borderColor; vLine.Rect.anchoredPosition = new Vector2(cellX + cellW - borderWidth, cellY);
objs.VLine = vLine; vLine.Rect.sizeDelta = new Vector2(borderWidth, cellH);
vLine.Image.color = borderColor;
objs.VLine = vLine;
}
} }
private void RenderCellText(XTableCellData data, private void RenderCellText(XTableCellData data,
@@ -260,9 +267,10 @@ namespace XericUI.XTable.Rendering.Component
if (string.IsNullOrEmpty(data.Text)) return; if (string.IsNullOrEmpty(data.Text)) return;
var txt = m_Assembler.GetText("cell_text", cellNS); var txt = m_Assembler.GetText("cell_text", cellNS);
if (txt == null) return;
txt.Rect.anchoredPosition = new Vector2(cellX + CellPadding, cellY); txt.Rect.anchoredPosition = new Vector2(cellX + CellPadding, cellY);
txt.Rect.sizeDelta = new Vector2(cellW - CellPadding2, cellH); txt.Rect.sizeDelta = new Vector2(cellW - CellPadding2, cellH);
txt.Text.text = data.Text; txt.Text.SetText(data.Text);
if (editorMode) if (editorMode)
{ {
@@ -283,6 +291,7 @@ namespace XericUI.XTable.Rendering.Component
Sprite sprite = GetOrCreateSprite(data.Image); Sprite sprite = GetOrCreateSprite(data.Image);
var img = m_Assembler.GetImage("cell_image", cellNS); var img = m_Assembler.GetImage("cell_image", cellNS);
if (img == null) return;
img.Rect.anchoredPosition = new Vector2(cellX + CellPadding, cellY); img.Rect.anchoredPosition = new Vector2(cellX + CellPadding, cellY);
img.Rect.sizeDelta = new Vector2(cellW - CellPadding2, cellH); img.Rect.sizeDelta = new Vector2(cellW - CellPadding2, cellH);
img.Image.sprite = sprite; img.Image.sprite = sprite;
@@ -296,6 +305,7 @@ namespace XericUI.XTable.Rendering.Component
if (data.Prefab == null) return; if (data.Prefab == null) return;
GameObject instance = m_PrefabPool.Acquire(data.Prefab, row, col); GameObject instance = m_PrefabPool.Acquire(data.Prefab, row, col);
if (instance == null) return;
RectTransform prt = instance.GetComponent<RectTransform>(); RectTransform prt = instance.GetComponent<RectTransform>();
prt.anchoredPosition = new Vector2(cellX, cellY); prt.anchoredPosition = new Vector2(cellX, cellY);
prt.sizeDelta = new Vector2(cellW, cellH); prt.sizeDelta = new Vector2(cellW, cellH);
@@ -471,16 +481,14 @@ namespace XericUI.XTable.Rendering.Component
private void CleanOrphanChildren() private void CleanOrphanChildren()
{ {
Transform poolRoot = m_Assembler?.PoolRoot;
Transform prefabPoolRoot = m_PrefabPool?.PoolRoot;
Transform contentT = ContentTransform; Transform contentT = ContentTransform;
Transform borderT = contentT?.Find("_TableBorders"); Transform borderT = contentT?.Find("_TableBorders");
for (int i = contentT.childCount - 1; i >= 0; i--) for (int i = contentT.childCount - 1; i >= 0; i--)
{ {
var child = contentT.GetChild(i); var child = contentT.GetChild(i);
if (poolRoot != null && child == poolRoot) continue; // 跳过池管理的对象(标记有 POOL_FLAGS = DontSave | HideInHierarchy)
if (prefabPoolRoot != null && child == prefabPoolRoot) continue; if ((child.hideFlags & HideFlags.DontSave) != 0) continue;
if (m_BackgroundGo != null && child.gameObject == m_BackgroundGo) continue; if (m_BackgroundGo != null && child.gameObject == m_BackgroundGo) continue;
if (borderT != null && child == borderT) continue; if (borderT != null && child == borderT) continue;
@@ -37,10 +37,6 @@ namespace XericUI.XTable.Rendering.Elements
#region 字段 #region 字段
private Transform m_Parent; private Transform m_Parent;
private Transform m_PoolRoot;
/// <summary>池根节点(所有回收对象均挂在此节点下,节点本身处于 inactive)</summary>
public Transform PoolRoot => m_PoolRoot;
/// <summary>运行时的默认样式命名空间(编辑器下为 null,不绑定)</summary> /// <summary>运行时的默认样式命名空间(编辑器下为 null,不绑定)</summary>
private string m_DefaultStyleNamespace; private string m_DefaultStyleNamespace;
@@ -48,14 +44,20 @@ namespace XericUI.XTable.Rendering.Elements
/// <summary>所有已绑定的 StyleBoundElement 回调集合(用于样式变更时通知外部)</summary> /// <summary>所有已绑定的 StyleBoundElement 回调集合(用于样式变更时通知外部)</summary>
public event System.Action OnStyleChangeDetected; public event System.Action OnStyleChangeDetected;
private Stack<PooledImage> m_ImagePool = new Stack<PooledImage>(); private Queue<PooledImage> m_ImagePool = new Queue<PooledImage>();
private Stack<PooledText> m_TextPool = new Stack<PooledText>(); private Queue<PooledText> m_TextPool = new Queue<PooledText>();
private List<PooledImage> m_ActiveImages = new List<PooledImage>(); private List<PooledImage> m_ActiveImages = new List<PooledImage>();
private List<PooledText> m_ActiveTexts = new List<PooledText>(); private List<PooledText> m_ActiveTexts = new List<PooledText>();
/// <summary>HideFlags 应用于所有动态创建的对象(不保存、不在 Hierarchy 显示)</summary> /// <summary>HideFlags 应用于所有动态创建的对象(不保存、不在 Hierarchy 显示)</summary>
private const HideFlags POOL_FLAGS = HideFlags.DontSave | HideFlags.HideInHierarchy; 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 #endregion
#region 构造函数与析构 #region 构造函数与析构
@@ -64,12 +66,6 @@ namespace XericUI.XTable.Rendering.Elements
{ {
m_Parent = parent; m_Parent = parent;
m_DefaultStyleNamespace = defaultStyleNamespace; m_DefaultStyleNamespace = defaultStyleNamespace;
GameObject poolRoot = new GameObject("_CellPool");
poolRoot.hideFlags = POOL_FLAGS;
poolRoot.transform.SetParent(parent, false);
poolRoot.SetActive(false);
m_PoolRoot = poolRoot.transform;
} }
/// <summary>彻底销毁所有池对象(包括活跃和休眠的)</summary> /// <summary>彻底销毁所有池对象(包括活跃和休眠的)</summary>
@@ -91,18 +87,11 @@ namespace XericUI.XTable.Rendering.Elements
// 杀死池中对象 // 杀死池中对象
while (m_ImagePool.Count > 0) while (m_ImagePool.Count > 0)
{ {
KillObject(m_ImagePool.Pop().Root); KillObject(m_ImagePool.Dequeue().Root);
} }
while (m_TextPool.Count > 0) while (m_TextPool.Count > 0)
{ {
KillObject(m_TextPool.Pop().Root); KillObject(m_TextPool.Dequeue().Root);
}
// 杀死池根
if (m_PoolRoot != null)
{
KillObject(m_PoolRoot.gameObject);
m_PoolRoot = null;
} }
} }
@@ -132,14 +121,18 @@ namespace XericUI.XTable.Rendering.Elements
PooledImage item; PooledImage item;
if (m_ImagePool.Count > 0) if (m_ImagePool.Count > 0)
{ {
item = m_ImagePool.Pop(); item = m_ImagePool.Dequeue();
item.Root.SetActive(true);
item.Root.transform.SetParent(m_Parent, false);
ResetImageDefaults(item); ResetImageDefaults(item);
} }
else if (m_ImageCreatedCount < MAX_POOL_ITEMS)
{
item = CreateImage(name);
m_ImageCreatedCount++;
}
else else
{ {
item = CreateImage(name); Debug.LogWarning($"[XericUIActionTable] Image 池已达上限 {MAX_POOL_ITEMS},无法分配新对象");
return null;
} }
BindStyle(item.Style, styleNamespace); BindStyle(item.Style, styleNamespace);
m_ActiveImages.Add(item); m_ActiveImages.Add(item);
@@ -151,14 +144,18 @@ namespace XericUI.XTable.Rendering.Elements
PooledText item; PooledText item;
if (m_TextPool.Count > 0) if (m_TextPool.Count > 0)
{ {
item = m_TextPool.Pop(); item = m_TextPool.Dequeue();
item.Root.SetActive(true);
item.Root.transform.SetParent(m_Parent, false);
ResetTextDefaults(item); ResetTextDefaults(item);
} }
else if (m_TextCreatedCount < MAX_POOL_ITEMS)
{
item = CreateText(name);
m_TextCreatedCount++;
}
else else
{ {
item = CreateText(name); Debug.LogWarning($"[XericUIActionTable] Text 池已达上限 {MAX_POOL_ITEMS},无法分配新对象");
return null;
} }
BindStyle(item.Style, styleNamespace); BindStyle(item.Style, styleNamespace);
m_ActiveTexts.Add(item); m_ActiveTexts.Add(item);
@@ -214,18 +211,13 @@ namespace XericUI.XTable.Rendering.Elements
private void ReturnImage(PooledImage item) private void ReturnImage(PooledImage item)
{ {
item.Style?.Unbind(); item.Style?.Unbind();
item.Root.SetActive(false); m_ImagePool.Enqueue(item);
item.Root.transform.SetParent(m_PoolRoot, false);
m_ImagePool.Push(item);
} }
private void ReturnText(PooledText item) private void ReturnText(PooledText item)
{ {
item.Style?.Unbind(); item.Style?.Unbind();
item.Text.text = ""; m_TextPool.Enqueue(item);
item.Root.SetActive(false);
item.Root.transform.SetParent(m_PoolRoot, false);
m_TextPool.Push(item);
} }
#endregion #endregion
@@ -251,7 +243,7 @@ namespace XericUI.XTable.Rendering.Elements
item.Rect.pivot = PIVOT_TOPLEFT; item.Rect.pivot = PIVOT_TOPLEFT;
item.Rect.anchoredPosition = Vector2.zero; item.Rect.anchoredPosition = Vector2.zero;
item.Rect.sizeDelta = Vector2.zero; item.Rect.sizeDelta = Vector2.zero;
item.Text.text = ""; item.Text.SetText(string.Empty);
item.Text.color = Color.black; item.Text.color = Color.black;
item.Text.fontSize = 14; item.Text.fontSize = 14;
item.Text.alignment = TextAlignmentOptions.Midline; item.Text.alignment = TextAlignmentOptions.Midline;
@@ -1,13 +1,12 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.Pool;
namespace XericUI.XTable.Rendering.Elements namespace XericUI.XTable.Rendering.Elements
{ {
/// <summary> /// <summary>
/// 预制体对象池——按原型分组管理,每个预制体对应一个独立的对象池。 /// 预制体对象池——按原型分组管理,每个预制体对应一个独立的对象池。
/// 使用 Stack + HashSet 模式与 <see cref="CellAssembler"/> 保持一致。 /// 使用 Queue + HashSet 模式,FIFO 保证对象按回收顺序均匀复用。
/// </summary> /// </summary>
public class PrefabPrototypePool public class PrefabPrototypePool
{ {
@@ -16,8 +15,8 @@ namespace XericUI.XTable.Rendering.Elements
private class PrefabPool private class PrefabPool
{ {
public GameObject Prototype; public GameObject Prototype;
/// <summary>休眠实例栈(池满时先归还的优先复用)</summary> /// <summary>休眠实例队列(FIFO,先归还的优先复用)</summary>
public Stack<GameObject> Available = new Stack<GameObject>(); public Queue<GameObject> Available = new Queue<GameObject>();
/// <summary>活跃实例集合(O(1) 查找 + 防止重复归还)</summary> /// <summary>活跃实例集合(O(1) 查找 + 防止重复归还)</summary>
public HashSet<GameObject> Active = new HashSet<GameObject>(); public HashSet<GameObject> Active = new HashSet<GameObject>();
} }
@@ -28,10 +27,10 @@ namespace XericUI.XTable.Rendering.Elements
private Dictionary<GameObject, PrefabPool> m_Pools = new Dictionary<GameObject, PrefabPool>(); private Dictionary<GameObject, PrefabPool> m_Pools = new Dictionary<GameObject, PrefabPool>();
private Transform m_Parent; private Transform m_Parent;
private Transform m_PoolRoot;
/// <summary>池根节点(所有回收对象均挂在此节点下,节点本身处于 inactive)</summary> /// <summary>渲染项目总数上限</summary>
public Transform PoolRoot => m_PoolRoot; private const int MAX_PREFAB_ITEMS = 256;
private int m_TotalCreated;
private const HideFlags POOL_FLAGS = HideFlags.DontSave | HideFlags.HideInHierarchy; private const HideFlags POOL_FLAGS = HideFlags.DontSave | HideFlags.HideInHierarchy;
@@ -45,15 +44,9 @@ namespace XericUI.XTable.Rendering.Elements
public PrefabPrototypePool(Transform parent) public PrefabPrototypePool(Transform parent)
{ {
m_Parent = parent; m_Parent = parent;
GameObject poolRoot = new GameObject("_PrefabPoolRoot");
poolRoot.hideFlags = POOL_FLAGS;
poolRoot.transform.SetParent(parent, false);
poolRoot.SetActive(false);
m_PoolRoot = poolRoot.transform;
} }
/// <summary>彻底销毁所有池对象(活跃 + 休眠 + 池根)</summary> /// <summary>彻底销毁所有池对象(活跃 + 休眠)</summary>
public void DestroyAll() public void DestroyAll()
{ {
foreach (var kv in m_Pools) foreach (var kv in m_Pools)
@@ -65,15 +58,9 @@ namespace XericUI.XTable.Rendering.Elements
pool.Active.Clear(); pool.Active.Clear();
// 销毁休眠实例 // 销毁休眠实例
while (pool.Available.Count > 0) while (pool.Available.Count > 0)
KillObject(pool.Available.Pop()); KillObject(pool.Available.Dequeue());
} }
m_Pools.Clear(); m_Pools.Clear();
if (m_PoolRoot != null)
{
KillObject(m_PoolRoot.gameObject);
m_PoolRoot = null;
}
} }
private static void KillObject(GameObject go) private static void KillObject(GameObject go)
@@ -89,7 +76,7 @@ namespace XericUI.XTable.Rendering.Elements
#region 池操作 #region 池操作
/// <summary>从池中获取实例(池空则 Instantiate 新的)</summary> /// <summary>从池中获取实例(FIFO 复用;池空且未达上限则 Instantiate;已达上限返回 null)</summary>
public GameObject Acquire(GameObject prefab, int row, int col) public GameObject Acquire(GameObject prefab, int row, int col)
{ {
if (prefab == null) return null; if (prefab == null) return null;
@@ -99,14 +86,18 @@ namespace XericUI.XTable.Rendering.Elements
GameObject instance; GameObject instance;
if (pool.Available.Count > 0) if (pool.Available.Count > 0)
{ {
instance = pool.Available.Pop(); instance = pool.Available.Dequeue();
instance.SetActive(true);
instance.transform.SetParent(m_Parent, false);
} }
else else if (m_TotalCreated < MAX_PREFAB_ITEMS)
{ {
instance = Object.Instantiate(prefab, m_Parent); instance = Object.Instantiate(prefab, m_Parent);
instance.hideFlags = POOL_FLAGS; instance.hideFlags = POOL_FLAGS;
m_TotalCreated++;
}
else
{
Debug.LogWarning($"[XericUIActionTable] Prefab 池已达上限 {MAX_PREFAB_ITEMS},无法分配新实例");
return null;
} }
instance.name = $"_prefab_{row}_{col}"; instance.name = $"_prefab_{row}_{col}";
@@ -122,7 +113,7 @@ namespace XericUI.XTable.Rendering.Elements
return instance; return instance;
} }
/// <summary>将指定实例归还对象池(Deactivate + 移入 PoolRoot)</summary> /// <summary>将指定实例归还对象池(不入池根,原地保留避免 OnEnable/OnDisable)</summary>
public void Release(GameObject instance) public void Release(GameObject instance)
{ {
if (instance == null) return; if (instance == null) return;
@@ -132,9 +123,7 @@ namespace XericUI.XTable.Rendering.Elements
{ {
if (kv.Value.Active.Remove(instance)) if (kv.Value.Active.Remove(instance))
{ {
instance.SetActive(false); kv.Value.Available.Enqueue(instance);
instance.transform.SetParent(m_PoolRoot, false);
kv.Value.Available.Push(instance);
return; return;
} }
} }
@@ -143,7 +132,7 @@ namespace XericUI.XTable.Rendering.Elements
KillObject(instance); KillObject(instance);
} }
/// <summary>将所有活跃实例回收至池</summary> /// <summary>将所有活跃实例回收至池(原地保留,仅从活跃集移除并入队)</summary>
public void ReturnAll() public void ReturnAll()
{ {
foreach (var kv in m_Pools) foreach (var kv in m_Pools)
@@ -151,9 +140,7 @@ namespace XericUI.XTable.Rendering.Elements
var pool = kv.Value; var pool = kv.Value;
foreach (var go in pool.Active) foreach (var go in pool.Active)
{ {
go.SetActive(false); pool.Available.Enqueue(go);
go.transform.SetParent(m_PoolRoot, false);
pool.Available.Push(go);
} }
pool.Active.Clear(); pool.Active.Clear();
} }