public void Clear() { while (PoolStorage.Count > 0) { TPoolObject poolObject = PoolStorage.Get(); if (poolObject != null) { Object.Destroy(poolObject.gameObject); } } lock (ActiveObjects) { for (var i = 0; i < ActiveObjects.Count; i++) { TPoolObject activeObject = ActiveObjects[i]; if (activeObject != null) { Object.Destroy(activeObject.gameObject); } } ActiveObjects.Clear(); } }
public void AddObjects(IEnumerable <IPoolObject> poolObjects) { foreach (IPoolObject poolObject in poolObjects) { var o = poolObject as TPoolObject; if (o != null) { o.Deactivate(); poolObject.PoolObjectDestroyed += PoolObjectDestroyed; PoolStorage.Add(o); } } }
public IPoolObject GetObject() { TPoolObject poolObject = null; if (PoolStorage.Count > 0) { while (poolObject == null && PoolStorage.Count > 0) { poolObject = PoolStorage.Get(); } } if (poolObject == null) { if (ActiveObjectsCount < MaxPoolObjects) { poolObject = CreateObject(); } else { lock (ActiveObjects) { poolObject = ChooseActiveObjectWithMinimumPriority(); poolObject.PoolObjectDestroyed -= PoolObjectDestroyed; poolObject.Deactivate(); ActiveObjects.Remove(poolObject); } } } poolObject.PoolObjectDestroyed += PoolObjectDestroyed; poolObject.Activate(); lock (ActiveObjects) { ActiveObjects.Add(poolObject); } return(poolObject); }
private void PoolObjectDestroyed(IPoolObject poolObject) { poolObject.PoolObjectDestroyed -= PoolObjectDestroyed; poolObject.Deactivate(); var o = poolObject as TPoolObject; if (ReferenceEquals(o, null) == false) { lock (ActiveObjects) { ActiveObjects.Remove(o); } PoolStorage.Add(o); } else { DevLogger.Error( string.Format("Pool object was not casted to type '{0}'", typeof(TPoolObject)), LogChannel.Common); } }
public void Destroy() { ActiveObjects.Clear(); PoolStorage.Clear(); }