Example #1
0
        /// <summary>
        /// Add a Prefab or GameObject to the Object Pool.
        /// </summary>
        /// <returns>
        /// The index (Pool ID) of the newly created pool, or if a pool already exists, return its index.
        /// </returns>
        /// <remarks>
        /// If you pass an existing GameObject from the scene, it will need to exist in the future to be able
        /// to spawn any additional objects outside of what was preloaded upon its addition to the Object Pool.
        /// It can be disabled, or inactive, it simply needs to exist if you want to add more later.
        /// </remarks>
        /// <param name="prefab">The Prefab or GameObject.</param>
        /// <param name="preloadAmount">The number of objects that should be preloaded.</param>
        /// <param name="spawnMore">Should more objects be added to the pool as needed?</param>
        /// <param name="slowMessage">Should Unity's SendMessage be used?</param>
        /// <param name="handleParticles">Should accomodations for Particles be made?</param>
        /// <param name="trackSpawned">Should spawned objects be tracked?</param>
        /// <param name="cullExtras">Should extra objects be removed from the Object Pool when not in use?</param>
        /// <param name="cullInterval">How often should we check for extras to cull.</param>
        public int Add(GameObject prefab, int preloadAmount, bool spawnMore, bool slowMessage,
                       bool handleParticles, bool trackSpawned, bool cullExtras, float cullInterval)
        {
            if (prefab == null)
            {
                throw new MissingReferenceException("You are passing a null gameObject reference to hObjectPool.Instance.Add()");
            }

            var tempLookup = GetPoolID(prefab.name);

            if (tempLookup == -1)
            {
                var newPool = new ObjectPoolCollection(preloadAmount, spawnMore, slowMessage,
                                                       handleParticles, trackSpawned, cullExtras, cullInterval);
                newPool.Initialize(prefab, transform, ObjectPools.Length);
                Array.Add <ObjectPoolCollection> (ref ObjectPools, newPool, false);
                if (_poolStringLookupTable == null)
                {
                    _poolStringLookupTable = new Dictionary <string, int> ();
                }
                _poolStringLookupTable.Add(newPool.Prefab.name, ObjectPools.Length - 1);
                return(ObjectPools.Length - 1);
            }
            return(tempLookup);
        }
Example #2
0
        /// <summary>
        /// Add a Prefab or GameObject to the Object Pool.
        /// </summary>
        /// <returns>
        /// The index (Pool ID) of the newly created pool, or if a pool already exists, return its index.
        /// </returns>
        /// <remarks>
        /// If you pass an existing GameObject from the scene, it will need to exist in the future to be able 
        /// to spawn any additional objects outside of what was preloaded upon its addition to the Object Pool. 
        /// It can be disabled, or inactive, it simply needs to exist if you want to add more later.
        /// </remarks>
        /// <param name="prefab">The Prefab or GameObject.</param>
        /// <param name="preloadAmount">The number of objects that should be preloaded.</param>
        /// <param name="spawnMore">Should more objects be added to the pool as needed?</param>
        /// <param name="slowMessage">Should Unity's SendMessage be used?</param>
        /// <param name="handleParticles">Should accomodations for Particles be made?</param>
        /// <param name="trackSpawned">Should spawned objects be tracked?</param>
        /// <param name="cullExtras">Should extra objects be removed from the Object Pool when not in use?</param>
        /// <param name="cullInterval">How often should we check for extras to cull.</param>
        public int Add(GameObject prefab, int preloadAmount, bool spawnMore, bool slowMessage, 
				                bool handleParticles, bool trackSpawned, bool cullExtras, float cullInterval)
        {
            if (prefab == null) {
                                throw new MissingReferenceException ("You are passing a null gameObject reference to hObjectPool.Instance.Add()");
                        }

                        var tempLookup = GetPoolID (prefab.name);
                        if (tempLookup == -1) {
                                var newPool = new ObjectPoolCollection (preloadAmount, spawnMore, slowMessage,
                                                      handleParticles, trackSpawned, cullExtras, cullInterval);
                                newPool.Initialize (prefab, transform, ObjectPools.Length);
                                Array.Add<ObjectPoolCollection> (ref ObjectPools, newPool, false);
                                if (_poolStringLookupTable == null)
                                        _poolStringLookupTable = new Dictionary<string, int> ();
                                _poolStringLookupTable.Add (newPool.Prefab.name, ObjectPools.Length - 1);
                                return ObjectPools.Length - 1;
                        }
                        return tempLookup;
        }