示例#1
0
        /// <summary>
        /// Instantiate one GameObject from prefab and add it to the queue
        /// </summary>
        /// <param name="queue">PoolInstance queue</param>
        /// <param name="prefab">Prefab to be instantiated</param>
        private void InstantiateAndQueue(Queue <PoolInstance> queue, GameObject prefab)
        {
            // Spawn the game object and deactivate it
            GameObject obj = Instantiate(prefab);

            obj.SetActive(false);

            // Check if PoolInstance script has been added, otherwise add it by code
            PoolInstance instance = obj.GetComponent <PoolInstance>();

            if (instance == null)
            {
                instance = obj.AddComponent <PoolInstance>();
            }

            // Set reference to the pool and original prefabs
            instance.Pool           = this;
            instance.OriginalPrefab = prefab;

            // Despawn it
            Despawn(instance);

            // Add to queue
            queue.Enqueue(instance);
        }
示例#2
0
        /// <summary>
        /// Despawn this game object back to the object pool.
        /// Only successful if it contains PoolInstance and it's original prefab currently being pooled.
        /// </summary>
        /// <param name="obj">GameObject to despawn back to object pool</param>
        public void Despawn(GameObject obj)
        {
            if (obj == null)
            {
                return;
            }

            PoolInstance poolInstance = obj.GetComponent <PoolInstance>();

            Despawn(poolInstance);
        }
示例#3
0
        /// <summary>
        /// Despawns this game object back to the object pool.
        /// Only successful if its original prefab is currently being pooled.
        /// </summary>
        /// <param name="instance">PoolInstance whose GameObject will be despawned back to object pool</param>
        public void Despawn(PoolInstance instance)
        {
            if (instance == null)
            {
                return;
            }
            if (!pools.ContainsKey(instance.OriginalPrefab))
            {
                return;
            }

            // Disable game object
            instance.gameObject.SetActive(false);

            // Call the onDespawned event on PoolInstance
            instance.OnDespawned();

            // Queue it back to pool
            pools[instance.OriginalPrefab].poolQueue.Enqueue(instance);
        }
示例#4
0
        /// <summary>
        /// Try to spawn from the object pool with a prefab reference
        /// </summary>
        /// <param name="prefab">Same prefab reference on the ObjectPooler</param>
        /// <returns>A GameObject if it's available on the pool, null if not available</returns>
        public GameObject TryToSpawn(GameObject prefab)
        {
            if (prefab == null)
            {
                return(null);
            }

            if (!pools.ContainsKey(prefab))
            {
                return(null);
            }

            // If the pool is empty, check if we can instantiate more
            if (pools[prefab].poolQueue.Count <= 0)
            {
                if (pools[prefab].poolInfo.autoIncreasePool)
                {
                    // Instantiate more to the pool
                    InstantiateAndQueue(pools[prefab].poolQueue, prefab);
                    return(TryToSpawn(prefab));
                }
                else
                {
                    // We can't instantiate anymore
                    return(null);
                }
            }

            // Dequeue it and activate the game object
            PoolInstance instance = pools[prefab].poolQueue.Dequeue();
            GameObject   obj      = instance.gameObject;

            obj.SetActive(true);

            // Invoke the onSpawned event on PoolInstance
            instance.OnSpawned();

            return(obj);
        }