/// <summary> /// Called by each PoolScript on its own, this adds it to the dictionary. /// </summary> public static void Add(PoolScript poolScript) { //check if the PoolScript does not contain a prefab if (poolScript.prefab == null) { //Debug.LogError("Prefab of poolScript: " + poolScript.gameObject.name + " is empty! Can't add poolScript to Pools Dictionary."); return; } //check if the PoolScript has been added already if (Pools.ContainsKey(poolScript.prefab)) { //Debug.LogError("PoolScript with prefab " + poolScript.prefab.name + " has already been added to Pools Dictionary."); return; } //add it to dictionary Pools.Add(poolScript.prefab, poolScript); }
/// <summary> /// Creates a new PoolScript at runtime. This is being called for prefabs which have not been linked /// to a PoolScript in the scene in the editor, but are called via Spawn() nonetheless. /// </summary> public static void CreatePool(GameObject prefab, int preLoad, bool limit, int maxCount) { //debug error if poolScript was already added before if (Pools.ContainsKey(prefab)) { //Debug.LogError("PoolScript Manager already contains PoolScript for prefab: " + prefab.name); return; } //create new gameobject which will hold the new PoolScript component GameObject newPoolGO = new GameObject("PoolScript " + prefab.name); //add PoolScript component to the new gameobject in the scene PoolScript newPoolScript = newPoolGO.AddComponent <PoolScript>(); //assign default parameters newPoolScript.prefab = prefab; newPoolScript.preLoad = preLoad; newPoolScript.limit = limit; newPoolScript.maxCount = maxCount; //let it initialize itself after assigning variables newPoolScript.Awake(); }