protected virtual bool PoolObjectEnabled(GameObject testedObject) { MultipleObjectPoolerObject searchedObject = GetPoolObject(testedObject); if (searchedObject != null) { return(searchedObject.Enabled); } else { return(false); } }
/// <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 (_currentIndex >= _pooledGameObjectsOriginalOrder.Count) { ResetCurrentIndex(); } MultipleObjectPoolerObject searchedObject = GetPoolObject(_pooledGameObjects[_currentIndex].gameObject); if (_currentIndex >= _pooledGameObjects.Count) { return(null); } if (!searchedObject.Enabled) { _currentIndex++; return(null); } // if the object is already active, we need to find another one if (_pooledGameObjects[_currentIndex].gameObject.activeInHierarchy) { GameObject findObject = FindInactiveObject(_pooledGameObjects[_currentIndex].gameObject.name, _pooledGameObjects); if (findObject != null) { _currentIndex++; return(findObject); } // if its pool can expand, we create a new one if (searchedObject.PoolCanExpand) { _currentIndex++; 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; _currentIndex++; return(_pooledGameObjects[newIndex]); } }
/// <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); // if we can't pool the same object twice, we'll loop for a while to try and get another one int 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 inactive 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]); } }