private void Awake() { if (Instance != null) { Destroy(gameObject); return; } Instance = this; for (int i = 0; i < pools.Length; i++) { nameToIndex.Add(pools[i].prefab.objectName, i); // Instantiating objects first time for (int j = 0; j < pools[i].startCount; j++) { PoolObject _obj = Instantiate(pools[i].prefab, new Vector3(1000, 1000), Quaternion.identity, transform); _obj.Deactivate(); pools[i].AddObject(_obj); } } }
public PoolObject Spawn(PoolObjectIdentificator _name, Vector3 pos, Quaternion rot) { if (!nameToIndex.ContainsKey(_name)) { Debug.LogError("<color=red>There is no pool named " + _name + " in Dictionary</color>"); return(null); } // Adding objects if queue is empty if (pools[nameToIndex[_name]].ObjectsToUse <= 0) { for (int j = 0; j < pools[nameToIndex[_name]].growBy; j++) { pools[nameToIndex[_name]].AddObject(Instantiate(pools[nameToIndex[_name]].prefab, transform)); } } PoolObject _obj = pools[nameToIndex[_name]].GetObject(); _obj.transform.SetPositionAndRotation(pos, rot); _obj.Activate(this); return(_obj); }
internal void AddObject(PoolObject _obj) { objects.Enqueue(_obj); ObjectsToUse++; }
void ReturnToPool(PoolObject poolObject) { poolObject.Deactivate(); poolObjects.Push(poolObject); }