Josip Å tajdohar

ObjectPoolerBase.cs PoolClasses.cs ObjectPooler.cs EnemyPooler.cs
This file defines the abstract basic pool of gameObjects and its properties with a generic T (enum coresponding to a specific type of object pooler). The properties are: prefab that is used to spawn the game objects, the inital pool count, and a boolean which sets when the pool count should be expanding if the pool is empty. This file also defines two pool implementations with two enums. One implementation is for the ObjectPooler.cs and the other is for the EnemyPooler.cs. The implementation for the enemy pooler contains the reference to a scriptable object which defines the enemy's stats. The Enemy.cs and ScriptableEnemy.cs are not shown on this site as they are not really a part of this solution.

                using System.Collections;
                using System.Collections.Generic;
                using UnityEngine;
                
                namespace ObjectPooling
                {
                    /// <summary>
                    /// Generic pool item class for generic enum type.
                    /// </summary>
                    /// <typeparam name="T"> A enum that defines the pool types.</typeparam>
                    [System.Serializable]
                    public class ObjectPoolBase<T> where T : struct, System.IConvertible
                    {
                        /// <summary>
                        /// Prefab used to create instances of the pool.
                        /// </summary>
                        public GameObject objectToPool;
                
                        /// <summary>
                        /// The number of maximal pooled items at the start of the game.
                        /// </summary>
                        public int amountToPool;
                
                        /// <summary>
                        /// Should the pool extend in case there aren't any more spawned objects inside of the pool.
                        /// </summary>
                        public bool shouldExpand;
                
                        /// <summary>
                        /// Returns the pools type. Should be overriden in a defined pool.
                        /// </summary>
                        /// <returns>The type of this pool.</returns>
                        public virtual T ReturnType()
                        {
                            return default;
                        }
                    }
                
                    /// <summary>
                    /// Class containing all the information for a usable pool item. <see cref="ObjectPooler"/>
                    /// </summary>
                    [System.Serializable]
                    public class ObjectPoolItem : ObjectPoolBase<PoolType>
                    {
                        /// <summary>
                        /// The type of the item. <see cref="PoolType"/>
                        /// </summary>
                        public PoolType poolType;
                
                        /// <summary>
                        /// Getter for <see cref="poolType"/>.
                        /// </summary>
                        /// <returns> <see cref="poolType"/>. </returns>
                        public override PoolType ReturnType()
                        {
                            return poolType;
                        }
                    }
                
                    /// <summary>
                    /// Class containing all the information needed for a usable enemy pool item. <see cref="EnemyPooler"/>
                    /// </summary>
                    [System.Serializable]
                    public class EnemyPoolItem : ObjectPoolBase<EnemyType>
                    {
                        /// <summary>
                        /// Type of the enemy.
                        /// </summary>
                        public EnemyType enemyType;
                
                        /// <summary>
                        /// The scriptable asset used as a template for the enemy setup.
                        /// After the pool is populated each instance of a enemy will have his own <see cref="ScriptableEnemy"/>.
                        /// </summary>
                        public ScriptableEnemy scriptableEnemy;
                
                        /// <summary>
                        /// Getter for <see cref="enemyType"/>.
                        /// </summary>
                        /// <returns> The <see cref="enemyType"/>. </returns>
                        public override EnemyType ReturnType()
                        {
                            return enemyType;
                        }
                    }

                    /// <summary>
                    /// The pool type used by the <see cref="ObjectPooler"/>. Spawning and returning to pool will be handled by this type. Take care while writing code.
                    /// </summary>
                    public enum PoolType
                    {
                        /// <summary>
                        /// Unknown type.
                        /// </summary>
                        UNKNOWN = 0,
                
                        /// <summary>
                        /// Line renderer and audio source for the base bullet. Currently used by the pistol and uzi.
                        /// </summary>
                        FIRELINE = 20,
                
                        /// <summary>
                        /// Line renderer and audio source for a laser type weapon. Currently used by the railgun.
                        /// </summary>
                        LASERLINE = 21,
                
                        /// <summary>
                        /// Line renderer for the shotgun.
                        /// </summary>
                        SHUTGUNLINE = 22,
                
                        /// <summary>
                        /// Audio source for the shotgun.
                        /// </summary>
                        SHUTGUNSOUND = 50,
                
                        /// <summary>
                        /// Devils fireball <see cref="Projectile"/>.
                        /// </summary>
                        FIREBALL = 80,
                
                        /// <summary>
                        /// Rocket launchers <see cref="ExplosiveProjectile"/>.
                        /// </summary>
                        ROCKET = 81,
                
                        /// <summary>
                        /// The grenade object. The thrown object, not the hand held one.
                        /// </summary>
                        GRANADE = 82,
                
                        /// <summary>
                        /// Basic ammo pick up type.
                        /// </summary>
                        AMMO_PICK_UP = 100
                    }
                
                    /// <summary>
                    /// The pool type used by the <see cref="EnemyPooler"/>.
                    /// </summary>
                    public enum EnemyType
                    {
                        /// <summary>
                        /// The base enemy type.
                        /// </summary>
                        ENEMY,
                
                        /// <summary>
                        /// Larger <see cref="Devil"/> type that shoots <see cref="PoolType.FIREBALL"/>'s.
                        /// </summary>
                        DEVIL
                    }
                }