Object Pooling :
Before go into Object pooling lets move to Object Cycling.
Object Cyling : It’s a process of instanting and destroying gameobjects. Its a common case in a game where we create and destroy objects , but we should
keep on note that every time instantiating new objects costs performance and fragments the RAM.Destroying objects also costs performance because of the Garbage Collector.
Games like Jamestown,Cuphead,Subway surfers,Bullet Hell-everytime a new object is born and new object is destroyed,thought it costs performace we ave to do that so what can be the solution.
Instantiate new objects only once ,instead of destroying them we can disable them and reuse when needed ,so to implement this technique we use Object Pooling Mechanisms.
Object Pooling :
It’s one of the way to optimize game projects and lower the burden on CPU when instantiating and destroying gameobjects .
The processing power of the CPU should be used in a right way ,i.e to process more important task in a game rather than being a Garbage collector.
So to relieve the burden we use the concept called Object Pooling.
Object pooling is one of the most important design pattern used mostly in shooting,endless runner type of games.
What is Object Pool ?
Object Pool is a pool of objects what we need in game.If we want any object from the pool get from it , use it and throw to the pool again.
It gives better performance but loading time will be little bit slow as it creates pool of objects before the game starts.
Mechanism of Object Pool :
- Create an object in the pool
2.deactivate it
3.add it to the collections.
4.No need to instantiate , just activate the object created when needed.
5.When object is no longer needed instead of destroying it ,put the object in the pool and deactivate it.
Shooting the bullet when space key pressed ,here the object is instantiated but at the same position , the bullet is not moving
so to move the bullet go to Bullest.cs and write the script
the bullet in the game will move on forever, so to remove the bullet we destroy the object and Garbage Collector will see the rest.
This process of Instantiating and Destroying objects is called Object Cycling.
Now let see Object Pooling
now we initialized pool next thing is to get Object from pool
cheking for position,rotation and returning Object to pool
Instantiating object from pool and now we can script in Bullet.cs , so that we return object to the pool(i.e instead of Destroying Objects)
This is how object pooling works and will show you the hierachy panel ,when game is played
Please comment for any mistakes.
Happy Gaming