Unity Game Dev - Why I Wrote My Own Object Pool Manager

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello there and welcome to my channel midnight oil software where i talk about game development using unity and occasionally digital art today i wanted to do something a little bit different today i wanted to respond to a question as someone posted on my youtube page so not too long ago i posted a video about how to do object pooling in unity and in that video i used my own homegrown object pooling manager and this particular user pushed the button made this comment on my channel he said this is not using unity engine.pool how come now what he's asking is why am i not using the built-in object puller that's provided with unity and i did respond to his question here but i thought that was a good question and that it really warranted more of an explanation as to why i'm using my own home brewed object pulling manager instead of what's built in with unity and so i wanted to address that in this video and i'm going to start by talking about how the built-in object pulling in unity works so the way it works in unity is actually just the standard c-sharp dot net object pool and it requires you to specify the type of object that you want to pull so in this example here they're creating a pool of particle systems and so they instantiate here new object pool particle system and the constructor takes a few parameters it takes some callback functions and then some other parameters for managing the pool and if you look at this create pooled item called back function that it's got here this is what gets called when you call the dot get method on your object pool and what it does is it creates a game object of type particle system adds a component particle system to it and as you can see it's very specific to a particle system this pool will only work with particle systems now if you remember in my tutorial um for my object pool example i have a spawner class here that has a collection of templates um prefabs actually so if you go in and look at the spawner script and go to the top of the script you'll see i've got this list of game objects that i'm calling prefabs okay so in the editor i'm just assigning four different types of game objects to that collection of game objects which are the prefabs that i'm going to spawn with my spawner and so i have a little function here spawn random object and this is called repeatedly from the on enable method um every .1 seconds and i'm grabbing a random game object from that collection of prefabs and i'm calling spawn game object using that prefab and so if you want to see that excuse me if i want to see that in action i will click play and you can see it's spawning every tenth of a second a random game object from this collection so if i wanted to use the built-in object puller that unity provides i would have to create a pool for every type of prefab that i want to create so for instance i could have var pool equals new object pool game object and then in my parameters here i could have create ball object for instance and then that that thing could create a ball prefab so hopefully you can see how what a management nightmare that would be for every single type of prefab that i want to spawn i would have to create a separate pool just for that prefab type and then if i ever created a new prefab i'd have to create a new pool just for that prefab type and my manager would have to look up based on the name of the prefab which object pool to give you so mine can be consumed perfectly generically totally agnostic of the underlying type of game object this might be so i wanted to demonstrate how easy this would be to implement in your own game and by the way if you think i'm off base about this i actually when i first started looking at the built-in unity object pooling thought maybe i'm missing something maybe this isn't uh as difficult as i thought to work with prefabs and so i actually went up to the unity forum and i posted a question about it and i explained how i use prefabs with my object pool and explain the difficulty that i was running into trying to use the built-in object pooler just in case i missed something and there was something obvious i was overlooking and the official answer from unity is that their pool system is not written for game objects and prefabs and that for my use case he would stick to what i already have so it does say that there is a way you could use it with prefabs however it requires an exact type so hopefully that answers the question as to why i came up with my own object pooling manager and shared it with the community but to show you just how easy it would be to use i'm going to take you to my 3d space shooter tutorial if you've been following my channel you know that i've been doing a tutorial series on how to create a 3d space shooter in unity so in this i've got a escort fighter here that has a pair of blasters and the blaster shoots a projectile and if we go and look at the script for that blaster you'll see that it takes a serialized field for a projectile prefab now in this case i know it's going to be a projectile this is a base class that i'm going to derive from for every type of projectile i might have and i can show you if we go back into the editor that in this case i'm using a player projectile but the point being this guy doesn't really care what kind of projectile this prefab is in fact this could just as easily be a game object and it would work just as well now i'm not using object pulling here i'm only instantiating this and then if we go and look at the definition for a projectile you can see that after it runs out of fuel i destroy it so i'm instantiating and destroying these projectiles which as you know is not very performant if you have lots of these things being instantiated and destroyed because the garbage collectors got to go and clean them up so if i wanted to use my object pool manager in this game how would i do that well one thing that i've done is i've taken my object pool manager and i've extracted it to a package and i've actually submitted this to the unity asset store and it's under evaluation now uh in the approval process so hopefully it'll be approved sometime soon and it will be available on the asset store in the meantime you can use the github link that i'm putting in the description of the video to grab this package and import it into your game and so the way you can do that is assuming you've downloaded it from github you just say assets import package custom package and i've already browsed to my object pool example where i've got this unity package here and if i open that you'll get the import dialog and you can see that it has a demo scene which is essentially this scene right here that i have in my demo that shows how to use the object pooler we don't need that for my game so i'm going to uncheck that but we do want everything else we probably don't need the user's guide it's a pdf file but i'll go ahead and leave that checked so just to look at what's in this package we have the actual object pool manager itself which is a prefab and it's got this script associated with it and i have some helper functions now one script in particular this load persistent object script is attached to this manager's prefab and it's under a resources folder so if you include this when you do your import then what's going to happen is as soon as you run your game by virtue of that script that has that runtime load attribute it will automatically load my manager's prefab from your resource folder and that has attached to it in object pool manager so let's go look at that script real quick load persistent objects it's got this runtime initialize on load method is set to load before the scene loads as soon as this executes it will grab that manager's prefab from the resources folder instantiate it and call don't destroy unload on it so what that means is as soon as i run the game let's go out here where we can see my hierarchy i'll collapse this you'll see appearing here in the hierarchy a don't destroy on load node and underneath of that is our object puller of course i gotta turn off maximize so we can still see this and there's our don't destroy unload and underneath of that you can see we've got my managers seems like it's loaded two of them oh i already have one in my game so it did create another one of those and it's got an object pool manager now if i didn't want that i could just remove that and then i could just add the prefab for the object pool manager to my managers class here and that would work just as well so if i want to use that object pool manager there's one other thing i have to do if you've been following my series on 3d space shooter you'll know that in my scripts folder i added an assembly definition and i'm currently only pulling in a reference to the detonator package that we had downloaded if i was to try and say i go into my blaster script here if i was to try and instead of calling instantiate call object pool manager it's not going to be able to find the object pool manager in my project and so in order to make that work it's really simple i just go into my object pool manager into the scripts folder and i create an assembly definition and i will call this midnight oil software and if you're not using an assembly definition in your game you won't have to do this step this is just because i'm using them in my game to kind of segregate things if i go back into my scripts folder select the assembly definition and then under references click the plus sign and select midnight oil software apply go back into our blaster script now if i say object pool manager you see it finds it spawn game object and we'll use our projectileprefab.gameobject we'll use the same parameters muzzle.position transform dot rotation that will spawn this projectile and if it's already got one in the pool it will use it now we also need to go into our projectile and where we would destroy it instead of destroying it we can say object pool manager dot d spawn game object game object oh and before i do that i want to show you what it looks like without this change so let's go back into our game i'm going to hit play and you'll see the don't destroy unload created here and you see there's our managers clone with the object pool manager now if i come in here and start shooting you see that because i'm not using the object pooler for those it's creating and destroying game objects and they're just being added to the regular hierarchy and destroyed they're not under don't destroy on load all right so let's go back into our script and remove those comments go back into the projectile remove those comments and let's do this again all right if i come in here and start shooting you see that those projectiles were created under don't destroy unload and they're not being destroyed they're just being deactivated and reused all right so one thing i do want to point out about the way that my object pool manager works is under the covers i am using a dictionary that maps the prefab name to a queue of game objects but i'm only storing these game objects in a pool when they're deactivated so when you despawn a game object we deactivate it and we get the pool for it and we enqueue it into the pool so what this means is unlike the built-in object pooling that unity provides and the way many other people implement object pooling i'm not tracking active game objects in my pool so when i go to spawn a new object i don't have to search through all the active ones to find an inactive one that i can give you to reuse when you spawn a new object or when you grab one from the pool it's removed from the pool and activate it and then when you deactivate it it's added back to a pool so that makes spawning new ones from the pool even faster than the implementation that would be provided by unity so i hope that was helpful i hope that helped explain why i would write my own object pool manager and why you might want to use it and just how easy it is to plug it into your application and if you did find it helpful do me a favor and click that like and subscribe button it really does help me to grow my channel and i want to get this material in front of as many people as possible to help them out so if you did find it useful and you liked it not only click like and subscribe but give me a comment and you have any questions or anything any suggestions i'd appreciate hearing from you um i appreciate the fact that that um press the button dude um asked that question on my channel and gave me this opportunity to respond and so yeah i'd love to hear from you i'd like to hear what you find useful on my channel and if there's any suggestions for things i can improve or any topics in particular you'd like to see me cover please let me know so once again thanks so much for watching and good luck on your game development journey [Music] you
Info
Channel: Midnite Oil Software LLC
Views: 1,179
Rating: undefined out of 5
Keywords: unity, tutorial, games, game development, unity game engine, madewithunity, indiegamedev, indiedev, unity2d, unity3d, tutorials, objectpooling, object pooling in unity
Id: TswH7TYTLhQ
Channel Id: undefined
Length: 16min 56sec (1016 seconds)
Published: Tue Jul 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.