private GameObject SpawnItem(ItemDPool item) { GameObject itemPool = Instantiate(item.prefab, transform, true); itemPool.AddComponent <TagPoolerObject>(); itemPool.GetComponent <TagPoolerObject>().tagItem = item.tagItem; itemPool.SetActive(false); return(itemPool); }
private ItemDPool GetItemPool(string tagItem) { ItemDPool item = null; foreach (ItemDPool itemTemp in itemsPool) { if (itemTemp != null) { if (tagItem.Equals(itemTemp.tagItem)) { item = itemTemp; break; } } } return(item); }
public GameObject GetObject(string tagItem, Vector3 position, Quaternion rotation, bool active = true) { if (!poolDictionary.ContainsKey(tagItem)) { Debug.LogWarning("Pool with tag " + tagItem + " doens't exist."); return(null); } GameObject objPooled; if (poolDictionary[tagItem].Count > 0) { objPooled = poolDictionary[tagItem].Dequeue(); objPooled.transform.position = position; objPooled.transform.rotation = rotation; objPooled.SetActive(active); return(objPooled); } else { ItemDPool itemPool = GetItemPool(tagItem); if (itemPool != null) { if (itemPool.isExpandable) { Debug.Log("Expanding pool <color=red>" + tagItem + "</color>"); GameObject newItemPool = SpawnItem(itemPool); newItemPool.transform.position = position; newItemPool.transform.rotation = rotation; newItemPool.SetActive(active); Transform[] children = gameObject.GetComponentsInChildren <Transform>(); Transform parent = null; foreach (Transform trans in children) { if (trans.name == tagItem) { parent = trans; break; } } if (parent == null) { parent = new GameObject(itemPool.tagItem).transform; parent.transform.SetParent(transform); } newItemPool.transform.SetParent(parent); return(newItemPool); } else { Debug.LogWarning("Object isn't expandable."); return(null); } } else { Debug.LogWarning("Pool with tag " + tagItem + "doens't exist."); return(null); } } }