Tested and packed in Unity3D 2021.3.15f1
- Сreates an game object on the scene with
prefab.name + "PoolContainer"
name if the parent is not specified. - Instantiate prefab copies under specified parrent object
public ObjectPool(T objectPrefab, int count, string poolName = "PoolContainer") public ObjectPool(T objectPrefab, int count, Transform parent)
- Current - return current item
var item = pool.Current;
- ContainerObject - return container GameObject
GameObject count = pool.ContainerObject;
- Count - return items count
int count = pool.Count;
- GetByIndex() - return item by index, set tis item as current
var item = pool.GetByIndex(index);
- GetNext() - return next item, increase index
var item = pool.GetNext();
- Clear() - just delete pool container game object, clear inner array
pool.Clear();
Example
using UnityEngine;
using PLib.Pool;
public class EnemyPoolContainer : MonoBehaviour
{
[SerializeField] private Enemy _enemyPrefab;
[SerializeField] private int _enemiesCount = 10;
private ObjectPool<Enemy> _enemies;
private GameObject _container;
private void Awake()
{
_enemies = new ObjectPool<Enemy>(_enemyPrefab, _enemiesCount, transform);
_container = _enemies.ContainerObject;
foreach(Enemy enemy in _enemies)
enemy.transform.position = Random.insideUnitCircle * 5;
for(int i = 0; i < 5; i++)
_enemies.GetByIndex(i).gameObject.SetActive(false);
_enemies.Current.SomeAction(); //Current enemy is 5
_enemies.GetNext().SomeSecondAction(); //Current enemy is 6
_enemies.Clear();
}
}
Example
using UnityEngine;
using PLib.Singleton;
public class SomeClass : Singleton<SomeClass>
{
// If you need Awake method
private void Awake()
{
base.Awake();
// Your code
}
}