Beispiel #1
0
        public PoolableGameObject Spawn(Transform parent = null)
        {
            PoolableGameObject spawnedObject = null;

            if (_instanceQueue.Count > 0)
            {
                spawnedObject = _instanceQueue[0];
                _instanceQueue.RemoveAt(0);
            }
            else
            {
                spawnedObject = parent != null?Object.Instantiate(_prefab, parent) : Object.Instantiate(_prefab);

                spawnedObject.SourcePool = this;
            }

            //If prefab is active, then also activate the spawned game object
            if (Prefab.gameObject.activeSelf)
            {
                spawnedObject.gameObject.SetActive(true);
            }

            spawnedObject.OnPoolableSpawned();

            return(spawnedObject);
        }
Beispiel #2
0
        public void Despawn(PoolableGameObject spawnedObject)
        {
            Debug.Assert(spawnedObject.SourcePool == this, "Attempting to despawn an object to a pool that is not its spawn source");

            spawnedObject.OnPoolableDespawn();
            spawnedObject.gameObject.SetActive(false);
            _instanceQueue.Add(spawnedObject);
        }
Beispiel #3
0
 public Pool(PoolableGameObject prefab)
 {
     _prefab = prefab;
 }
Beispiel #4
0
 public void RemoveInstance(PoolableGameObject instance)
 {
     _instanceQueue.Remove(instance);
 }