private void CreateAll() { for (int i = 0; i < _pooledObjectsStats.Length; i++) { PooledObjectStats stats = _pooledObjectsStats[i]; Queue <Transform> queue = new Queue <Transform>(); GameObject parent; if (stats.prefab.GetComponent <RectTransform>() == null) { parent = new GameObject(stats.name + "s"); parent.transform.SetParent(transform); } else { parent = new GameObject(stats.name + "s", typeof(RectTransform)); parent.transform.SetParent(_canvas.transform); ResetRectTransform(parent); } stats.parent = parent.transform; for (int j = 0; j < _pooledObjectsStats[i].number; j++) { Transform pooledObject = Create(queue, stats); queue.Enqueue(pooledObject); } _poolDictionary.Add(_pooledObjectsStats[i], queue); } }
public void Despawn(PooledObjectStats stats, Transform despawnedObject) { Queue <Transform> queue = _poolDictionary[stats]; despawnedObject.position = new Vector3(0, YPOS, 0); despawnedObject.rotation = Quaternion.identity; queue.Enqueue(despawnedObject); despawnedObject.gameObject.SetActive(false); }
public Transform Spawn(PooledObjectStats stats, Vector3 position, Quaternion rotation) { Transform spawnedObject = Spawn(stats); spawnedObject.position = position; spawnedObject.rotation = rotation; return(spawnedObject); }
private Transform Create(Queue <Transform> queue, PooledObjectStats stats) { Transform pooledObject = _resourceFactory.Instantiate(stats.prefab, new Vector3(0, YPOS, 0), stats.parent); IPooledObject pooledObjectComponent = pooledObject.GetComponent <IPooledObject>(); if (pooledObjectComponent != null) { pooledObjectComponent.PooledObjectStats = stats; } pooledObject.gameObject.SetActive(false); return(pooledObject); }
public Transform Spawn(PooledObjectStats stats) { Queue <Transform> queue = _poolDictionary[stats]; if (queue.Count == 0) { queue.Enqueue(Create(queue, stats)); if (stats.expandBy == ExpandBy.Doubling) { for (int j = 0; j < stats.number - 1; j++) { queue.Enqueue(Create(queue, stats)); } } } Transform spawnedObject = queue.Dequeue(); spawnedObject.gameObject.SetActive(true); ReInitialize(spawnedObject); return(spawnedObject); }