/// <summary> /// This fuction sets the params for the object pool that will be created. /// </summary> /// <param name="GenericObj">Choose from the pooledSubject enum what the secondary "generic" object is</param> /// <param name="PoolStartSize">the amount of objects the pool starts with</param> /// <param name="IncreaseIncrement">the amount of objects that will be added when the pool hits its current limit</param> /// <param name="ManagerTicksBeforeClean">the amount of ticks needed before the pool wil check for a clean</param> /// <param name="CleanThreshold">the amount of unused objects needed for a clean to happen example</param> public void Init(PooledSubObject GenericObj, int PoolStartSize = 20, int IncreaseIncrement = 5, int ManagerTicksBeforeClean = 5, int CleanThreshold = 20) { m_ObjectsAliveInPool = 0; m_objEnum = GenericObj; PoolObj TempData = ObjectPrefab.GetComponent <PoolObj>(); TempData.Pool = this; this.m_CleanThreshold = CleanThreshold; this.m_IncreaseIncrement = IncreaseIncrement; this.m_anagerTicksBeforeClean = ManagerTicksBeforeClean; //m_DeadList = new Queue<GameObject>(); m_DeadList = new Queue <PoolObj>(); for (int i = 0; i < PoolStartSize; i++) { GameObject NewPoolGameObj = Instantiate(ObjectPrefab, transform); PoolObj poolObj = NewPoolGameObj.GetComponent <PoolObj>(); switch (GenericObj) { case PooledSubObject.Default: poolObj.GenericObj = null; break; case PooledSubObject.GameObject: poolObj.GenericObj = poolObj.gameObject; break; case PooledSubObject.VisualEffect: poolObj.GenericObj = poolObj.gameObject.GetComponent <VisualEffect>(); break; case PooledSubObject.Enemy: poolObj.GenericObj = poolObj.gameObject.GetComponent <Enemy>(); break; case PooledSubObject.Rigidbody: poolObj.GenericObj = poolObj.gameObject.GetComponent <Rigidbody>(); break; case PooledSubObject.TowerProjectile: poolObj.GenericObj = poolObj.gameObject.GetComponent <TowerProjectile>(); break; default: //m_GenericObj = null; break; } m_DeadList.Enqueue(poolObj); m_ObjectsInPool++; NewPoolGameObj.SetActive(false); } }
/// <summary> /// This returns the objectpool that contains your prefab. /// If the pool does not exist yet, it will automatically create /// </summary> /// <param name="Prefab">The prefab that will be pooled</param> /// <param name="PoolStartSize">the starting size of the object pool</param> /// <param name="IncreaseIncrement">the steps in which the pool will increase when needed</param> /// <param name="ManagerTicksBeforeClean">amount of ticks before clean check</param> /// <param name="CleanThreshold">threshold for the amount of disabled objects needed before it will be cleaned</param> /// <param name="DontDestroyOnLoadBool"></param> /// <returns></returns> public ObjectPool GetObjectPool(GameObject Prefab, int PoolStartSize = 20, int IncreaseIncrement = 5, int ManagerTicksBeforeClean = 5, int CleanThreshold = 20, bool DontDestroyOnLoadBool = false, PooledSubObject generic = PooledSubObject.Default) { if (Prefab.GetComponent <PoolObj>() == null) { Debug.LogError("POOLOBJECT COMPONENT NOT FOUND ON PREFAB"); return(null); } else { for (int i = 0; i < ObjectPools.Count; i++) { if (ObjectPools[i].ObjectPrefab == Prefab) { return(ObjectPools[i]); } } GameObject newPoolObj = new GameObject(Prefab.name + " pool"); if (DontDestroyOnLoadBool) { DontDestroyOnLoad(newPoolObj); } newPoolObj.AddComponent <ObjectPool>(); ObjectPool newPool = newPoolObj.GetComponent <ObjectPool>(); newPool.ObjectPrefab = Prefab; newPool.ObjectPoolManager = this; OnTick += newPool.OnTick; newPool.Init(generic, PoolStartSize, IncreaseIncrement, ManagerTicksBeforeClean, CleanThreshold); ObjectPools.Add(newPool); return(newPool); } }