private List <GameObject> InstantiateObjectForPool(PoolableObjectConfig obj) { List <GameObject> objects = new List <GameObject>(); for (int i = 0; i < obj.count; i++) { GameObject objInstance = Instantiate(obj.prefab); objInstance.SetActive(false); objInstance.transform.SetParent(this.transform); objInstance.name = obj.prefab.name; objInstance.AddComponent <PoolableObjectInstance>(); PoolableObjectInstance poolableRef = objInstance.GetComponent <PoolableObjectInstance>(); poolableRef.Key = obj.key; poolableRef.UseStatus = PoolableObjectInstance.UsageStatus.Ready; objects.Add(objInstance); } return(objects); }
public T GetObject <T>(string key) where T : class { if (ObjectsPool.ContainsKey(key)) { List <GameObject> objectWithThisKey = ObjectsPool[key]; if (objectWithThisKey.Count > 1) { return(PrepareItemtoExit(objectWithThisKey).GetComponent <T>()); } else { PoolableObjectConfig objconf = new PoolableObjectConfig(key, objectWithThisKey[0], DefaultRegenerateCount); objectWithThisKey.AddRange(InstantiateObjectForPool(objconf)); return(PrepareItemtoExit(objectWithThisKey).GetComponent <T>()); } } else { Debug.LogError("No Object With This Key"); return(null); } }