/// <summary> /// Initializes the item store for the pool. /// </summary> /// <param name="mode">Access mode of the pool.</param> /// <param name="collection">Collection to add to the store.</param> protected virtual void InitItemStore(PoolAccessMode mode, IEnumerable <T> collection) { this.ItemStore = mode switch { PoolAccessMode.FirstIn => new QueueStore(collection), PoolAccessMode.LastIn => new StackStore(collection), _ => null, }; }
/// <summary> /// Initializes the item store for the pool. /// </summary> /// <param name="mode">Access mode of the pool.</param> /// <param name="size">Size of the pool.</param> protected virtual void InitItemStore(PoolAccessMode mode, int size = 0) { this.ItemStore = mode switch { PoolAccessMode.FirstIn => new QueueStore(size), PoolAccessMode.LastIn => new StackStore(size), _ => throw new ArgumentException($"No ItemStore exists for PoolAccessMode '{mode}'.", "mode"), }; }
/// <inheritdoc/> public void CreatePool <T>(T prefab, int size = 0, PoolAccessMode accessMode = PoolAccessMode.LastIn, PoolLoadingMode loadingMode = PoolLoadingMode.Eager) where T : ObjectPoolItem <T> { if (prefab == null) { throw new ArgumentNullException("prefab"); } if (this.HasPool(prefab)) { throw new InvalidOperationException("Pool already exists."); } ObjectPooling.ObjectPool <T> pool = new ObjectPooling.ObjectPool <T>((T)prefab, size, accessMode, loadingMode); pool.OnPoolParentCreated += (obj) => { obj.transform.SetParent(this.PoolParent.transform); }; this.ObjectPools.Add(prefab, pool); }
/// <summary> /// Initializes a new instance of the <see cref="ObjectPool{T}"/> class. /// </summary> /// <param name="objectPoolItem">The ObjectPoolItem to be copied when creating a new object for the pool.</param> /// <param name="size">The target size of the pool. The pool will expand beyond this size if needed.</param> /// <param name="accessMode">Determines the order in which a pool pulls objects from its store.</param> /// <param name="loadingMode">Determines how a pool reaches its size.</param> public ObjectPool( T objectPoolItem, int size = 0, PoolAccessMode accessMode = PoolAccessMode.LastIn, PoolLoadingMode loadingMode = PoolLoadingMode.Eager) : base( (objectPool) => { return(GameObject.Instantiate(objectPoolItem.gameObject).GetComponent <T>()); }, size, accessMode, loadingMode) { if (objectPoolItem == null) { throw new ArgumentNullException("objectPoolItem"); } this.poolParentName = objectPoolItem.gameObject.name; }
/// <summary> /// Initializes a new instance of the <see cref="ObjectPool{T}"/> class. /// </summary> /// <param name="factory">A method which takes in a pool and returns an object for that pool.</param> /// <param name="size">The target size of the pool. The pool will expand beyond this size if needed.</param> /// <param name="accessMode">Determines the order in which a pool pulls objects from its store.</param> /// <param name="loadingMode">Determines how a pool reaches its size.</param> public ObjectPool( Func <ObjectPool <T>, T> factory, int size = 0, PoolAccessMode accessMode = PoolAccessMode.LastIn, PoolLoadingMode loadingMode = PoolLoadingMode.Eager) { if (size < 0) { throw new ArgumentOutOfRangeException("size", size, "Argument 'size' cannot be negative."); } this.Factory = factory ?? throw new ArgumentNullException("factory"); this.Size = size; this.AccessMode = accessMode; this.LoadingMode = loadingMode; this.OwnedItems = new HashSet <T>(); this.StoredItems = new HashSet <T>(); this.InitItemStore(this.AccessMode, this.Size); if (loadingMode == PoolLoadingMode.Eager) { this.PreloadItems(); } }
/// <inheritdoc/> public void CreatePool <T>(T prefab, int size = 0, PoolAccessMode accessMode = PoolAccessMode.LastIn, PoolLoadingMode loadingMode = PoolLoadingMode.Eager) where T : ObjectPoolItem <T> { this.PoolManager.CreatePool(prefab, size, accessMode, loadingMode); }