is there any bug with the Unity Object pooling library or something wrong with my code? only instantiate one object, even due I set initialize to 8 and maximum to 8.
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;
public class PoolSystemManager : MonoBehaviour
{
public static PoolSystemManager _Instance { get; private set; }
public enum PoolType
{
None,
Fern01,
FernSword,
Bush01
}
[Serializable]
public struct Pool
{
public PoolType poolKey; // Enum pool key
public GameObject prefab; // Prefab to pool
public int initialPoolSize; // Initial pool size
public int maxPoolSize; // Max pool size
public Transform parent; // Parent object for organizing the pooled objects
}
[SerializeField]
private List<Pool> pools; // List of pools to be created, editable in the inspector
private Dictionary<PoolType, ObjectPool<IPoolableObject>> poolDictionary;
private void Awake()
{
if (_Instance == null)
{
_Instance = this;
}
else
{
Destroy(gameObject);
return;
}
InitializePools();
}
// Initialize all pools based on the inspector list
private void InitializePools()
{
poolDictionary = new Dictionary<PoolType, ObjectPool<IPoolableObject>>();
foreach (var pool in pools)
{
if (!poolDictionary.ContainsKey(pool.poolKey))
{
ObjectPool<IPoolableObject> objectPool = new ObjectPool<IPoolableObject>(
createFunc: () => CreatePooledObject(pool.prefab, pool.parent),
actionOnGet: obj => OnObjectGet(obj),
actionOnRelease: obj => OnObjectRelease(obj),
actionOnDestroy: obj => Destroy(obj.GetGameObject()),
collectionCheck: false,
defaultCapacity: pool.initialPoolSize,
maxSize: pool.maxPoolSize);
poolDictionary[pool.poolKey] = objectPool;
print(objectPool.CountAll);
print(objectPool.CountActive);
// Pre-populate the pool
PrepopulatePool(pool.poolKey, pool.initialPoolSize);
print(objectPool.CountAll);
print(objectPool.CountActive);
}
else
{
Debug.LogWarning($"Pool with key {pool.poolKey} already exists.");
}
}
Debug.Log("Pools initialized successfully.");
}
// Instantiate the prefab and get the IPoolableObject reference
private IPoolableObject CreatePooledObject(GameObject prefab, Transform parent)
{
//// Pick a random prefab from the array
//int randomIndex = UnityEngine.Random.Range(0, prefabs.Length);
//GameObject selectedPrefab = prefabs[randomIndex];
GameObject obj = Instantiate(prefab);
obj.SetActive(false);
obj.transform.SetParent(parent);
IPoolableObject poolableObject = obj.GetComponent<IPoolableObject>();
return poolableObject;
}
private void PrepopulatePool(PoolType poolKey, int count)
{
if (poolDictionary.TryGetValue(poolKey, out var pool))
{
for (int i = 0; i < count; i++)
{
var obj = pool.Get(); // Create the object
pool.Release(obj); // Immediately release it to the pool
}
Debug.Log($"Pool {poolKey} pre-populated with {count} objects.");
}
else
{
Debug.LogError($"Pool with key {poolKey} not found.");
}
}
// Called when the object is pulled from the pool
private void OnObjectGet(IPoolableObject poolableObject)
{
poolableObject.OnPoolGet();
poolableObject.GetGameObject().SetActive(true);
}
// Called when the object is released to the pool
private void OnObjectRelease(IPoolableObject poolableObject)
{
poolableObject.OnPoolRelease();
poolableObject.GetGameObject().SetActive(false);
}
// Pull an object from the pool
public IPoolableObject GetFromPool(PoolType key, Vector3 position = default, Quaternion rotation = default)
{
if (poolDictionary.ContainsKey(key))
{
IPoolableObject obj = poolDictionary[key].Get();
obj.GetGameObject().transform.position = position;
obj.GetGameObject().transform.rotation = rotation;
return obj;
}
return null;
}
// Release an object back to the pool
public void ReleaseToPool(PoolType key, IPoolableObject obj)
{
if (poolDictionary.ContainsKey(key))
{
poolDictionary[key].Release(obj);
}
}
// Return statistics for debugging
public string GetPoolStats(PoolType key)
{
if (poolDictionary.ContainsKey(key))
{
var pool = poolDictionary[key];
return $"Pool {key}: Active: {pool.CountActive}, Inactive: {pool.CountInactive}, Total:
{pool.CountAll}";
}
return $"Pool {key} not found.";
}
}