/// <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> /// Spawned objects must call this to destroy themselves (i.e. re-enter the inactive pool). /// </summary> /// <param name="bubble">GameObject to "destroy" (return to inactive pool).</param> public void Destroy(GameObject go) { Transform goT = go.GetComponent <Transform>(); ActivePool.Remove(goT); goT.parent = this.InactivePool; }
private void OnDisable() { _elapsedTime = 0; transform.position = Vector3.zero; ActivePool.RemoveAt(Index); Pool.Add(gameObject); }
/// <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); }
private void Awake() { Instance = this; evt = new EventCenter(); timeLine = new TimeLine(); activePool = new ActivePool(); DontDestroyOnLoad(gameObject); InitData(null); }
private void Awake() { Instance = this; evt = new EventCenter(); timeLine = new TimeLine(); activePool = new ActivePool(); DontDestroyOnLoad(gameObject); DataManager.Instance.GetAllPlayer(); }
/// <summary> /// Return active item to inactive item list /// </summary> public T ReturnObjectToInactive(T item) { if (ActivePool.Contains(item)) { ActivePool.Remove(item); } item.OnDisable(); item.Active = false; InactivePool.Add(item); return(item); }
/// <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> /// Destroys (returns to inactive pool) all objects. /// </summary> public void DestroyAll() { if (this.ActivePool != null && this.InactivePool) { while (ActivePool.Last != null) { ActivePool.Last.Value.parent = this.InactivePool; ActivePool.RemoveLast(); } } }
/// <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> /// Spawns an object from the pool. /// </summary> /// <returns>The spawned (depooled) object.</returns> /// <param name="position">Position.</param> public GameObject Spawn(Vector3 position) { // handle exhausted pool if (this.InactivePool.childCount == 0) { if (this.ExhaustionBehaviour == ExhaustBehaviour.UseOldest) { Transform deactivate = this.ActivePool.First.Value; ActivePool.RemoveFirst(); deactivate.parent = this.InactivePool; } else if (this.ExhaustionBehaviour == ExhaustBehaviour.Grow) { int rndIdx = Random.Range(0, this.ActivePool.Count); LinkedListNode <Transform> current = this.ActivePool.First; int counter = 0; while (current != null && current.Next != null && counter < rndIdx) { current = current.Next; ++counter; } if (current != null && current.Value) { AddPrefabToPool(current.Value.gameObject, this.PoolSize); ++this.PoolSize; //#if SAGO_DEBUG //Debug.Log(string.Format("Growing Pool {0} to {1}", this.name, this.PoolSize), this); //#endif } } else { return(null); } } if (this.InactivePool.childCount > 0) { Transform activated = this.InactivePool.GetChild(this.InactivePool.childCount - 1); activated.parent = this.Transform; activated.position = position; ActivePool.AddLast(activated); if (this.ApplyRandomScaling) { float scale = Random.Range(this.ScaleRange[0], this.ScaleRange[1]); activated.localScale = new Vector3(scale, scale, scale); } return(activated.gameObject); } return(null); }
/// <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); }
private void OnDisable() { ActivePool.RemoveObjectFromPool(name); }
private void OnEnable() { ActivePool.AddObjectToActiveObjectPool(this); }
public ActivePool() { instance = this; }