/// <summary> /// </summary> /// <param name="gotopool">Must implement IPoolable</param> /// <param name="numberofinitialelements"></param> /// <param name="startaddingtosecondarypoolat"></param> private void InitializeStack(AbstractPoolable gotopool, int numberofinitialelements = 4, bool useactivepool = false, bool isautocleanupenabled = true) { autoCleanUpEnabled = isautocleanupenabled; if (gotopool == null) { Debug.LogError("Gotopool is null!"); return; } useActivePool = useactivepool; activePool = new List <AbstractPoolable>(); rangeOfPool = numberofinitialelements; goToPool = gotopool; pool = new Stack <AbstractPoolable>(); for (int i = 0; i < numberofinitialelements; i++) { var gotoadd = GenerateNewInstance(); gotoadd.gameObject.SetActive(false); pool.Push(gotoadd); } //setlists(); }
private AbstractPoolable GenerateInstances(Vector3 position, bool isactive = false) { AbstractPoolable gotoaddtolist = null; if (numberOfInstancesToCreateWhenAllQueuesEmpty == 0) { numberOfInstancesToCreateWhenAllQueuesEmpty = 3; } //add new instances to the list for (int i = 0; i < numberOfInstancesToCreateWhenAllQueuesEmpty; i++) { gotoaddtolist = GenerateNewInstance(); pool.Push(gotoaddtolist); gotoaddtolist.gameObject.SetActive(isactive); // gotoaddtolist.transform.SetParent(primaryParentGO.transform); //Debug.Log("Adding element to the stack: " + gotoaddtolist + i); } Debug.Log("Adding elements: " + numberOfInstancesToCreateWhenAllQueuesEmpty + ", to the stack: " + gotoaddtolist); gotoaddtolist = GenerateNewInstance(); gotoaddtolist.transform.position = position; gotoaddtolist.gameObject.SetActive(isactive); //setlists(); return(gotoaddtolist); }
private void AddInstanceToPool(AbstractPoolable objecttohide) { objecttohide.gameObject.SetActive(false); if (useActivePool) { activePool.Remove(objecttohide); } if (pool != null) { pool.Push(objecttohide); } //setlists(); }
public ObjectPool(AbstractPoolable gotopool, int numberofinitialelements = 14, bool useactivepool = false, bool isautocleanupenabled = true, float cleantimeinterval = 3f) { InitializeStack( gotopool, numberofinitialelements, useactivepool, isautocleanupenabled); if (isautocleanupenabled) { m_scheduler = new GameObject(gotopool.ToString() + "_memorycleanscheduler").AddComponent <MemoryPoolAutoCleanUpScheduler>(); m_scheduler.CleanTimeInterval = cleantimeinterval; m_scheduler.OnAutoMemoryCleanRequest += AutoCleanUp; } }
/// <summary> /// Pooling back. /// </summary> /// <param name="objecttohide"></param> public void HideObject(AbstractPoolable objecttohide) { if (!objecttohide) { Debug.LogError("Objecttohide is null! Abort pooling!"); return; } AbstractPoolable objectpoolable = objecttohide.GetComponent <AbstractPoolable>(); if (objectpoolable == null) { Debug.LogError("A non-poolable object is trying to get into the pooling system." + "Add an IPoolable script to your gameobject! " + "Destroying! "); DestroyGO(objecttohide); return; } //setlists(); AddInstanceToPool(objecttohide); }