public void GetObjectFromPool()
        {
            if (ObjectList == null)
            {
                return;
            }

            Vector3 pos = new Vector3();

            pos.x = Random.Range(-5, 6);
            pos.y = 0f;
            pos.z = Random.Range(-5, 6);

            PoolObject po = PoolManager.Instance.GetObjectFromPool(poolName, pos, Quaternion.identity);

            if (po)
            {
                ObjectList.Add(po);
            }
        }
Пример #2
0
 /// <summary>
 /// 将指定对象返回池中,复杂度o(1)
 /// </summary>
 /// <param name="po"></param>
 public void ReturnObjectToPool(PoolObject 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));
     }
 }
Пример #3
0
 public void ReturnObjectToPool(PoolObject po)
 {
     //PoolObject po = go.GetComponent<PoolObject>();
     if (po == null)
     {
         Debug.LogWarning("Specified object is not a pooled instance: " + po.name);
     }
     else
     {
         if (poolMap.ContainsKey(po.PoolName))
         {
             Pool pool = poolMap[po.PoolName];
             pool.ReturnObjectToPool(po);
         }
         else
         {
             Debug.LogWarning("No pool available with name: " + po.PoolName);
         }
     }
 }
Пример #4
0
        /// <summary>
        ///  null in case the pool does not have any object available & can grow size is false.
        /// </summary>
        /// <param name="poolName">name</param>
        /// <param name="position">position</param>
        /// <param name="rotation">rotation</param>
        /// <returns></returns>
        public PoolObject GetObjectFromPool(string poolName, Vector3 position, Quaternion rotation)
        {
            //GameObject result = null;
            PoolObject result = null;

            if (poolMap.ContainsKey(poolName))
            {
                Pool pool = poolMap[poolName];
                result = pool.NextAvailableObject(position, rotation);
                //scenario when no available object is found in pool
                if (result == null)
                {
                    Debug.LogWarning("No object available in pool. Consider setting fixedSize to false.: " + poolName);
                }
            }
            else
            {
                Debug.LogError("Invalid pool name specified: " + poolName);
            }

            return(result);
        }
Пример #5
0
        //o(1)
        /// <summary>
        /// 获取一个池中可用对象,如池中已无可用对象,则新建一个对象实例
        /// </summary>
        /// <param name="position"></param>
        /// <param name="rotation"></param>
        /// <returns></returns>
        public GameObject NextAvailableObject(Vector3 position, Quaternion rotation)
        {
            PoolObject po = null;

            //池中有可用对象,直接从池中取
            if (availableObjStack.Count > 0)
            {
                po = availableObjStack.Pop();
            }
            //池中已无可用对象,new一个
            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);
        }
Пример #6
0
        //o(1)
        /// <summary>
        /// Get an available object in the pool, if there is no available object in the pool, create a new object instance
        /// </summary>
        /// <param name="position"></param>
        /// <param name="rotation"></param>
        /// <returns></returns>
        public PoolObject NextAvailableObject(Vector3 position, Quaternion rotation)
        {
            PoolObject po = null;

            //There are available objects in the pool, directly fetch from the pool
            if (availableObjStack.Count > 0)
            {
                po = availableObjStack.Pop();
            }
            //There are no objects available in the pool, new one
            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);
            }

            //Set the behavior of the object
            GameObject result = null;

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

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

            return(po);
        }