Ejemplo n.º 1
0
    protected virtual bool PoolObjectEnabled(GameObject testedObject)
    {
        MultipleObjectPoolerObject searchedObject = GetPoolObject(testedObject);

        if (searchedObject != null)
        {
            return(searchedObject.Enabled);
        }
        else
        {
            return(false);
        }
    }
Ejemplo n.º 2
0
    protected virtual GameObject GetPooledGameObjectPoolSizeBased()
    {
        int randomIndex = UnityEngine.Random.Range(0, _pooledGameObjects.Count);

        int overflowCounter = 0;

        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
                MultipleObjectPoolerObject 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]);
        }
    }