/// <summary> /// Add a prefab to the Pool list, and create a pool. Returns the list index. /// </summary> /// <param name="_prefab"></param> /// <param name="_growBy"></param> /// <param name="_scriptToAdd">Indicate a typeof(Component) that you want added (if not already there) to the root of all instantiated pool items.</param> /// <returns></returns> public static void AddPrefabToPool(GameObject _prefab, int startingSize = 8, int _growBy = 8, Type _scriptToAdd = null, bool tidyUp = false) { #if UNITY_EDITOR if (!_prefab) { Debug.LogWarning("Attempt to add null object to the pool."); } #endif if (poolItemDefs.ContainsKey(_prefab)) { return; } pools.Add(_prefab, new Stack <Pool>()); var poolDef = new PoolItemDef(_prefab, _growBy, _scriptToAdd); poolItemDefs.Add(_prefab, poolDef); GrowPool(_prefab, startingSize); if (tidyUp) { #if UNITY_EDITOR _prefab.transform.parent = tidyUpParent; #endif _prefab.gameObject.SetActive(false); } }
private static void GrowPool(GameObject _prefab, int growAmt = -1) { #if UNITY_EDITOR // put pooled items onto a parent to tidy up in editor mode - completely ignored in release if (tidyUpInEditorMode && !tidyUpParent) { tidyUpParent = new GameObject("Pool Items (Editor Only)").transform; } #endif PoolItemDef def = poolItemDefs[_prefab]; // Grow by the amount originally specified if no growAmt is given. int growby = (growAmt < 1) ? def.growBy : growAmt; for (int i = 0; i < growby; i++) { #if UNITY_EDITOR // put pooled items onto a parent to tidy up in editor mode - completely ignored in release GameObject go = Instantiate(def.prefab, (tidyUpInEditorMode) ? tidyUpParent : null); #else GameObject go = Instantiate(def.prefab); #endif AddItemToPool(go, _prefab, def); } }
private static void AddItemToPool(GameObject go, GameObject _prefab, PoolItemDef def) { go.SetActive(false); //Precache some stuff so this pool get later doesn't require any GetComponent calls. Pool p = go.AddComponent <Pool>(); p.CacheComponents(); // Add the scrpitToAdd if it doesn't already exist if (def.scriptToAdd != null && go.GetComponent(def.scriptToAdd) == null) { p.extraScript = go.AddComponent(def.scriptToAdd); } p.origPrefab = _prefab; // Add this new instance to the pool pools[_prefab].Push(p); }
private static void GrowPool(GameObject _prefab, int growAmt = -1) { #if UNITY_EDITOR // put pooled items onto a parent to tidy up in editor mode - completely ignored in release if (tidyUpInEditorMode && !tidyUpParent) { tidyUpParent = new GameObject("Pool Items (Editor Only)").transform; } #endif PoolItemDef def = poolItemDefs[_prefab]; // Grow by the amount originally specified if no growAmt is given. int growby = (growAmt < 1) ? def.growBy : growAmt; for (int i = 0; i < growby; i++) { #if UNITY_EDITOR // put pooled items onto a parent to tidy up in editor mode - completely ignored in release GameObject go = Instantiate(def.prefab, (tidyUpInEditorMode) ? tidyUpParent : null); #else GameObject go = Instantiate(def.prefab); #endif go.SetActive(false); //Precache some stuff so this pool get later doesn't require any GetComponent calls. Pool p = go.AddComponent <Pool>(); p.CacheComponents(); // Add the scrpitToAdd if it doesn't already exist if (def.scriptToAdd != null && go.GetComponent(def.scriptToAdd) == null) { p.extraScript = go.AddComponent(def.scriptToAdd); } p.origPrefab = _prefab; // Add this new instance to the pool pools[_prefab].Push(p); } }
public static void GrowPool(int poolIndex) { for (int i = 0; i < poolItemDefs[poolIndex].growBy; i++) { PoolItemDef pt = poolItemDefs[poolIndex]; GameObject go = Instantiate(pt.prefab); go.SetActive(false); Pool p = go.AddComponent <Pool>(); // Add the scrpitToAdd if it doesn't already exist if (pt.scriptToAdd != null && go.GetComponent(pt.scriptToAdd) == null) { go.AddComponent(pt.scriptToAdd); } p.poolIndex = poolIndex; pools[poolIndex].Push(p); } }