示例#1
0
    // Use this for initialization
    void InitializePool()
    {
        containerObject = new GameObject("ObjectPool");
        containerObject.transform.parent = transform;

        //Loop through the object prefabs and make a new list for each one.
        //We do this because the pool can only support prefabs set to it in the editor,
        //so we can assume the lists of pooled objects are in the same order as object prefabs in the array
        pool = new List <GameObject> [entries.Length];
        for (int i = 0; i < entries.Length; i++)
        {
            ObjectPoolEntry objectPrefab = entries[i];
            //create the repository
            pool[i] = new List <GameObject>();
            //fill it
            for (int n = 0; n < objectPrefab.count; n++)
            {
                if (objectPrefab.prefab)
                {
                    GameObject newObj = Instantiate(objectPrefab.prefab) as GameObject;
                    newObj.name = objectPrefab.prefab.name;
                    PoolObject(newObj);
                }
            }
        }
    }
示例#2
0
    /// <summary>
    /// Gets a new object for the name type provided.  If no object type exists or if onlypooled is true and there is no objects of that type in the pool
    /// then null will be returned.
    /// </summary>
    /// <returns>
    /// The object for type.
    /// </returns>
    /// <param name='objectName'>
    /// Object type.
    /// </param>
    /// <param name='onlyPooled'>
    /// If true, it will only return an object if there is one currently pooled.
    /// </param>
    public GameObject GetObjectForType(string objectName, bool onlyPooled)
    {
        if (!entriesByName.ContainsKey(objectName))
        {
            Debug.Log("cant find " + objectName);
            return(null);
        }

        ObjectPoolEntry objectPoolEntry = entriesByName[objectName];

        if (objectPoolEntry.objectsInPool > 0)
        {
            GameObject pooledObject = objectPoolEntry.pool[--objectPoolEntry.objectsInPool];
            pooledObject.transform.parent = null;
            pooledObject.SetActive(true);

            return(pooledObject);
        }
        else if (!onlyPooled)
        {
            GameObject obj = Instantiate(objectPoolEntry.Prefab);

            obj.name = obj.name.Substring(0, obj.name.Length - CLONE_NAME_LENGTH);
            objectPoolEntry.pool.Add(obj);

            return(obj);
        }

        return(null);
    }
示例#3
0
    // Use this for initialization
    private void Start()
    {
        //Loop through the object prefabs and make a new list for each one.
        //We do this because the pool can only support prefabs set to it in the editor,
        //so we can assume the lists of pooled objects are in the same order as object prefabs in the array

        for (int i = 0; i < Entries.Count; i++)
        {
            ObjectPoolEntry objectPoolEntry = Entries[i];

            entriesByName.Add(objectPoolEntry.Prefab.name, objectPoolEntry);

            //create the repository
            objectPoolEntry.pool = new List <GameObject>(objectPoolEntry.Count);

            //fill it
            for (int n = 0; n < objectPoolEntry.Count; n++)
            {
                GameObject newObj = (GameObject)Instantiate(objectPoolEntry.Prefab, transform);
                newObj.name = objectPoolEntry.Prefab.name;
                newObj.SetActive(false);
            }
        }

        if (PoolingInitialisationCompletedEvent != null)
        {
            PoolingInitialisationCompletedEvent();
        }
    }
示例#4
0
        /// <summary>
        /// Creates new entry in objectpool and registers the given object type. Instances are automatically created (pCount). 
        /// </summary>
        /// <param name="pPrefabToPool"> Prefab to pool.</param>
        /// <param name="pObjectType"> The object type of the prefab.</param>
        /// <param name="pCount"> Number of instances of the prefab, which are created automatically.</param>
        public void CreateNewObjectPoolEntry(GameObject pPrefabToPool, int pObjectType, int pCount)
        {
            mListIndex++;
            var newEntry = new ObjectPoolEntry();
            newEntry.Initialize(pPrefabToPool, mListIndex, pCount);
            mListOfGameobjectStacks.Add(newEntry);

            mDictionaryOfObjectTypeIndex.Add(pObjectType, mListIndex);
        }
示例#5
0
        public static GameObject PopObject(string poolName)
        {
            ObjectPoolEntry pool = GetPool(poolName);

            if (pool == null)
            {
                throw new InvalidOperationException(string.Format("Object pool {0} does not exist", poolName));
            }

            return(pool.Pop());
        }
 public void RecycleObject(GameObject recycleMe, ObjectPoolEntry retrunToPool)
 {
     //I don't love needing to specify which pool it needs to be returned to. I'll fix this later.
     if (retrunToPool == null)
     {
         //belongs to no pool, just disable it.
         recycleMe.SetActive(false);
         return;
     }
     retrunToPool.Recycle(recycleMe);
 }
 public int GetPoolID(string poolName)
 {
     for (int i = 0; i < objectPools.Length; i++)
     {
         ObjectPoolEntry o = objectPools[i];
         if (o.Name == poolName)
         {
             return(i);
         }
     }
     return(-1);
 }
    ObjectPoolEntry GetPoolFromName(string objName)
    {
        ObjectPoolEntry result = null;

        foreach (ObjectPoolEntry o in objectPools)
        {
            if (o.Name == objName)
            {
                result = o;
            }
        }
        return(result);
    }
示例#9
0
        public static void PushObject(string poolName, GameObject go)
        {
            if (go == null)
            {
                throw new ArgumentNullException(string.Format("Trying to pool a null game object in pool {0}", poolName));
            }

            ObjectPoolEntry pool = GetPool(poolName);

            if (pool == null)
            {
                throw new InvalidOperationException(string.Format("Object pool {0} does not exist", poolName));
            }

            pool.Push(go);
        }
示例#10
0
    private void UpdateObjectPoolEntry()
    {
        Entries.Clear();

        List <GameObject> _prefabs = Resources.LoadAll <GameObject>(OBJECT_POOL_ENTRIES_PATH).ToList();

        foreach (GameObject _prefab in _prefabs)
        {
            ObjectPoolEntry objectPoolEntry = new ObjectPoolEntry
            {
                Prefab = _prefab,
            };

            Entries.Add(objectPoolEntry);
        }
    }
示例#11
0
    /// <summary>
    /// Pools the object specified.  Will not be pooled if there is no prefab of that type.
    /// </summary>
    /// <param name='obj'>
    /// Object to be pooled.
    /// </param>
    public void PoolObject(GameObject obj, bool resetRigidbody = false)
    {
        if (!entriesByName.ContainsKey(obj.name))
        {
            Destroy(obj);
            return;
        }

        ObjectPoolEntry objectPoolEntry = entriesByName[obj.name];

        obj.SetActive(false);
        obj.transform.parent = transform;
        if (resetRigidbody)
        {
            obj.GetComponent <Rigidbody>().velocity = Vector3.zero;
        }
        objectPoolEntry.pool[objectPoolEntry.objectsInPool++] = obj;
    }
示例#12
0
    void InitializePool()
    {
        containerObject_ = new GameObject("ObjectPool");
        containerObject_.transform.parent = transform;

        m_Pool = new List <GameObject> [m_Entries.Length];

        for (int i = 0; i < m_Pool.Length; ++i)
        {
            ObjectPoolEntry prefab = m_Entries[i];
            m_Pool[i] = new List <GameObject>();
            for (int j = 0; j < prefab.mCount; ++j)
            {
                GameObject newObj = (GameObject)GameObject.Instantiate(prefab.mPrefab);
                newObj.name = prefab.mPrefab.name;
                PoolObject(newObj);
            }
        }
    }
示例#13
0
 // Use this for initialization
 void Start()
 {
     ContainerObject = new GameObject("ObjectPool");
     //Loop through the object prefabs and make a new list for each one.
     //We do this because the pool can only support prefabs set to it in the editor,
     //so we can assume the lists of pooled objects are in the same order as object prefabs in the array
     for (int i = 0; i < Entries.Length; i++)
     {
         ObjectPoolEntry objectPrefab = Entries [i];
         //create the repository
         objectPrefab.pool = new GameObject[objectPrefab.Count];
         //fill it
         for (int n = 0; n < objectPrefab.Count; n++)
         {
             GameObject newObj = (GameObject)Instantiate(objectPrefab.Prefab);
             newObj.name = objectPrefab.Prefab.name;
             PoolObject(newObj);
         }
     }
 }