Пример #1
0
    //o(1)
    public GameObject NextAvailableObject(Vector3 position, Quaternion rotation)
    {
        PooledObjectScript 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);
    }
Пример #2
0
    public void ReturnObjectToPool(GameObject go)
    {
        //custom
        for (int i = 0; i < go.transform.childCount; i++)
        {
            GameObject child = go.transform.GetChild(i).gameObject;
            if (child.GetComponent <PooledObjectScript>() != null)
            {
                ReturnObjectToPool(child.gameObject);
                i--;
            }
        }
        //custom
        PooledObjectScript po = go.GetComponent <PooledObjectScript>();

        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);
            }
        }
    }
Пример #3
0
    //o(1)
    private void AddObjectToPool(PooledObjectScript po)
    {
        po.gameObject.SetActive(false);
        po.gameObject.transform.parent = null; //custom

        availableObjStack.Push(po);
        po.isPooled = true;
    }
Пример #4
0
    private PooledObjectScript NewObjectInstance()
    {
        GameObject         go = (GameObject)GameObject.Instantiate(poolObjectPrefab);
        PooledObjectScript po = go.GetComponent <PooledObjectScript>();

        if (po == null)
        {
            po = go.AddComponent <PooledObjectScript>();
        }
        //set name
        po.poolName = poolName;
        return(po);
    }
Пример #5
0
 //o(1)
 public void ReturnObjectToPool(PooledObjectScript po)
 {
     if (poolName.Equals(po.poolName))
     {
         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));
     }
 }