/// <summary> /// Add new item and add it to the active pool /// </summary> private T AddNewItemToPool() { T instance = (T)Activator.CreateInstance(typeof(T)); ActivePool.Add(instance); return(instance); }
/// <summary> /// Add new item and add it to the active pool with overflow for smaller asteroids /// </summary> private T AddNewItemToPool(float size, UnityEngine.Vector3 startPos, float rotation) { T instance = (T)Activator.CreateInstance(typeof(T)); ActivePool.Add(instance); instance.OnActivate(size, startPos, rotation, UnityEngine.Random.Range(0.0005f, 0.03f)); return(instance); }
/// <summary> /// Get the First Available Object from the Pool. /// </summary> /// <param name="key"></param> /// <returns></returns> public GameObject GetObject(int key) { if (!Pool.ContainsKey(key)) { return(null); } for (int i = Pool[key].Count - 1; i >= 0; i--) { GameObject cachedGameObject = Pool[key][i]; if (!cachedGameObject.activeInHierarchy) { cachedGameObject.SetActive(true); if (GetIReference(cachedGameObject) != null) { Pool[key].RemoveAt(i); ActivePool.Add(cachedGameObject); Reference.Index = Mathf.Clamp(Reference.Index, 0, ActivePool.Count - 1); } return(cachedGameObject); } } if (ExpandPool && ExpandSize > 0) { GameObject cachedGameObject = null; for (int i = 0; i < ExpandSize; i++) { cachedGameObject = InstantiatePrefab(Pool[key][0], Pool[key][0].transform.parent); SetReferences(cachedGameObject, key, Pool[key], ActivePool); Pool[key].Add(cachedGameObject); } if (Reference != null) { Pool[key].RemoveAt(Pool[key].Count - 1); ActivePool.Add(cachedGameObject); Reference.Index = Mathf.Clamp(Reference.Index, 0, ActivePool.Count - 1); } if (cachedGameObject != null) { cachedGameObject.SetActive(true); return(cachedGameObject); } } return(null); }
/// <summary> /// Same as above. Activate item with overflow for smaller objects with positions /// </summary> // TODO: Clean up to use an interface instead of variables in the overflow public T ActivateItem(T item, float size, UnityEngine.Vector3 startPos, float rotation) { // size, startPosition, Direction, Speed); item.OnActivate(size, startPos, rotation, UnityEngine.Random.Range(0.0005f, 0.03f)); item.Active = true; if (InactivePool.Contains(item)) { InactivePool.Remove(item); ActivePool.Add(item); } return(item); }
/// <summary> /// Activate an item /// </summary> public T ActivateItem(T item) { // size, startPosition , Direction , Speed); item.OnActivate(1, new UnityEngine.Vector3(UnityEngine.Random.Range(-11, 11), UnityEngine.Random.Range(-6, 6), 0), UnityEngine.Random.Range(0, 360), UnityEngine.Random.Range(0.0005f, 0.03f)); item.Active = true; if (InactivePool.Contains(item)) { InactivePool.Remove(item); ActivePool.Add(item); } return(item); }
/// <summary> /// Get the First Available Object from the Pool. /// </summary> /// <returns></returns> public GameObject GetObject() { for (int i = Pool.Count - 1; i >= 0; i--) { GameObject cachedGameObject = Pool[i]; if (!cachedGameObject.activeInHierarchy) { cachedGameObject.SetActive(true); if (GetIReference(cachedGameObject) != null) { Pool.RemoveAt(i); ActivePool.Add(cachedGameObject); Reference.Index = Mathf.Clamp(Reference.Index, 0, ActivePool.Count - 1); } return(cachedGameObject); } } if (ExpandPool) { GameObject cachedGameObject = null; for (int i = 0; i < ExpandSize; i++) { cachedGameObject = InstantiatePrefab(Prefab, Pool[0].transform.parent); SetPoolReference(cachedGameObject, Pool, ActivePool); Pool.Add(cachedGameObject); } if (Reference != null) { Pool.RemoveAt(Pool.Count - 1); ActivePool.Add(cachedGameObject); Reference.Index = Mathf.Clamp(Reference.Index, 0, ActivePool.Count - 1); } cachedGameObject.SetActive(true); return(cachedGameObject); } return(null); }