Example #1
0
        public T GetObject()
        {
            ObjectPoolUnit freeUnit = null;

            for (int i = 0; i < m_pool.Count; i++)
            {
                // if this unit is being used, continue to find a free one
                if (m_pool[i].IsBeingUsed == true)
                {
                    continue;
                }

                // if a free unit is found, store it and break the loop
                freeUnit = m_pool[i];
                break;
            }

            if (freeUnit == null)
            {
                if (m_isExtendable == false)
                {
                    // since we constraint T as class, this will always be null
                    // if T can be data types or structs, the default value can vary
                    return(default(T));
                }
                else
                {
                    freeUnit = CreateNewUnit();
                }
            }

            freeUnit.IsBeingUsed = true;
            m_objectBeingUsed.Add(freeUnit.ObjectInstance, freeUnit);
            return(freeUnit.ObjectInstance);
        }
Example #2
0
        private ObjectPoolUnit CreateNewUnit()
        {
            // create new units with passed-in factory method
            ObjectPoolUnit newUnit = new ObjectPoolUnit(m_factoryFunction.Invoke());

            m_pool.Add(newUnit);

            return(newUnit);
        }
Example #3
0
 public void ReleaseObject(T releasingObject)
 {
     if (m_objectBeingUsed.ContainsKey(releasingObject))
     {
         ObjectPoolUnit releasingUnit = m_objectBeingUsed[releasingObject];
         releasingUnit.IsBeingUsed = false;
         m_objectBeingUsed.Remove(releasingObject);
     }
     else
     {
         Debug.LogWarning("Object Pool of Type : " + typeof(T) + " trying to release object : " + releasingObject + ", which is not in pool.");
     }
 }
    //---------------------------------------------------------------------------------------------------------------------------------------------

    //從物件池中取得指定物件
    public GameObject PickUpObject(string goName)
    {
        if (!Dict_ObjectPoolTag.ContainsKey(goName))
        {
            return(null);                                         //查無物件
        }
        GameObject     _result = null;
        ObjectPoolUnit _unit   = Dict_ObjectPoolTag[goName];

        //創立新物件(Lambda)
        System.Action <ObjectPoolUnit> CreateNew = (ObjectPoolUnit u) =>
        {
            GameObject _go = Instantiate(u.prefabReference, u.parentHolder);
            u.AddElement(_go);

            _result = _go;
        };

        if (_unit.ObjectPoolList == null || _unit.ObjectPoolList.Count == 0) //若物件池中無物件
        {
            CreateNew(_unit);                                                //創立新物件
        }
        else //若物件池中存在物件
        {
            for (int i = 0; i < _unit.ObjectPoolList.Count; i++) //尋找隱藏中(active = false)物件
            {
                if (!_unit.ObjectPoolList[i].activeSelf)
                {
                    _result = _unit.ObjectPoolList[i];
                    break;
                }
            }

            if (_result == null)
            {
                CreateNew(_unit);                  //若所有物件皆在非隱藏狀態, 則建立新物件
            }
        }

        return(_result);
    }