I’m trying to set up a pooling system for tiles in the level of a runner game. I have a parent class Tile, which I set up a pool for below:
private ObjectPool<Tile> _tilePool;
_tilePool = new ObjectPool<Tile>(
createFunc: () => {
var newTile = Instantiate(templateTile);
newTile.gameObject.SetActive(true);
newTile.transform.parent = _tilePoolTransform;
newTile.transform.localPosition = Vector3.zero;
return newTile;
},
actionOnGet: (obj) => {
obj.gameObject.SetActive(true);
},
actionOnRelease: (obj) => {
obj.transform.parent = _tilePoolTransform;
obj.transform.localPosition = Vector3.zero;
obj.gameObject.SetActive(false);
},
actionOnDestroy: (obj) => Destroy(obj.gameObject),
collectionCheck: false,
defaultCapacity: 50,
maxSize: 100
);
The problem is, Tile has many subclasses, such as SpikeTile, EnemyTile, CoinTile, and this object pool doesn’t seem to let me spawn and recycle them. What’s the best way to set up a pool that lets me access all these subclasses?