/// <summary> /// Get the selected pool object from an existing pool. /// </summary> /// <param name="poolObject">The pool object.</param> /// <returns></returns> public PoolObjects GetObjectFromPool(string itemName) { PoolObjectItem item = itemsToPool.Find(x => x.itemName == itemName); //Get the pool key for this object. int poolKey = item.objectToPool.GetInstanceID(); if (poolDictionary.ContainsKey(poolKey)) { //Get the list List <PoolObjects> poolObjectsList = poolDictionary[poolKey]; //If the object exists return the first available one. //If we don't find any abailable object we will instantiate a new one. //This is why it is very important to disable the PoolObjects for (int i = 0; i < poolObjectsList.Count; i++) { if (poolObjectsList[i].active == false) { return(poolObjectsList[i]); } } //Now, spawn a new one, if we don't find anyone InstantiateNewPoolObject(item.objectToPool, poolObjectsList, item.parentHolder); //return the last object added to the list return(poolObjectsList[poolObjectsList.Count - 1]); } else { Debug.LogError("You are trying to get a PoolObject that doesn't exist in our poolDictionary. Please make sure that you add the pool object before trying to get them."); return(null); } }
/// <summary> /// Adds a new pool object to our dictionary. /// This is called from GameplayManager or from Destroyable objects that have debris. /// </summary> /// <param name="newPoolObject">The new to pool object.</param> public void AddObjectToPoolDictionary(PoolObjectItem item)//PoolObjects newPoolObject, int size) { //Lets make sure that we don't have any pool object with this id. int poolKey = item.objectToPool.GetInstanceID(); if (poolDictionary.ContainsKey(poolKey) == false) { //If we don't have the id of this pool object in our pool manager //Then just create a new entry for our dictionary //We will Instantiate one object and add it to the list //You can extend it and spawn like 10 or more objects List <PoolObjects> newPoolObjectsList = new List <PoolObjects>(); for (int i = 0; i < item.poolAmount; i++) { InstantiateNewPoolObject(item.objectToPool, newPoolObjectsList, item.parentHolder); } poolDictionary.Add(poolKey, newPoolObjectsList); } else { Debug.LogWarning("You've tried to add a new PoolObject to our dictionary, but it already contains the object."); } }