Exemple #1
0
        public GameObject GetNextObjectInCache()
        {
            GameObject obj = null;

            for (var i = 0; i < cacheSize; i++)
            {
                obj = objects[cacheIndex];

                if (obj == null)
                {
                    Debug.LogWarning("Try spawn of " + prefab.name +
                                     ", but cached object is destroyed! Instantiate object.");
                    obj        = objects[cacheIndex] = Pool.InstantiateGameObject(prefab, new Vector3(), Quaternion.identity);
                    cacheIndex = (cacheIndex + 1) % cacheSize;
                    return(obj);
                }

                if (!obj.activeSelf)
                {
                    break;
                }

                // If not, increment index and make it loop around
                // if it exceeds the size of the cache
                cacheIndex = (cacheIndex + 1) % cacheSize;
            }


            if (obj == null || (isDynamic && obj.activeSelf))
            {
                cacheSize++;
                objects.Add(Pool.InstantiateGameObject(prefab, new Vector3(), Quaternion.identity));
                GameObject nextObjectInCache = objects.Last();
                nextObjectInCache.SetActive(false);
                nextObjectInCache.name             = nextObjectInCache.name + (cacheSize - 1);
                nextObjectInCache.transform.parent = Pool.transform;
                cacheIndex = (cacheIndex + 1) % cacheSize;
                Pool.activeCachedObjects.Add(nextObjectInCache, true);
                return(nextObjectInCache);
            }

            cacheIndex = (cacheIndex + 1) % cacheSize;
            return(obj);
        }
Exemple #2
0
 public void Initialize(GOPool pool)
 {
     Pool    = pool;
     Type    = prefab.GetComponent <IBasePoolObject>();
     objects = new List <GameObject>();
     for (var i = 0; i < cacheSize; i++)
     {
         objects.Add(Pool.InstantiateGameObject(prefab, new Vector3(), Quaternion.identity));
         objects[i].SetActive(false);
         objects[i].name             = objects[i].name + i;
         objects[i].transform.parent = Pool.transform;
     }
 }