Files
AppleHillsProduction/Assets/Scripts/Minigames/DivingForPictures/ObstaclePool.cs

55 lines
2.2 KiB
C#
Raw Normal View History

2025-09-17 16:10:18 +02:00
using UnityEngine;
using Pooling;
namespace Minigames.DivingForPictures
{
/// <summary>
/// Manages a pool of floating obstacle objects to reduce garbage collection overhead.
/// Optimized for handling a large number of different obstacle prefab types.
/// </summary>
public class ObstaclePool : MultiPrefabPool<FloatingObstacle>
{
/// <summary>
/// Returns an obstacle to the pool
/// </summary>
/// <param name="obstacle">The obstacle to return to the pool</param>
/// <param name="prefabIndex">The index of the prefab this obstacle was created from</param>
public void ReturnObstacle(GameObject obstacle, int prefabIndex)
{
if (obstacle != null)
{
FloatingObstacle obstacleComponent = obstacle.GetComponent<FloatingObstacle>();
if (obstacleComponent != null)
{
2025-09-18 15:51:18 +02:00
Debug.Log($"[ObstaclePool] Returning obstacle {obstacle.name} to pool");
2025-09-17 16:10:18 +02:00
Return(obstacleComponent, prefabIndex);
}
else
{
Debug.LogWarning($"Attempted to return a GameObject without a FloatingObstacle component: {obstacle.name}");
Destroy(obstacle);
}
}
}
/// <summary>
/// Gets an obstacle from the pool, or creates a new one if the pool is empty
/// </summary>
/// <returns>An obstacle instance ready to use</returns>
public GameObject GetObstacle(int prefabIndex)
{
2025-09-18 15:51:18 +02:00
Debug.Log($"[ObstaclePool] GetObstacle called for prefab index {prefabIndex}");
2025-09-17 16:10:18 +02:00
FloatingObstacle obstacleComponent = Get(prefabIndex);
2025-09-18 15:51:18 +02:00
if (obstacleComponent == null)
{
Debug.LogError($"[ObstaclePool] Get() returned null for prefab index {prefabIndex}");
return null;
}
Debug.Log($"[ObstaclePool] Get() returned obstacle {obstacleComponent.name}, active state: {obstacleComponent.gameObject.activeInHierarchy}");
2025-09-17 16:10:18 +02:00
return obstacleComponent.gameObject;
}
}
}