Example #1
0
    public static Queue <GameObject> enemyPool  = new Queue <GameObject>(); //Queue for enemies

    public static GameObject GetFromPool(Poolable.types type)               //Get an object from the pool
    {
        GameObject result;

        if (type == Poolable.types.BULLET)        //if type is a BULLET
        {
            if (bulletPool.Count > 0)             //if we have bullets in the bulletPool
            {
                result = bulletPool.Dequeue();    //get a bullet ou of the bulletPool
            }
            else
            {
                result = Instantiate(Resources.Load("Bullet")) as GameObject;                 //create a new bullet, since the pool is empty
            }
        }
        else                                  //if (type == Poolable.types.ENEMY){
        {
            if (enemyPool.Count > 0)          //if we have enemies in the enemyPool
            {
                result = enemyPool.Dequeue(); //get a enemy ou of the enemyPool
            }
            else
            {
                result = Instantiate(Resources.Load("Enemy")) as GameObject;                 //create a new enemy, since the pool is empty
            }
        }

        result.SetActive(true);                   //Activate the bullet
        result.GetComponent <Poolable>().Reset(); //Call "Reset" for this objects Poolable component

        return(result);                           //return the result
    }
Example #2
0
    public static Queue <GameObject> playerPool = new Queue <GameObject>(); //Queue for enemies

    public static GameObject GetFromPool(Poolable.types type)
    {//Get an object from the pool
        GameObject result;

        if (type == Poolable.types.BLUE)
        {                                    //if type is a BULLET
            if (bluePool.Count > 0)
            {                                //if we have bullets in the bulletPool
                result = bluePool.Dequeue(); //get a bullet ou of the bulletPool
            }
            else
            {
                result = Instantiate(Resources.Load("Blue")) as GameObject; //create a new bullet, since the pool is empty
            }
        }
        else
        {//if (type == Poolable.types.ENEMY){
            Debug.Log("Deueue the player");
            if (playerPool.Count > 0)
            {                                  //if we have enemies in the enemyPool
                result = playerPool.Dequeue(); //get a enemy ou of the enemyPool
            }
            else
            {
                result = Instantiate(Resources.Load("Player1")) as GameObject; //create a new enemy, since the pool is empty
            }
        }

        result.SetActive(true);                   //Activate the bullet
        result.GetComponent <Poolable>().Reset(); //Call "Reset" for this objects Poolable component

        return(result);                           //return the result
    }