2025-09-16 12:23:25 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using UnityEngine;
|
2025-09-16 15:02:50 +02:00
|
|
|
|
using Pooling;
|
2025-09-16 12:23:25 +02:00
|
|
|
|
|
|
|
|
|
|
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>
|
2025-09-16 15:02:50 +02:00
|
|
|
|
public class TrenchTilePool : MultiPrefabPool<Tile>
|
2025-09-16 12:23:25 +02:00
|
|
|
|
{
|
|
|
|
|
|
/// <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)
|
|
|
|
|
|
{
|
2025-09-16 15:02:50 +02:00
|
|
|
|
if (tile != null)
|
2025-09-16 12:23:25 +02:00
|
|
|
|
{
|
2025-09-16 15:02:50 +02:00
|
|
|
|
Tile tileComponent = tile.GetComponent<Tile>();
|
|
|
|
|
|
if (tileComponent != null)
|
2025-09-16 12:23:25 +02:00
|
|
|
|
{
|
2025-09-16 15:02:50 +02:00
|
|
|
|
Return(tileComponent, prefabIndex);
|
2025-09-16 12:23:25 +02:00
|
|
|
|
}
|
2025-09-16 15:02:50 +02:00
|
|
|
|
else
|
2025-09-16 12:23:25 +02:00
|
|
|
|
{
|
2025-09-16 15:02:50 +02:00
|
|
|
|
Debug.LogWarning($"Attempted to return a GameObject without a Tile component: {tile.name}");
|
2025-09-16 12:23:25 +02:00
|
|
|
|
Destroy(tile);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-16 15:02:50 +02:00
|
|
|
|
/// Gets a tile from the pool, or creates a new one if the pool is empty
|
2025-09-16 12:23:25 +02:00
|
|
|
|
/// </summary>
|
2025-09-16 15:02:50 +02:00
|
|
|
|
/// <returns>A tile instance ready to use</returns>
|
|
|
|
|
|
public GameObject GetTile(int prefabIndex)
|
2025-09-16 12:23:25 +02:00
|
|
|
|
{
|
2025-09-16 15:02:50 +02:00
|
|
|
|
Tile tileComponent = Get(prefabIndex);
|
|
|
|
|
|
return tileComponent.gameObject;
|
2025-09-16 12:23:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|