POOL MANAGER CLASS
Breakdown of Pool Manager Class
SCRIPT DETAILS
Pool Manager Class should be a singleton for easy accessibility
GOAL
The goal of the Pool Manager Class is to manage pools of gameobjects the program will need to recycle through
FEATURES
The Pool Manager Class should have methods that can handle the following features:
Ability to pre-generate a list of bullet prefabs ---> Create a pool of bullet prefabs ---> Dynamically grow list as needed
Return requested bullets when triggered to
FULL SCRIPT
SCRIPT BREAKDOWN
SINGLETON
VARIABLES
_bulletContainer --> Empty parent container for unity hierarchy organization
_bulletPrefab --> Bullet prefab reference
_bulletPool --> List of bullet objects in our pool
Start()
LINE 3 --> Triggers the GenerateBullets() method to create a pool of 10 bullets upon the start of the program
GenerateBullets(int amountOfBullets)
GOALS --> Create a pool of bullet prefabs
LINE 1 --> Return type method that generates the bullet pool
LINE 3 --> For loop that runs for each bullet prefab that is generated
LINE 5 --> Each generated bullet is a copy of the bullet prefab
LINE 6 --> Each bullet is placed within the bullet container to keep the hierarchy organized
LINE 7 --> Each bullet is set to inactive until needed
LINE 8 --> Each bullet is added to the bullet Pool list
LINE 10 --> Once list is generated, it is returned to the program
RequestBullet()
GOAL --> Return requested bullets when triggered to & Dynamically Grow List as Needed
LINE 1 --> PUBLIC method to return requested Bullet GameObjects
LINE 3 --> Foreach loop that runs to make sure there is an inactive bullet prefab within the bulletPool
LINE 5 --> If statement that runs if a bullet prefab is found inactive
LINE 7 --> Sets bullet prefab active
LINE 8 --> Returns bullet Prefab | breaks out of method
The following code runs IF the program finds all bullet prefabs active
LINE 11 --> Instantiates a new bullet prefab
LINE 12 --> Places newly instantiated bullet within the bullet container for hierarchy organization
LINE 13 --> Adds newly instantiated bullet prefab to the pool
LINE 15 --> Returns newly instantiated bullet
NOTES
ActiveInHierarchy vs ActiveSelf
Within line 5 of the RequestBullet() Method, we notice that within the if statement there is a snippet of code that states bullet.activeInHierarchy.
activeInHierarchy returns a bool value determined by if a gameObject is active within the scene
--> Only returns true if BOTH the gameObject and ALL it's parent objects are active
activeSelf returns the local active state of the gameobject
--> GameObject may be inactive because a parent is not active, even if it returns true
Last updated