Ejemplo n.º 1
0
        /// <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;
        }
Ejemplo n.º 2
0
        /// <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();
            }
        }
Ejemplo n.º 3
0
        /// <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);
        }
 /// <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);
 }