コード例 #1
0
        /// <summary>
        /// Tries to find a gameobject in the pool according to the order the list has been setup in (one of each, no matter how big their respective pool sizes)
        /// </summary>
        /// <returns>The pooled game object original order.</returns>
        protected virtual GameObject GetPooledGameObjectOriginalOrder()
        {
            int newIndex;

            // if we've reached the end of our list, we start again from the beginning
            if (_currentIndexCounter >= Pool[_currentIndex].PoolSize)
            {
                _currentIndexCounter = 0;
                _currentIndex++;
            }
            if (_currentIndex >= Pool.Count)
            {
                ResetCurrentIndex();
            }

            MMMultipleObjectPoolerObject searchedObject = GetPoolObject(Pool[_currentIndex].GameObjectToPool);

            if (_currentIndex >= _objectPool.PooledGameObjects.Count)
            {
                return(null);
            }
            if (!searchedObject.Enabled)
            {
                _currentIndex++; return(null);
            }

            // if the object is already active, we need to find another one
            if (_objectPool.PooledGameObjects[_currentIndex].gameObject.activeInHierarchy)
            {
                GameObject findObject = FindInactiveObject(_objectPool.PooledGameObjects[_currentIndex].gameObject.name, _objectPool.PooledGameObjects);
                if (findObject != null)
                {
                    _currentIndexCounter++;
                    return(findObject);
                }

                // if its pool can expand, we create a new one
                if (searchedObject.PoolCanExpand)
                {
                    _currentIndexCounter++;
                    return(AddOneObjectToThePool(searchedObject.GameObjectToPool));
                }
                else
                {
                    // if it can't expand we return nothing
                    return(null);
                }
            }
            else
            {
                // if the object is inactive, we return it
                newIndex = _currentIndex;
                _currentIndexCounter++;
                return(_objectPool.PooledGameObjects[newIndex]);
            }
        }
コード例 #2
0
        protected virtual bool PoolObjectEnabled(GameObject testedObject)
        {
            MMMultipleObjectPoolerObject searchedObject = GetPoolObject(testedObject);

            if (searchedObject != null)
            {
                return(searchedObject.Enabled);
            }
            else
            {
                return(false);
            }
        }
コード例 #3
0
        /// <summary>
        /// Instantiates a multiple object pooler and sets it up
        /// </summary>
        protected virtual void InstantiateMultiplePool()
        {
            GameObject newPooler = new GameObject();

            newPooler.name = this.name + "_Pooler";
            newPooler.transform.SetParent(this.transform);
            MMMultipleObjectPooler multiplePooler = newPooler.AddComponent <MMMultipleObjectPooler>();

            multiplePooler.Pool = new List <MMMultipleObjectPoolerObject>();
            foreach (MMFloatingText obj in PooledMultipleMMFloatingText)
            {
                MMMultipleObjectPoolerObject item = new MMMultipleObjectPoolerObject();
                item.GameObjectToPool = obj.gameObject;
                item.PoolCanExpand    = PoolCanExpand;
                item.PoolSize         = PoolSize;
                item.Enabled          = true;
                multiplePooler.Pool.Add(item);
            }
            multiplePooler.NestWaitingPool       = NestWaitingPool;
            multiplePooler.MutualizeWaitingPools = MutualizeWaitingPools;
            multiplePooler.FillObjectPool();
            _pooler = multiplePooler;
        }
コード例 #4
0
        /// <summary>
        /// Randomly choses one object from the pool, based on its pool size probability (the larger the pool size, the higher the chances it'll get picked)
        /// </summary>
        /// <returns>The pooled game object pool size based.</returns>
        protected virtual GameObject GetPooledGameObjectPoolSizeBased()
        {
            // we get a random index
            int randomIndex = UnityEngine.Random.Range(0, _pooledGameObjects.Count);

            int overflowCounter = 0;

            // we check to see if that object is enabled, if it's not we loop
            while (!PoolObjectEnabled(_pooledGameObjects[randomIndex]) && overflowCounter < _pooledGameObjects.Count)
            {
                randomIndex = UnityEngine.Random.Range(0, _pooledGameObjects.Count);
                overflowCounter++;
            }
            if (!PoolObjectEnabled(_pooledGameObjects[randomIndex]))
            {
                return(null);
            }

            // if we can't pool the same object twice, we'll loop for a while to try and get another one
            overflowCounter = 0;
            while (!CanPoolSameObjectTwice &&
                   _pooledGameObjects[randomIndex].name == _lastPooledObjectName &&
                   overflowCounter < _pooledGameObjects.Count)
            {
                randomIndex = UnityEngine.Random.Range(0, _pooledGameObjects.Count);
                overflowCounter++;
            }

            //  if the item we've picked is active
            if (_pooledGameObjects[randomIndex].gameObject.activeInHierarchy)
            {
                // we try to find another inactive object of the same type
                GameObject pulledObject = FindInactiveObject(_pooledGameObjects[randomIndex].gameObject.name, _pooledGameObjects);
                if (pulledObject != null)
                {
                    return(pulledObject);
                }
                else
                {
                    // if we couldn't find an inactive object of this type, we see if it can expand
                    MMMultipleObjectPoolerObject searchedObject = GetPoolObject(_pooledGameObjects[randomIndex].gameObject);
                    if (searchedObject == null)
                    {
                        return(null);
                    }
                    // if the pool for this object is allowed to grow (this is set in the inspector if you're wondering)
                    if (searchedObject.PoolCanExpand)
                    {
                        return(AddOneObjectToThePool(searchedObject.GameObjectToPool));
                    }
                    else
                    {
                        // if it's not allowed to grow, we return nothing.
                        return(null);
                    }
                }
            }
            else
            {
                // if the pool wasn't empty, we return the random object we've found.
                return(_pooledGameObjects[randomIndex]);
            }
        }