TowerDefenseGame/Assets/Scripts/Runtime/Environment/GameObjectPool.cs
Nico a52bbb0849 Create object pooling system
Optimize VFX and builder handling mechanics
2025-06-28 11:00:01 -07:00

34 lines
1.0 KiB
C#

using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
using UnityEngine.Pool;
public class GameObjectPool {
private ObjectPool<GameObject> Pool;
public GameObjectPool(GameObject prefab, int defaultSize = 10, int maxSize = 50) {
var container = new GameObject(prefab.name + " Pool");
Pool = new ObjectPool<GameObject>(
createFunc: () => {
var obj = Object.Instantiate(prefab, container.transform);
obj.SetActive(false);
return obj;
},
actionOnGet: obj => { obj.SetActive(true); },
actionOnRelease: obj => { obj.SetActive(false); },
actionOnDestroy: obj => Object.Destroy(obj),
collectionCheck: true,
defaultCapacity: defaultSize,
maxSize: maxSize
);
}
public GameObject Get(Vector3 position, Quaternion rotation) {
var obj = Pool.Get();
obj.transform.SetPositionAndRotation(position, rotation);
return obj;
}
public void Release(GameObject obj) => Pool.Release(obj);
}