Files
test/Assets/PerfectWorld/Scripts/UI/ItemPanelPool.cs
2025-10-16 10:25:49 +07:00

194 lines
5.0 KiB
C#

using System.Collections.Generic;
using UnityEngine;
public class ItemPanelPool : MonoBehaviour
{
[Header("Pool Settings")]
public GameObject itemPanelPrefab;
public int initialPoolSize = 20;
public int maxPoolSize = 100;
public bool expandPool = true;
[Header("Pool Management")]
public Transform poolParent; // Where inactive objects are stored
private Queue<GameObject> availablePanels = new Queue<GameObject>();
private List<GameObject> allPanels = new List<GameObject>();
private int currentPoolSize = 0;
void Start()
{
InitializePool();
}
void InitializePool()
{
if (itemPanelPrefab == null)
{
Debug.LogError("ItemPanelPrefab is not assigned!");
return;
}
// Create pool parent if not assigned
if (poolParent == null)
{
GameObject poolParentObj = new GameObject("ItemPanelPool");
poolParentObj.transform.SetParent(transform);
poolParent = poolParentObj.transform;
}
// Pre-populate pool
for (int i = 0; i < initialPoolSize; i++)
{
CreateNewPanel();
}
Debug.Log($"ItemPanelPool initialized with {currentPoolSize} panels");
}
GameObject CreateNewPanel()
{
if (itemPanelPrefab == null) return null;
GameObject newPanel = Instantiate(itemPanelPrefab, poolParent);
newPanel.SetActive(false);
// Add ShopItemPanel component if not present
ShopItemPanel panelScript = newPanel.GetComponent<ShopItemPanel>();
if (panelScript == null)
{
panelScript = newPanel.AddComponent<ShopItemPanel>();
}
availablePanels.Enqueue(newPanel);
allPanels.Add(newPanel);
currentPoolSize++;
return newPanel;
}
public GameObject GetPanel()
{
GameObject panel = null;
// Try to get from available pool
if (availablePanels.Count > 0)
{
panel = availablePanels.Dequeue();
}
// Create new panel if pool is empty and we can expand
else if (expandPool && currentPoolSize < maxPoolSize)
{
panel = CreateNewPanel();
if (panel != null)
{
panel = availablePanels.Dequeue(); // Get the panel we just created
}
}
// Pool is full and can't expand
else
{
Debug.LogWarning("ItemPanelPool is full and cannot expand further!");
return null;
}
if (panel != null)
{
panel.SetActive(true);
// Reset the panel state
ShopItemPanel panelScript = panel.GetComponent<ShopItemPanel>();
if (panelScript != null)
{
panelScript.ResetPanel(); // Reset for reuse
}
}
return panel;
}
public void ReturnPanel(GameObject panel)
{
if (panel == null) return;
// Reset panel state
ShopItemPanel panelScript = panel.GetComponent<ShopItemPanel>();
if (panelScript != null)
{
panelScript.ResetPanel(); // Reset for reuse
}
// Deactivate and move to pool parent
panel.SetActive(false);
panel.transform.SetParent(poolParent);
panel.transform.localPosition = Vector3.zero;
panel.transform.localRotation = Quaternion.identity;
panel.transform.localScale = Vector3.one;
// Add back to available pool
availablePanels.Enqueue(panel);
}
public void ReturnAllPanels()
{
// Return all active panels to pool
foreach (GameObject panel in allPanels)
{
if (panel != null && panel.activeInHierarchy)
{
ReturnPanel(panel);
}
}
}
public int GetAvailableCount()
{
return availablePanels.Count;
}
public int GetActiveCount()
{
int activeCount = 0;
foreach (GameObject panel in allPanels)
{
if (panel != null && panel.activeInHierarchy)
{
activeCount++;
}
}
return activeCount;
}
public int GetTotalCount()
{
return currentPoolSize;
}
public void ClearPool()
{
// Destroy all panels
foreach (GameObject panel in allPanels)
{
if (panel != null)
{
DestroyImmediate(panel);
}
}
availablePanels.Clear();
allPanels.Clear();
currentPoolSize = 0;
}
void OnDestroy()
{
ClearPool();
}
[ContextMenu("Log Pool Status")]
void LogPoolStatus()
{
Debug.Log($"Pool Status - Available: {GetAvailableCount()}, Active: {GetActiveCount()}, Total: {GetTotalCount()}");
}
}