Ejemplo n.º 1
0
        //o(1)
        public GameObject NextAvailableObject(Vector3 position, Quaternion rotation)
        {
            ObjectToPool po = null;

            if (availableObjStack.Count > 0)
            {
                po = availableObjStack.Pop();
            }
            else if (fixedSize == false)
            {
                //increment size var, this is for info purpose only
                poolSize++;
                Debug.Log(string.Format("Growing pool {0}. New size: {1}", poolName, poolSize));
                //create new object
                po = NewObjectInstance();
            }
            else
            {
                Debug.LogWarning("No object available & cannot grow pool: " + poolName);
            }

            GameObject result = null;

            if (po != null)
            {
                po.isPooled = false;
                result      = po.gameObject;
                result.SetActive(true);

                result.transform.position = position;
                result.transform.rotation = rotation;
            }

            return(result);
        }
Ejemplo n.º 2
0
 //o(1)
 private void AddObjectToPool(ObjectToPool po)
 {
     //add to pool
     po.gameObject.SetActive(false);
     availableObjStack.Push(po);
     po.isPooled = true;
 }
Ejemplo n.º 3
0
        private ObjectToPool NewObjectInstance()
        {
            GameObject   go = (GameObject)GameObject.Instantiate(ObjectToPoolPrefab);
            ObjectToPool po = go.GetComponent <ObjectToPool>();

            if (po == null)
            {
                po = go.AddComponent <ObjectToPool>();
            }
            //set name
            po.poolName = poolName;
            return(po);
        }
Ejemplo n.º 4
0
        public void ReturnObjectToPool(GameObject go)
        {
            ObjectToPool po = go.GetComponent <ObjectToPool>();

            if (po == null)
            {
                Debug.LogWarning("Specified object is not a pooled instance: " + go.name);
            }
            else
            {
                if (poolDictionary.ContainsKey(po.poolName))
                {
                    Pool pool = poolDictionary[po.poolName];
                    pool.ReturnObjectToPool(po);
                }
                else
                {
                    Debug.LogWarning("No pool available with name: " + po.poolName);
                }
            }
        }
Ejemplo n.º 5
0
 //o(1)
 public void ReturnObjectToPool(ObjectToPool po)
 {
     if (poolName.Equals(po.poolName))
     {
         /* we could have used availableObjStack.Contains(po) to check if this object is in pool.
          * While that would have been more robust, it would have made this method O(n)
          */
         if (po.isPooled)
         {
             Debug.LogWarning(po.gameObject.name + " is already in pool. Why are you trying to return it again? Check usage.");
         }
         else
         {
             AddObjectToPool(po);
         }
     }
     else
     {
         Debug.LogError(string.Format("Trying to add object to incorrect pool {0} {1}", po.poolName, poolName));
     }
 }