fixed an oopsy daisy in the object pool

This commit is contained in:
Dom Harris 2021-01-05 12:19:52 +00:00
parent cc08bf1ca4
commit 51cb7b8d3c
2 changed files with 20 additions and 7 deletions

View File

@ -13,6 +13,8 @@ public class ObjectPool<T> : MonoBehaviour where T : Component
private List<T> _pool; private List<T> _pool;
private List<GameObject> _poolGameObjects; private List<GameObject> _poolGameObjects;
private List<int> _indicesToRemove;
public event Action<T> OnObjectFound; public event Action<T> OnObjectFound;
public event Action Initialised; public event Action Initialised;
@ -40,17 +42,28 @@ public class ObjectPool<T> : MonoBehaviour where T : Component
return (foundObject, idx); return (foundObject, idx);
} }
private void LateUpdate()
{
for (int i = _indicesToRemove.Count - 1; i >= 0; i--)
{
var obj = _pool[i];
_pool.RemoveAt(i);
_poolGameObjects.RemoveAt(i);
_pool.Add(obj);
_poolGameObjects.Add(obj.gameObject);
}
_indicesToRemove.Clear();
}
public void ReturnObject(T obj, int idx) public void ReturnObject(T obj, int idx)
{ {
_pool.RemoveAt(idx); _indicesToRemove.Add(idx);
_pool.Add(obj);
_poolGameObjects.RemoveAt(idx);
_poolGameObjects.Add(obj.gameObject);
} }
public IEnumerator Start() public IEnumerator Start()
{ {
_indicesToRemove = new List<int>();
_pool = new List<T>(numObjects); _pool = new List<T>(numObjects);
_poolGameObjects = new List<GameObject>(numObjects); _poolGameObjects = new List<GameObject>(numObjects);
for (int i = 0; i < numObjects; ++i) for (int i = 0; i < numObjects; ++i)

View File

@ -81,8 +81,8 @@ MonoBehaviour:
m_PreInfinity: 2 m_PreInfinity: 2
m_PostInfinity: 2 m_PostInfinity: 2
m_RotationOrder: 4 m_RotationOrder: 4
m_ConstantMin: 2.5 m_ConstantMin: 1.25
m_ConstantMax: 3 m_ConstantMax: 1.5
bulletSize: bulletSize:
m_Mode: 3 m_Mode: 3
m_CurveMultiplier: 0 m_CurveMultiplier: 0