Introduction To Object Pooling In Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're like me you probably instantiate and destroy a lot of stuff in your code but wouldn't it be much cooler to create a certain amount of objects initially and just reuse them over and over again to improve performance and that's where object pooling comes in handy object pooling was invented back in 1684 from kevin leopold pool who also invented the pool table and the swimming pool unfortunately he passed away last year so wikipedia says object pooling can offer a significant performance boost in situations where the cost of initializing a class instance is high and the rate of instantiation and destruction of a class is high in this case objects can frequently be reused and each reuse saves a significant amount of time i don't know about you but that sounds pretty useful to me so without further ado let's start with the tutorial i set up a small demo scene with a little spaceship that shoots whenever the player presses the left mouse button and as soon as a bullet hits the wall the bullet gets destroyed but as we learned not even 30 seconds ago there is a better way to handle stuff like that so let's create an empty game object called object pool add a script to it and open it up in order to access our object pool from other classes without having a reference to it we can use the singleton pattern to do this we add a public static object pool called instance to our script in the awake method we then set instance to this if instance is null next we need a private list for our pooled game objects and a private integer for the amount last but not least we need a reference to the game object that we want to pool in our case a bullet prefab add a for loop to the start method and set the length to our previously created integer in the for loop instantiate the object you want to pool and store it in a game object afterwards we disable the game object and add it to the list of pooled objects now we need a public method that returns a game object add a for loop again but this time set the length to the count of our pooled objects if we find the game object that's not active in the hierarchy in other words an available bullet that we can shoot we want to return it otherwise we want to return null the only thing left to do is to replace the instantiation and destruction of bullets in our other scripts in the player script instead of instantiating a bullet at runtime we want to request a bullet from our object pool and store it in a game object if the game object is not null which means there is an available bullet we set its desired position and enable it and in the bullet script simply disable the game object whenever it hits a wall instead of destroying it that's it back in unity there is one more thing we need to do select the object pool and drag the bullet prefab onto the bullet prefab reference in the script let's hit play and see what happens as you can see we now have 20 inactive bullets in the hierarchy and as soon as we start shooting they become active and as soon as they hit the wall they become inactive again so basically object pooling is nothing else than enabling and disabling pre-loaded objects but it's not hard to implement and your cpu will thank you if this video brought you closer to object pooling please leave a like and consider subscribing and as always thank you for watching and i'll see you next time
Info
Channel: bendux
Views: 3,513
Rating: undefined out of 5
Keywords: bendux, unity, tutorial, object pooling, object pool, indie, game development, how to
Id: YCHJwnmUGDk
Channel Id: undefined
Length: 4min 14sec (254 seconds)
Published: Fri Mar 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.