Files
AppleHillsProduction/Assets/Scripts/Minigames/DivingForPictures/Tiles/TrenchTilePool.cs

46 lines
1.5 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using Pooling;
namespace Minigames.DivingForPictures
{
/// <summary>
/// Manages a pool of trench tile objects to reduce garbage collection overhead.
/// Optimized for handling a large number of different prefab types.
/// </summary>
public class TrenchTilePool : MultiPrefabPool<Tile>
{
/// <summary>
/// Returns a tile to the pool
/// </summary>
/// <param name="tile">The tile to return to the pool</param>
/// <param name="prefabIndex">The index of the prefab this tile was created from</param>
public void ReturnTile(GameObject tile, int prefabIndex)
{
if (tile != null)
{
Tile tileComponent = tile.GetComponent<Tile>();
if (tileComponent != null)
{
Return(tileComponent, prefabIndex);
}
else
{
Debug.LogWarning($"Attempted to return a GameObject without a Tile component: {tile.name}");
Destroy(tile);
}
}
}
/// <summary>
/// Gets a tile from the pool, or creates a new one if the pool is empty
/// </summary>
/// <returns>A tile instance ready to use</returns>
public GameObject GetTile(int prefabIndex)
{
Tile tileComponent = Get(prefabIndex);
return tileComponent.gameObject;
}
}
}