예제 #1
0
    public GameObject GetObject(ObjectPoolType type, Vector3 startPosition)
    {
        if (!objectPoolList.ContainsKey(type.ToString()))
        {
            return(null);
        }

        ObjectPool pool = (ObjectPool)objectPoolList[type.ToString()];

        GameObject obj;

        if (pool.UnusedList.Count > 0)
        {
            obj = pool.UnusedList[0];
            pool.UnusedList.RemoveAt(0);
            obj.SetActive(true);
        }
        else
        {
            obj = Instantiate(pool.Prefab);
            obj.transform.name   = pool.Prefab.name;
            obj.transform.parent = pool.Group.transform;
        }

        obj.transform.position = startPosition;
        obj.transform.rotation = Quaternion.identity;

        return(obj);
    }
예제 #2
0
    private void CreateAndAddObject(ObjectPoolType poolType)
    {
        Queue <PooledObject> targetPool = GetQueue(poolType);

        if (targetPool == null)
        {
            Debug.LogError("Pool null");
            return;
        }

        PooledObject targetObject = GetPrefabFromPool(poolType);

        PooledObject newObject = Instantiate(targetObject) as PooledObject;

        newObject.poolType        = poolType;
        newObject.gameObject.name = poolType.ToString() + GameManager.Instance.createdPooledObjects;
        newObject.gameObject.SetActive(false);
        newObject.transform.SetParent(transform, false);
        targetPool.Enqueue(newObject);
        GameManager.AddCreations();
    }
예제 #3
0
    public GameObject GetPooledObject(ObjectPoolType type)
    {
        var itemInPool = _pooledObjects.Where(i => !i.activeInHierarchy && i.tag.ToUpper() == type.ToString().ToUpper()).FirstOrDefault();

        if (itemInPool != null)
        {
            itemInPool.SetActive(true);

            return(itemInPool);
        }

        var objectPoolItem = _itemsToPool.Where(i => i.Type == type).FirstOrDefault();

        if (objectPoolItem != null && objectPoolItem.ShouldExpand)
        {
            var obj = Instantiate(objectPoolItem.ObjectToPool);

            obj.name = objectPoolItem.Type.ToString() + "_" + _pooledObjects.Where(i => i.tag.ToUpper() == type.ToString().ToUpper()).Count();
            obj.SetActive(true);

            _pooledObjects.Add(obj);

            return(obj);
        }

        return(null);
    }