Esempio n. 1
0
    private static PoolableObject AddObjectToPool(PoolableObject poPrefab)
    {
        int id    = poPrefab.GetInstanceID();
        var clone = GameObject.Instantiate(poPrefab, Vector3.zero, Quaternion.identity);

        clone.gameObject.SetActive(false);
        objectPool[id].Add(clone);
        return(clone);
    }
Esempio n. 2
0
    public static void CreatePool(PoolableObject poPrefab, int size)
    {
        int id = poPrefab.GetInstanceID();

        objectPool[id]  = new List <PoolableObject>(size);
        poolIndexes[id] = 0;
        for (int i = 0; i < size; ++i)
        {
            AddObjectToPool(poPrefab);
        }
    }
Esempio n. 3
0
    public void Init(PoolableObject obj, int size)
    {
        poolableObject = obj;
        initailSize    = size;

        poolKey  = poolableObject.GetInstanceID();
        poolRoot = new GameObject("Pool_" + obj.name + "_" + poolKey);
        poolRoot.transform.SetParent(transform, stayTransform);
        for (int cnt = 0; cnt < initailSize; cnt++)
        {
            PoolableObject newOne = NewOne();
            m_pool.Enqueue(newOne);
            newOne.gameObject.SetActive(false);
            newOne.transform.SetParent(poolRoot.transform);
        }
    }
Esempio n. 4
0
    public static nObjectPool GetObjectPool(PoolableObject poolableObject)
    {
        int poolKey = poolableObject.poolKey;

        if (poolableObject.gameObject.scene.name == null)
        {
            poolKey = poolableObject.GetInstanceID();
        }

        nObjectPool poolDictCache;
        bool        isHasPool = PoolDict.TryGetValue(poolKey, out poolDictCache);

        if (!isHasPool)
        {
            poolDictCache = CreateNewObjectPool(poolableObject);
        }

        return(poolDictCache);
    }
Esempio n. 5
0
    public static PoolableObject GetObjectFromPool(PoolableObject prefab)
    {
        int id = prefab.GetInstanceID();

        if (!objectPool.ContainsKey(id))
        {
            CreatePool(prefab, initialPoolSize);
        }

        for (int i = 0; i < objectPool[id].Count; i++)
        {
            poolIndexes[id] = (poolIndexes[id] + 1) % objectPool[id].Count;             // make sthe iterator go back to position 0
            if (objectPool[id][poolIndexes[id]].gameObject.activeInHierarchy == false)
            {
                return(objectPool[id][poolIndexes[id]]);
            }
        }


        return(AddObjectToPool(prefab));
    }